A - Star

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君はゲームで遊んでいます。

このゲームでは、今までに集めたコインの枚数が 100 の倍数になるごとにご褒美がもらえます。

高橋君が今までに集めたコインの枚数は X 枚です。次にご褒美をもらうためには、あと何枚のコインを集めればよいでしょうか? (X100 の倍数の場合、コインを累計で X 枚集めたことに対するご褒美はすでにもらったとします。)

制約

  • 0 \leq X \leq 10^5

入力

入力は以下の形式で標準入力から与えられる。

X

出力

次にご褒美をもらうために、追加で集める必要のあるコインの枚数を出力せよ。


入力例 1

140

出力例 1

60

次にご褒美がもらえるのは、コインを累計で 200 枚集めたときなので、あと 200-140=60 枚のコインを集める必要があります。


入力例 2

1000

出力例 2

100

次にご褒美がもらえるのは、コインを累計で 1100 枚集めたときです。

Score : 100 points

Problem Statement

Takahashi is playing a game.

In this game, each time the number of coins you have collected so far becomes a multiple of 100, you get a prize.

Takahashi has collected X coins so far. How many more coins does he need to collect before he gets the next prize? (If X is a multiple of 100, we assume that he has already got the prize for collecting X coins in total.)

Constraints

  • 0 \leq X \leq 10^5

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of additional coins that he needs to collect before he gets the next prize.


Sample Input 1

140

Sample Output 1

60

He gets the next prize when he has collected 200 coins in total. To get it, he needs to collect 60 more coins.


Sample Input 2

1000

Sample Output 2

100

He gets the next prize when he has collected 1100 coins in total.

B - uNrEaDaBlE sTrInG

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

先頭から奇数番目の文字が全て英小文字であり、かつ、先頭から偶数番目の文字が全て英大文字であるような文字列を 読みにくい文字列 と呼びます。

文字列 S が読みにくい文字列かどうか判定してください。

制約

  • S は英大文字及び英小文字のみからなる
  • S の長さは 1 以上 1000 以下

入力

入力は以下の形式で標準入力から与えられる。

S

出力

S が読みにくい文字列なら Yes、読みにくい文字列でないなら No を出力せよ。


入力例 1

dIfFiCuLt

出力例 1

Yes

先頭から奇数番目の文字が全て英小文字であり、かつ、先頭から偶数番目の文字が全て英大文字であるので、読みにくい文字列です。


入力例 2

eASY

出力例 2

No

先頭から 3 番目の文字が英小文字ではないので、読みにくい文字列ではありません。


入力例 3

a

出力例 3

Yes

Score : 200 points

Problem Statement

We call a string hard-to-read when its odd-positioned (1-st, 3-rd, 5-th, ... from the beginning) characters are all lowercase English letters and its even-positioned characters (2-nd, 4-th, 6-th, ... from the beginning) are all uppercase English letters.

Determine whether a string S is hard-to-read.

Constraints

  • S consists of uppercase and lowercase English letters.
  • The length of S is between 1 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard-to-read, print Yes; otherwise, print No.


Sample Input 1

dIfFiCuLt

Sample Output 1

Yes

The odd-positioned characters are all lowercase and the even-positioned characters are all uppercase, so it is hard-to-read.


Sample Input 2

eASY

Sample Output 2

No

The 3-rd character is not lowercase, so it is not hard-to-read.


Sample Input 3

a

Sample Output 3

Yes
C - Kaprekar Number

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

0 以上の整数 x に対して、g_1(x), g_2(x), f(x) を次のように定めます。

  • g_1(x)= x を十進法で表したときの各桁の数字を大きい順に並び替えてできる整数
  • g_2(x)= x を十進法で表したときの各桁の数字を小さい順に並び替えてできる整数
  • f(x)=g_1(x)-g_2(x)

例えば g_1(314)=431, g_2(3021)=123, f(271)=721-127=594 です。先頭の余分な 0 は無視されることに注意してください。

