C - Piano

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

無限に長いピアノの鍵盤があります。 この鍵盤内の連続する区間であって、白鍵 W 個と黒鍵 B 個からなるものは存在しますか?

文字列 wbwbwwbwbwbw を無限に繰り返してできる文字列を S とおきます。

S の部分文字列であって、W 個の wB 個の b からなるものは存在しますか?

S の部分文字列とは S の部分文字列とは、ある 2 つの正整数 l,r\ (l\leq r) に対して、Sl 文字目、l+1 文字目、\dotsr 文字目をこの順に繋げてできる文字列のことをいいます。

制約

  • W,B は整数
  • 0\leq W,B \leq 100
  • W+B \geq 1

入力

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

W B

出力

S の部分文字列であって、W 個の wB 個の b からなるものが存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

3 2

出力例 1

Yes

S の最初の 15 文字は wbwbwwbwbwbwwbw であり、S11 文字目から 15 文字目までを取り出してできる文字列 bwwbw3 個の w2 個の b からなる部分文字列の一例です。


入力例 2

3 0

出力例 2

No

3 個の w0 個の b からなる文字列は www のみですが、これは S の部分文字列ではありません。


入力例 3

92 66

出力例 3

Yes

Score: 200 points

Problem Statement

There is an infinitely long piano keyboard. Is there a continuous segment within this keyboard that consists of W white keys and B black keys?

Let S be the string formed by infinitely repeating the string wbwbwwbwbwbw.

Is there a substring of S that consists of W occurrences of w and B occurrences of b?

What is a substring of S? A substring of S is a string that can be formed by concatenating the l-th, (l+1)-th, \dots, r-th characters of S in this order for some two positive integers l and r (l\leq r).

Constraints

  • W and B are integers.
  • 0\leq W,B \leq 100
  • W+B \geq 1

Input

The input is given from Standard Input in the following format:

W B

Output

If there is a substring of S that consists of W occurrences of w and B occurrences of b, print Yes; otherwise, print No.


Sample Input 1

3 2

Sample Output 1

Yes

The first 15 characters of S are wbwbwwbwbwbwwbw. You can take the 11-th through 15-th characters to form the string bwwbw, which is a substring consisting of three occurrences of w and two occurrences of b.


Sample Input 2

3 0

Sample Output 2

No

The only string consisting of three occurrences of w and zero occurrences of b is www, which is not a substring of S.


Sample Input 3

92 66

Sample Output 3

Yes
D - Trick Taking

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

プレイヤー 1 、プレイヤー 2\ldots 、プレイヤー N番号がつけられた N 人のプレイヤーがカードゲームで対戦します。
各プレイヤーはカードを 1 枚場に出します。

各カードは2 つの属性を持ち、どちらの属性も正整数で表されます。
i = 1, 2, \ldots, N について、プレイヤー i が場に出したカードの色は C_i であり、値は R_i です。 R_1, R_2, \ldots, R_N はすべて異なります。

N 人のプレイヤーの中から 1 人の勝者を下記の方法で決めます。

  • 色が T であるカードが 1 枚以上場に出された場合、色が T であるカードのうち値が最大のものを出したプレイヤーが勝者である。
  • 色が T であるカードが場に 1 枚も出されなかった場合、プレイヤー 1 が出したカードと同じ色のカードのうち値が最大のものを出したプレイヤーが勝者である。(プレイヤー 1 自身も勝者となり得ることに注意してください。)

勝者となるプレイヤーの番号を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C_i \leq 10^9
  • 1 \leq R_i \leq 10^9
  • i \neq j \implies R_i \neq R_j
  • 入力はすべて整数

入力

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

N T
C_1 C_2 \ldots C_N
R_1 R_2 \ldots R_N

出力

答えを出力せよ。


入力例 1

4 2
1 2 1 2
6 3 4 5

出力例 1

4

色が 2 であるカードが 1 枚以上場に出されています。 よって、色が 2 であるカードのうち値が最大の 5 のカードを出した、プレイヤー 4 が勝者です。


入力例 2

4 2
1 3 1 4
6 3 4 5

出力例 2

1

色が 2 であるカードが 1 枚も場に出されていません。 よって、プレイヤー 1 が出したカードの色と同じ色(すなわち色 1 )のカードのうち値が最大の 6 のカードを出した、プレイヤー 1 が勝者です。


入力例 3

2 1000000000
1000000000 1
1 1000000000

出力例 3

1

Score : 200 points

Problem Statement

N players with ID numbers 1, 2, \ldots, N are playing a card game.
Each player plays one card.

Each card has two parameters: color and rank, both of which are represented by positive integers.
For i = 1, 2, \ldots, N, the card played by player i has a color C_i and a rank R_i. All of R_1, R_2, \ldots, R_N are different.

