E - Swiss-System Tournament

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

1 から 2N の番号がついた 2N 人でじゃんけん大会をします。

大会は M ラウンドからなり、各ラウンドは、全ての人が 1 度ずつ参加するような 11N 試合からなります。

i=0,1,\ldots,M について、i ラウンド目の終了時点での順位を次のように決めます。

  • i ラウンド目までの勝数が多い方が上位
  • i ラウンド目までの勝数が同じときは、番号が小さい方が上位

また、i=1,\ldots,M について、i ラウンド目の各試合の組み合わせを次のように決めます。

  • k=1,2,\ldots,N について、i-1 ラウンド目終了時点の順位が 2k-1 位の人と 2k 位の人が試合をする

各試合では、対戦する 2 人がそれぞれ 1 度だけ手を出し、勝ち・負け・引き分けのいずれかの結果が発生します。

未来予知ができる高橋君は、人 ij ラウンド目の試合で出す手が A_{i,j} であることを知っています。
A_{i,j}G, C, P のいずれかであり、それぞれグー、チョキ、パーを表します。

M ラウンド目終了時点の順位を求めてください。

じゃんけんのルール じゃんけんの結果は、2 人の出した手に応じて次のように決まります。
  • 一方がグーで他方がチョキのとき、グーを出した人が勝ち、チョキを出した人は負け
  • 一方がチョキで他方がパーのとき、チョキを出した人が勝ち、パーを出した人は負け
  • 一方がパーで他方がグーのとき、パーを出した人が勝ち、グーを出した人は負け
  • 両者が同じ手を出したとき、引き分け

制約

  • 1 \leq N \leq 50
  • 1 \leq M \leq 100
  • A_{i,j}G, C, P のいずれか

入力

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

N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}

出力

2N 行出力せよ。

i 行目には、M ラウンド目終了時点での順位が i 位である人の番号を出力せよ。


入力例 1

2 3
GCP
PPP
CCC
PPC

出力例 1

3
1
2
4

1 ラウンド目では人 1234 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2314 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 1 が勝ちます。
3 ラウンド目では人 3124 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 4 が勝ちます。
よって最終的な順位は、上位から順に人 3,1,2,4 となります。


入力例 2

2 2
GC
PG
CG
PP

出力例 2

1
2
3
4

1 ラウンド目では人 1234 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2314 がそれぞれ試合をし、前者の試合は引き分け、後者の試合は人 1 が勝ちます。
よって最終的な順位は、上位から順に人 1,2,3,4 となります。

Score : 300 points

Problem Statement

2N players, with ID numbers 1 through 2N, will participate in a rock-scissors-paper contest.

The contest has M rounds. Each round has N one-on-one matches, where each player plays in one of them.

For each i=0, 1, \ldots, M, the players' ranks at the end of the i-th round are determined as follows.

  • A player with more wins in the first i rounds ranks higher.
  • Ties are broken by ID numbers: a player with a smaller ID number ranks higher.

Additionally, for each i=1, \ldots, M, the matches in the i-th round are arranged as follows.

  • For each k=1, 2, \ldots, N, a match is played between the players who rank (2k-1)-th and 2k-th at the end of the (i-1)-th round.

In each match, the two players play a hand just once, resulting in one player's win and the other's loss, or a draw.

Takahashi, who can foresee the future, knows that Player i will play A_{i, j} in their match in the j-th round, where A_{i,j} is G, C, or P.
Here, G stands for rock, C stands for scissors, and P stands for paper. (These derive from Japanese.)

Find the players' ranks at the end of the M-th round.

Rules of rock-scissors-paper The result of a rock-scissors-paper match is determined as follows, based on the hands played by the two players.
  • If one player plays rock (G) and the other plays scissors (C), the player playing rock (G) wins.
  • If one player plays scissors (C) and the other plays paper (P), the player playing scissors (C) wins.
  • If one player plays paper (P) and the other plays rock (G), the player playing paper (P) wins.
  • If the players play the same hand, the match is drawn.

Constraints

  • 1 \leq N \leq 50
  • 1 \leq M \leq 100
  • A_{i,j} is G, C, or P.

Input

Input is given from Standard Input in the following format:

N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}

Output

Print 2N lines.

The i-th line should contain the ID number of the player who ranks i-th at the end of the M-th round.


Sample Input 1

2 3
GCP
PPP
CCC
PPC

Sample Output 1

3
1
2
4

In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. Player 3 wins the former, and Player 1 wins the latter.
In the third round, matches are played between Players 3 and 1, and between Players 2 and 4. Player 3 wins the former, and Player 4 wins the latter.
Therefore, in the end, the players are ranked in the following order: 3,1,2,4, from highest to lowest.


Sample Input 2

2 2
GC
PG
CG
PP

Sample Output 2

1
2
3
4

In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. The former is drawn, and Player 1 wins the latter.
Therefore, in the end, the players are ranked in the following order: 1,2,3,4, from highest to lowest.