整数 N,K が与えられるので、a_0=N, a_{i+1}=f(a_i)\ (i\geq 0) で定まる数列の a_K を求めてください。

制約

  • 0 \leq N \leq 10^9
  • 0 \leq K \leq 10^5
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N K

出力

a_K を出力せよ。


入力例 1

314 2

出力例 1

693
  • a_0=314
  • a_1=f(314)=431-134=297
  • a_2=f(297)=972-279=693

です。


入力例 2

1000000000 100

出力例 2

0
  • a_0=1000000000
  • a_1=f(1000000000)=1000000000-1=999999999
  • a_2=f(999999999)=999999999-999999999=0
  • a_3=f(0)=0-0=0
  • \vdots

となります。


入力例 3

6174 100000

出力例 3

6174

Score : 300 points

Problem Statement

For an integer x not less than 0, we define g_1(x), g_2(x), f(x) as follows:

  • g_1(x)= the integer obtained by rearranging the digits in the decimal notation of x in descending order
  • g_2(x)= the integer obtained by rearranging the digits in the decimal notation of x in ascending order
  • f(x)=g_1(x)-g_2(x)

For example, we have g_1(314)=431, g_2(3021)=123, f(271)=721-127=594. Note that the leading zeros are ignored.

Given integers N, K, find a_K in the sequence defined by a_0=N, a_{i+1}=f(a_i)\ (i\geq 0).

Constraints

  • 0 \leq N \leq 10^9
  • 0 \leq K \leq 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print a_K.


Sample Input 1

314 2

Sample Output 1

693

We have:

  • a_0=314
  • a_1=f(314)=431-134=297
  • a_2=f(297)=972-279=693

Sample Input 2

1000000000 100

Sample Output 2

0

We have:

  • a_0=1000000000
  • a_1=f(1000000000)=1000000000-1=999999999
  • a_2=f(999999999)=999999999-999999999=0
  • a_3=f(0)=0-0=0
  • \vdots

Sample Input 3

6174 100000

Sample Output 3

6174
D - Base n

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

09 からなる文字列 X と、整数 M が与えられます。

X に含まれる最も大きい数字を d とします。

d+1 以上の整数 n を選んで Xn 進法表記の数とみなして得られる値のうち、M 以下であるようなものは何種類あるでしょうか?

制約

  • X09 のみからなる
  • X の長さは 1 以上 60 以下
  • X の先頭の文字は 0 ではない
  • 1 \leq M \leq 10^{18}

入力

入力は以下の形式で標準入力から与えられる。

X
M

出力

答えを出力せよ。


入力例 1

22
10

出力例 1

2

X に含まれる最も大きい数字は 2 です。

  • X3 進法表記とみなして得られる値は 8 です。
  • X4 進法表記とみなして得られる値は 10 です。

得られる値のうち 10 以下のものはこの 2 つのみです。


入力例 2

999
1500

出力例 2

3

X に含まれる最も大きい数字は 9 です。

  • X10 進法表記とみなして得られる値は 999 です。
  • X11 進法表記とみなして得られる値は 1197 です。
  • X12 進法表記とみなして得られる値は 1413 です。

得られる値のうち 1500 以下のものはこの 3 つのみです。


入力例 3

100000000000000000000000000000000000000000000000000000000000
1000000000000000000

出力例 3

1

X2 進法表記とみなして得られる 576460752303423488 が、唯一の 1000000000000000000 以下の得られる数です。

Score : 400 points

Problem Statement

Given are a string X consisting of 0 through 9, and an integer M.

Let d be the greatest digit in X.

How many different integers not greater than M can be obtained by choosing an integer n not less than d+1 and seeing X as a base-n number?

Constraints

  • X consists of 0 through 9.
  • The length of X is between 1 and 60 (inclusive).
  • X does not begin with a 0.
  • 1 \leq M \leq 10^{18}

Input

Input is given from Standard Input in the following format:

X
M

Output

Print the answer.


Sample Input 1

22
10

Sample Output 1

2

The greatest digit in X is 2.

  • By seeing X as a base-3 number, we get 8.
  • By seeing X as a base-4 number, we get 10.