Among the N players, one winner is decided as follows.

  • If one or more cards with the color T are played, the player who has played the card with the greatest rank among those cards is the winner.
  • If no card with the color T is played, the player who has played the card with the greatest rank among the cards with the color of the card played by player 1 is the winner. (Note that player 1 may win.)

Print the ID number of the winner.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq T \leq 10^9
  • 1 \leq C_i \leq 10^9
  • 1 \leq R_i \leq 10^9
  • i \neq j \implies R_i \neq R_j
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N T
C_1 C_2 \ldots C_N
R_1 R_2 \ldots R_N

Output

Print the answer.


Sample Input 1

4 2
1 2 1 2
6 3 4 5

Sample Output 1

4

Cards with the color 2 are played. Thus, the winner is player 4, who has played the card with the greatest rank, 5, among those cards.


Sample Input 2

4 2
1 3 1 4
6 3 4 5

Sample Output 2

1

No card with the color 2 is played. Thus, the winner is player 1, who has played the card with the greatest rank, 6, among the cards with the color of the card played by player 1 (color 1).


Sample Input 3

2 1000000000
1000000000 1
1 1000000000

Sample Output 3

1
E - Approximate Equalization 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

整数列 A=(A_1,A_2,\dots,A_N) があります。 あなたは次の操作を好きな回数(0 回でもよい)行うことができます。

  • 1\leq i,j \leq N を満たす整数 i,j を選ぶ。A_i1 減らし、A_j1 増やす。

A の最小値と最大値の差を 1 以下にするために必要な最小の操作回数を求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

4
4 7 3 7

出力例 1

3

以下のように 3 回の操作を行うことで、A の最小値と最大値の差を 1 以下にすることができます。

  • i=2,j=3 として操作を行う。A=(4,6,4,7) になる。
  • i=4,j=1 として操作を行う。A=(5,6,4,6) になる。
  • i=4,j=3 として操作を行う。A=(5,6,5,5) になる。

3 回未満の操作で A の最小値と最大値の差を 1 以下にすることはできません。よって答えは 3 です。


入力例 2

1
313

出力例 2

0

入力例 3

10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993

出力例 3

2499999974

Score : 400 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N). You can perform the following operation any number of times (possibly zero).

  • Choose integers i and j with 1\leq i,j \leq N. Decrease A_i by one and increase A_j by one.

Find the minimum number of operations required to make the difference between the minimum and maximum values of A at most one.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

4
4 7 3 7

Sample Output 1

3

By the following three operations, the difference between the minimum and maximum values of A becomes at most one.

  • Choose i=2 and j=3 to make A=(4,6,4,7).
  • Choose i=4 and j=1 to make A=(5,6,4,6).
  • Choose i=4 and j=3 to make A=(5,6,5,5).

You cannot make the difference between maximum and minimum values of A at most one by less than three operations, so the answer is 3.


Sample Input 2

1
313

Sample Output 2

0

Sample Input 3

10
999999997 999999999 4 3 2 4 999999990 8 999999991 999999993

Sample Output 3

2499999974
F - AtCoder Cards

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder社ではカードを使った 1 人ゲームが流行っています。
ゲームで使う各カードには、英小文字 1 文字または @ の文字が書かれており、いずれのカードも十分多く存在します。
ゲームは以下の手順で行います。

  1. カードを同じ枚数ずつ 2 列に並べる。
  2. @ のカードを、それぞれ a, t, c, o, d, e, r のいずれかのカードと置き換える。
  3. 2 つの列が一致していれば勝ち。そうでなければ負け。

このゲームに勝ちたいあなたは、次のようなイカサマをすることにしました。

  • 手順 1 以降の好きなタイミングで、列内のカードを自由に並び替えてよい。

手順 1 で並べられた 2 つの列を表す 2 つの文字列 S,T が与えられるので、イカサマをしてもよいときゲームに勝てるか判定してください。

制約

  • S,T は英小文字と @ からなる
  • S,T の長さは等しく 1 以上 2\times 10^5 以下

入力

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

S
T

出力

イカサマをしてもよいとき、ゲームに勝てるなら Yes、勝てないなら No と出力せよ。


入力例 1

ch@ku@ai
choku@@i

出力例 1

Yes

@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。


入力例 2

ch@kud@i
akidu@ho

出力例 2

Yes

イカサマをし、@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。


入力例 3

aoki
@ok@

出力例 3

No

イカサマをしても勝つことはできません。


入力例 4

aa
bb

出力例 4

No

Score : 300 points

Problem Statement

A single-player card game is popular in AtCoder Inc. Each card in the game has a lowercase English letter or the symbol @ written on it. There is plenty number of cards for each kind. The game goes as follows.

  1. Arrange the same number of cards in two rows.
  2. Replace each card with @ with one of the following cards: a, t, c, o, d, e, r.
  3. If the two rows of cards coincide, you win. Otherwise, you lose.