F - Centers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。

i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,Nf(i) の昇順に並べ替えてください。

f(i) の定義は厳密には以下の通りです。

  • A_j = i を満たす jj=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。

制約

  • 1\leq N \leq 10^5
  • 1 \leq A_j \leq N
  • i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_{3N}

出力

1,2,\dots,Nf(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。


入力例 1

3
1 1 3 2 3 2 2 3 1

出力例 1

1 3 2
  • A の中にある 1A_1,A_2,A_9 なので、f(1) = 2 です。
  • A の中にある 2A_4,A_6,A_7 なので、f(2) = 6 です。
  • A の中にある 3A_3,A_5,A_8 なので、f(3) = 5 です。

よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。


入力例 2

1
1 1 1

出力例 2

1

入力例 3

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

出力例 3

3 4 1 2

Score : 250 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.

For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).

Formally, f(i) is defined as follows.

  • Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.

Constraints

  • 1\leq N \leq 10^5
  • 1 \leq A_j \leq N
  • i occurs in A exactly three times, for each i=1,2,\dots,N.
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_{3N}

Output

Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.


Sample Input 1

3
1 1 3 2 3 2 2 3 1

Sample Output 1

1 3 2
  • 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
  • 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
  • 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.

Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.


Sample Input 2

1
1 1 1

Sample Output 2

1

Sample Input 3

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

Sample Output 3

3 4 1 2
G - Swapping Puzzle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

HW 列の 2 つのグリッド A, B が与えられます。

1 \leq i \leq H1 \leq j \leq W を満たす各整数の組 (i, j) について、 i 行目 j 列目にあるマスをマス (i, j) と呼ぶとき、 グリッド A の マス (i, j) には整数 A_{i, j} が、 グリッド B の マス (i, j) には整数 B_{i, j} がそれぞれ書かれています。

あなたは「下記の 2 つのうちのどちらか 1 つを行う」という操作を好きな回数( 0 回でもよい)だけ繰り返します。

  • 1 \leq i \leq H-1 を満たす整数 i を選び、グリッド A の i 行目と (i+1) 行目を入れ替える。
  • 1 \leq i \leq W-1 を満たす整数 i を選び、グリッド A の i 列目と (i+1) 列目を入れ替える。

上記の操作の繰り返しによって、グリッド A をグリッド B に一致させることが可能かどうかを判定してください。 さらに、一致させることが可能な場合は、そのために行う操作回数の最小値を出力してください。

ここで、グリッド A とグリッド B が一致しているとは、 1 \leq i \leq H1 \leq j \leq W を満たす全ての整数の組 (i, j) について、 グリッド A の マス (i, j) とグリッド B の マス (i, j) に書かれた整数が等しいこととします。

制約

  • 入力される値は全て整数
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

入力

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

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

出力

グリッド A をグリッド B に一致させることが不可能な場合は -1 を、 可能な場合はグリッド A をグリッド B に一致させるために行う操作回数の最小値を出力せよ。


入力例 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

出力例 1

3

初期状態のグリッド A の 4 列目と 5 列目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

続けて、グリッド A の 2 行目と 3 行目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

最後に、グリッド A の 2 列目と 3 列目を入れ替えると、グリッド A は下記の通りになり、グリッド B に一致します。

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

上に述べた 3 回の操作でグリッド A をグリッド B に一致させることができ、 これより少ない回数の操作でグリッド A をグリッド B に一致させることはできないため、 3 を出力します。


入力例 2

2 2
1 1
1 1
1 1
1 1000000000

出力例 2

-1

問題文中の操作をどのように行ってもグリッド A をグリッド B に一致させることは不可能であるため -1 を出力します。


入力例 3

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

出力例 3

0

グリッド A ははじめからグリッド B に一致しています。


入力例 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

出力例 4

20

Score : 425 points

Problem Statement

You are given two grids, A and B, each with H rows and W columns.

For each pair of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, let (i, j) denote the cell in the i-th row and j-th column. In grid A, cell (i, j) contains the integer A_{i, j}. In grid B, cell (i, j) contains the integer B_{i, j}.

You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:

  • Choose an integer i satisfying 1 \leq i \leq H-1 and swap the i-th and (i+1)-th rows in grid A.
  • Choose an integer i satisfying 1 \leq i \leq W-1 and swap the i-th and (i+1)-th columns in grid A.

Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.

Here, grid A is identical to grid B if and only if, for all pairs of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, the integer written in cell (i, j) of grid A is equal to the integer written in cell (i, j) of grid B.

Constraints

  • All input values are integers.
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

Input

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

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

Output

If it is impossible to make grid A identical to grid B, output -1. Otherwise, print the minimum number of operations required to make grid A identical to grid B.


Sample Input 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

Sample Output 1

3

Swapping the fourth and fifth columns of the initial grid A yields the following grid:

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

Then, swapping the second and third rows yields the following grid:

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

Finally, swapping the second and third columns yields the following grid, which is identical to grid B:

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print 3.