These two values are the only ones that we can obtain and are not greater than 10.


Sample Input 2

999
1500

Sample Output 2

3

The greatest digit in X is 9.

  • By seeing X as a base-10 number, we get 999.
  • By seeing X as a base-11 number, we get 1197.
  • By seeing X as a base-12 number, we get 1413.

These three values are the only ones that we can obtain and are not greater than 1500.


Sample Input 3

100000000000000000000000000000000000000000000000000000000000
1000000000000000000

Sample Output 3

1

By seeing X as a base-2 number, we get 576460752303423488, which is the only value that we can obtain and are not greater than 1000000000000000000.

E - Train

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

AtCoder国には 1 から N の番号がついた N 個の都市と、1 から M の番号がついた M 本の鉄道があります。

鉄道 i は都市 A_i と都市 B_i の間を結んでおり、時刻が K_i の倍数になる毎に、双方の都市からそれぞれ他方の都市への列車が発車します。この列車は出発から到着までに T_i の時間がかかります。

あなたはいま都市 X にいます。時刻 0 またはそれ以降に都市 X を発車する列車に乗って移動を開始するとき、都市 Y には最速でいつたどり着けるか求めてください。都市 Y にたどり着くことが出来ない場合はそのことを報告してください。
ただし、乗り換えにかかる時間は無視できるため、どの都市においても、あなたの乗っている列車の到着時刻と同時に発車する別の列車に乗り換えることが可能であるとします。

制約

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq X,Y \leq N
  • X \neq Y
  • 1 \leq A_i,B_i \leq N
  • A_i \neq B_i
  • 1 \leq T_i \leq 10^9
  • 1 \leq K_i \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M X Y
A_1 B_1 T_1 K_1
\vdots
A_M B_M T_M K_M

出力

都市 Y にたどり着くことができる最も早い時刻を出力せよ。ただし、都市 Y にたどり着くことが出来ない場合はかわりに -1 と出力せよ。


入力例 1

3 2 1 3
1 2 2 3
2 3 3 4

出力例 1

7

まず、時刻 0 に鉄道 1 に乗って、都市 1 から都市 2 へ移動します。都市 2 には時刻 2 に到着します。

その後、時刻 4 に鉄道 2 に乗って、都市 2 から都市 3 へ移動します。都市 3 には時刻 7 に到着します。

これより早く都市 3 に着く方法はありません。


入力例 2

3 2 3 1
1 2 2 3
2 3 3 4

出力例 2

5

まず、時刻 0 に鉄道 2 に乗って、都市 3 から都市 2 へ移動します。都市 2 には時刻 3 に到着します。

その後、時刻 3 に鉄道 1 に乗って、都市 2 から都市 1 へ移動します。都市 1 には時刻 5 に到着します。


入力例 3

3 0 3 1

出力例 3

-1

入力例 4

9 14 6 7
3 1 4 1
5 9 2 6
5 3 5 8
9 7 9 3
2 3 8 4
6 2 6 4
3 8 3 2
7 9 5 2
8 4 1 9
7 1 6 9
3 9 9 3
7 5 1 5
8 2 9 7
4 9 4 4

出力例 4

26

Score : 500 points

Problem Statement

In the Republic of AtCoder, there are N cities numbered 1 through N and M railroads numbered 1 through M.

Railroad i connects City A_i and City B_i bidirectionally. At time 0, K_i, and all subsequent multiples of K_i, a train departs from each of these cities and head to the other city. The time each of these trains takes to reach the destination is T_i.

You are now at City X. Find the earliest time you can reach City Y when you start the journey by taking a train that departs City X not earlier than time 0. If City Y is unreachable, report that fact.
The time it takes to transfer is ignorable. That is, at every city, you can transfer to a train that departs at the exact time your train arrives at that city.

Constraints

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq X,Y \leq N
  • X \neq Y
  • 1 \leq A_i,B_i \leq N
  • A_i \neq B_i
  • 1 \leq T_i \leq 10^9
  • 1 \leq K_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X Y