To win this game, you will do the following cheat.

  • Freely rearrange the cards within a row whenever you want after step 1.

You are given two strings S and T, representing the two rows you have after step 1. Determine whether it is possible to win with cheating allowed.

Constraints

  • S and T consist of lowercase English letters and @.
  • The lengths of S and T are equal and between 1 and 2\times 10^5, inclusive.

Input

The input is given from Standard Input in the following format:

S
T

Output

If it is possible to win with cheating allowed, print Yes; otherwise, print No.


Sample Input 1

ch@ku@ai
choku@@i

Sample Output 1

Yes

You can replace the @s so that both rows become chokudai.


Sample Input 2

ch@kud@i
akidu@ho

Sample Output 2

Yes

You can cheat and replace the @s so that both rows become chokudai.


Sample Input 3

aoki
@ok@

Sample Output 3

No

You cannot win even with cheating.


Sample Input 4

aa
bb

Sample Output 4

No
G - Multiply and Rotate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

正の整数 a があります。また、黒板に 1 個の数が 10 進表記で書かれています。
黒板に現在書かれている数を x としたとき、高橋君は次のいずれかの操作を行い、黒板に書かれている数を変化させることができます。

  • x を消し、 xa 倍した数を 10 進表記で新たに書きこむ。
  • x を文字列とみなして、列の末尾の数字を文字列の先頭に移動させる。
    ただし、この操作は x \geq 10 かつ x10 で割り切れないときにしか行えない。

たとえば a = 2, x = 123 であるとき、高橋君は次のいずれかの操作を行うことができます。

  • x を消して、 x \times a = 123 \times 2 = 246 を新たに書きこむ。
  • x を文字列とみなして、123 の末尾の数字である 3 を先頭に移動させる。黒板に書かれている数は 123 から 312 に変化する。

はじめ、黒板には 1 が書かれています。書かれている数を N に変化させるには最小で何回の操作が必要ですか?ただし、どのように操作しても書かれている数を N に変化させられない場合は -1 を出力してください。

制約

  • 2 \leq a \lt 10^6
  • 2 \leq N \lt 10^6
  • 入力はすべて整数である。

入力

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

a N

出力

答えを出力せよ。


入力例 1

3 72

出力例 1

4

以下に説明する操作を行うことで、 黒板に書かれている数を 4 回で 1 から 72 に変化させることができます。

  • 1 つ目の操作を行う。黒板に書かれている数は 1 \to 3 に変わる。
  • 1 つ目の操作を行う。黒板に書かれている数は 3 \to 9 に変わる。
  • 1 つ目の操作を行う。黒板に書かれている数は 9 \to 27 に変わる。
  • 2 つ目の操作を行う。黒板に書かれている数は 27 \to 72 に変わる。

3 回以下の操作で 72 に変化させることはできないため、答えは 4 になります。


入力例 2

2 5

出力例 2

-1

どのように操作しても黒板に書かれている数を 5 に変化させることはできません。


入力例 3

2 611

出力例 3

12

適切に操作を選ぶことで、 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 61112 回の操作で黒板に書かれている数を 611 に変化させることができ、これが最小です。


入力例 4

2 767090

出力例 4

111

Score : 400 points

Problem Statement

We have a positive integer a. Additionally, there is a blackboard with a number written in base 10.
Let x be the number on the blackboard. Takahashi can do the operations below to change this number.

  • Erase x and write x multiplied by a, in base 10.
  • See x as a string and move the rightmost digit to the beginning.
    This operation can only be done when x \geq 10 and x is not divisible by 10.

For example, when a = 2, x = 123, Takahashi can do one of the following.

  • Erase x and write x \times a = 123 \times 2 = 246.
  • See x as a string and move the rightmost digit 3 of 123 to the beginning, changing the number from 123 to 312.

The number on the blackboard is initially 1. What is the minimum number of operations needed to change the number on the blackboard to N? If there is no way to change the number to N, print -1.

Constraints

  • 2 \leq a \lt 10^6
  • 2 \leq N \lt 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a N

Output

Print the answer.


Sample Input 1

3 72

Sample Output 1

4

We can change the number on the blackboard from 1 to 72 in four operations, as follows.

  • Do the operation of the first type: 1 \to 3.
  • Do the operation of the first type: 3 \to 9.
  • Do the operation of the first type: 9 \to 27.
  • Do the operation of the second type: 27 \to 72.

It is impossible to reach 72 in three or fewer operations, so the answer is 4.


Sample Input 2

2 5

Sample Output 2

-1

It is impossible to change the number on the blackboard to 5.


Sample Input 3

2 611

Sample Output 3

12

There is a way to change the number on the blackboard to 611 in 12 operations: 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 611, which is the minimum possible.


Sample Input 4

2 767090

Sample Output 4

111