Sample Input 2

2 2
1 1
1 1
1 1
1 1000000000

Sample Output 2

-1

There is no way to perform the operation to make grid A match grid B, so print -1.


Sample Input 3

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

Sample Output 3

0

Grid A is already identical to grid B at the beginning.


Sample Input 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

Sample Output 4

20
H - Arithmetic Number

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

以下の条件を満たす正の整数 n を、 等差数 と呼びます。

  • (n を先頭に余計な 0 を付けずに 10 進法で表記した際、) n の上から i 桁目を d_i とする。このとき、 nk 桁の整数であったとすると、 (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) が成立する。
    • この条件は、「 数列 (d_1,d_2,\dots,d_k) が等差数列である」と言い換えることができる。
    • 但し、 n1 桁の整数である時、 n は等差数であるものとする。

たとえば、 234,369,86420,17,95,8,11,777 は等差数ですが、 751,919,2022,246810,2356 は等差数ではありません。

等差数のうち、 X 以上で最小のものを求めてください。

制約

  • X1 以上 10^{17} 以下の整数である

入力

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

X

出力

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


入力例 1

152

出力例 1

159

152 以上で最小の等差数は 159 です。


入力例 2

88

出力例 2

88

X 自身が等差数である場合もあります。


入力例 3

8989898989

出力例 3

9876543210

Score : 500 points

Problem Statement

Let us call a positive integer n that satisfies the following condition an arithmetic number.

  • Let d_i be the i-th digit of n from the top (when n is written in base 10 without unnecessary leading zeros.) Then, (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) holds, where k is the number of digits in n.
    • This condition can be rephrased into the sequence (d_1,d_2,\dots,d_k) being arithmetic.
    • If n is a 1-digit integer, it is assumed to be an arithmetic number.

For example, 234,369,86420,17,95,8,11,777 are arithmetic numbers, while 751,919,2022,246810,2356 are not.

Find the smallest arithmetic number not less than X.

Constraints

  • X is an integer between 1 and 10^{17} (inclusive).

Input

Input is given from Standard Input in the following format:

X

Output

Print the answer as an integer.


Sample Input 1

152

Sample Output 1

159

The smallest arithmetic number not less than 152 is 159.


Sample Input 2

88

Sample Output 2

88

X itself may be an arithmetic number.


Sample Input 3

8989898989

Sample Output 3

9876543210
I - Dividing Game

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

各要素が 2 以上である長さ N の正整数列 A = (A_1, A_2, \dots ,A_N) が与えられます。Anna と Bruno はこれらの整数を用いてゲームをします。二人は、 Anna を先手として交互に以下の操作を行います。

  • 整数 i \ (1 \leq i \leq N) を自由に選ぶ。A_i の正の約数であって A_i 自身でないものを自由に選び x とし、 A_ix で置き換える。

操作が行えなくなった方が負けで、負けなかった方が勝ちです。両者が勝ちを目指して最適な行動を取るとき、どちらが勝つか判定してください。

制約

  • 1 \leq N \leq 10^5
  • 2 \leq A_i \leq 10^5
  • 入力はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

Anna がゲームに勝つ場合 Anna と、 Bruno がゲームに勝つ場合 Bruno と出力せよ。


入力例 1

3
2 3 4

出力例 1

Anna

例えば、ゲームの進行として以下のようなものが考えられます。必ずしも両者が最適な行動を取った例とは限らないことに注意してください。

  • Anna が A_32 にする。
  • Bruno が A_11 にする。
  • Anna が A_21 にする。
  • Bruno が A_31 にする。
  • Anna のターンで操作ができないので Bruno の勝ちとなる。

実際には、このサンプルでは Anna が最適に行動すると常に Anna が勝つことができます。


入力例 2

4
2 3 4 6

出力例 2

Bruno

Score : 475 points

Problem Statement

You are given a sequence of N positive integers A = (A_1, A_2, \dots ,A_N), where each element is at least 2. Anna and Bruno play a game using these integers. They take turns, with Anna going first, performing the following operation.

  • Choose an integer i \ (1 \leq i \leq N) freely. Then, freely choose a positive divisor x of A_i that is not A_i itself, and replace A_i with x.

The player who cannot perform the operation loses, and the other player wins. Determine who wins assuming both players play optimally for victory.

Constraints

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

Input

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

N
A_1 A_2 \cdots A_N

Output

Print Anna if Anna wins the game, and Bruno if Bruno wins.


Sample Input 1

3
2 3 4

Sample Output 1

Anna

For example, the game might proceed as follows. Note that this example may not necessarily represent optimal play by both players:

  • Anna changes A_3 to 2.
  • Bruno changes A_1 to 1.
  • Anna changes A_2 to 1.
  • Bruno changes A_3 to 1.
  • Anna cannot operate on her turn, so Bruno wins.

Actually, for this sample, Anna always wins if she plays optimally.


Sample Input 2

4
2 3 4 6

Sample Output 2

Bruno