A_1 B_1 T_1 K_1
\vdots
A_M B_M T_M K_M

Output

Print the earliest possible time you can reach City Y. If City Y is unreachable, print -1 instead.


Sample Input 1

3 2 1 3
1 2 2 3
2 3 3 4

Sample Output 1

7

First, you take Railroad 1 at time 0 and go from City 1 to City 2. You arrive at City 2 at time 2.

Then, you take Railroad 2 at time 4 and go from City 2 to City 3. You arrive at City 3 at time 7.

There is no way to reach City 3 earlier.


Sample Input 2

3 2 3 1
1 2 2 3
2 3 3 4

Sample Output 2

5

First, you take Railroad 2 at time 0 and go from City 3 to City 2. You arrive at City 2 at time 3.

Then, you take Railroad 1 at time 3 and go from City 2 to City 1. You arrive at City 1 at time 5.


Sample Input 3

3 0 3 1

Sample Output 3

-1

Sample Input 4

9 14 6 7
3 1 4 1
5 9 2 6
5 3 5 8
9 7 9 3
2 3 8 4
6 2 6 4
3 8 3 2
7 9 5 2
8 4 1 9
7 1 6 9
3 9 9 3
7 5 1 5
8 2 9 7
4 9 4 4

Sample Output 4

26
F - Potion

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 種類の素材があり、素材 i には A_i の魔力があります。

魔法使いの高橋君は、この中から 1 種類以上を選んで合成し、ポーションを作ろうとしています。

k 種類の素材を合成して出来たポーションの魔力は、合成した直後には素材の魔力の合計であり、時間が 1 経過するごとに k 増加します。 魔力の増加は連続的ではなく離散的であることに注意してください。

高橋君が時刻 01 度だけ素材の合成を行うとき、魔力がちょうど X のポーションは、最速でいつ手に入りますか?

なお、制約下で魔力がちょうど X のポーションを作れることが証明されます。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^7
  • 10^9 \leq X \leq 10^{18}
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N X
A_1 \ldots A_N

出力

魔力がちょうど X のポーションを手に入れることができる最も早い時刻を出力せよ。


入力例 1

3 9999999999
3 6 8

出力例 1

4999999994

素材 1 と素材 3 を合成して出来たポーションの魔力は、時刻 03+8=11 であり、時間が 1 経過するごとに 2 増加するので、時刻 49999999949999999999 になります。これが最速です。

素材 1,2,3 全てを合成して出来たポーションの魔力は、時刻 33333333279999999998、時刻 333333332810000000001 となり、ちょうど 9999999999 にはなりません。


入力例 2

1 1000000000000000000
1

出力例 2

999999999999999999

Score : 600 points

Problem Statement

There are N kinds of materials. Material i has a magic power of A_i.

Takahashi, the magician, wants to make a potion by choosing one or more kinds from these materials and mixing them.

At the moment when he makes a potion by mixing k kinds of materials, the magic power of the potion is the sum of the materials used. Then, every second, its magic power increases by k. Note that the increase of the magic power is a discrete - not continuous - process.

Takahashi will mix materials just once, at time 0. What is the earliest time he can get a potion with a magic power of exactly X?

Under the constraints, it can be proved that it is possible to make a potion with a magic power of exactly X.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^7
  • 10^9 \leq X \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
A_1 \ldots A_N

Output

Print the earliest time he can get a potion with a magic power of exactly X.


Sample Input 1

3 9999999999
3 6 8

Sample Output 1

4999999994

The potion made by mixing Material 1 and Material 3 has a magic power of 3+8=11 at time 0, and it increases by 2 every second, so it will be 9999999999 at time 4999999994, which is the earliest possible time.

The potion made by mixing all the materials 1, 2, 3 will have a magic power of 9999999998 at time 3333333327 and 10000000001 at time 3333333328, so it will never be exactly 9999999999.


Sample Input 2

1 1000000000000000000
1

Sample Output 2

999999999999999999