A - You should output ARC, though this is ABC.

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

整数 R,C22 列からなる行列 A が与えられるので、 A_{R,C} を出力してください。

制約

  • 入力は全て整数
  • 1 \le R,C \le 2
  • 0 \le A_{i,j} \le 100

入力

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

R C
A_{1,1} A_{1,2}
A_{2,1} A_{2,2}

出力

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


入力例 1

1 2
1 0
0 1

出力例 1

0

A_{1,2}=0 です。


入力例 2

2 2
1 2
3 4

出力例 2

4

A_{2,2}=4 です。


入力例 3

2 1
90 80
70 60

出力例 3

70

A_{2,1}=70 です。

Score : 100 points

Problem Statement

Given integers R, C, and a 2 \times 2 matrix A, print A_{R,C}.

Constraints

  • All values in input are integers.
  • 1 \le R,C \le 2
  • 0 \le A_{i,j} \le 100

Input

Input is given from Standard Input in the following format:

R C
A_{1,1} A_{1,2}
A_{2,1} A_{2,2}

Output

Print the answer as an integer.


Sample Input 1

1 2
1 0
0 1

Sample Output 1

0

We have A_{1,2}=0.


Sample Input 2

2 2
1 2
3 4

Sample Output 2

4

We have A_{2,2}=4.


Sample Input 3

2 1
90 80
70 60

Sample Output 3

70

We have A_{2,1}=70.

B - Swap Odd and Even

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

英小文字からなる長さが偶数の文字列 S が与えられます。S の長さを |S|Si 文字目を S_i で表します。

i = 1, 2, \ldots, \frac{|S|}{2} の順に以下の操作を行い、すべての操作を終えた後の S を出力してください。

  • S_{2i-1}S_{2i} を入れ替える

制約

  • S は英小文字からなる長さが偶数の文字列
  • S の長さは 100 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

abcdef

出力例 1

badcfe

操作を行う前は S = abcdef です。
i = 1 について操作を行うと、S_1S_2 が入れ替わるので S = bacdef になります。
i = 2 について操作を行うと、S_3S_4 が入れ替わるので S = badcef になります。
i = 3 について操作を行うと、S_5S_6 が入れ替わるので S = badcfe になります。
したがって、badcfe を出力します。


入力例 2

aaaa

出力例 2

aaaa

入力例 3

atcoderbeginnercontest

出力例 3

taocedbrgeniencrnoetts

Score : 100 points

Problem Statement

You are given a string S of even length consisting of lowercase English letters. Let |S| be the length of S, and S_i be the i-th character of S.

Perform the following operation for each i = 1, 2, \ldots, \frac{|S|}{2} in this order, and print the final S.

  • Swap S_{2i-1} and S_{2i}.

Constraints

  • S is a string of even length consisting of lowercase English letters.
  • The length of S is at most 100.

Input

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

S

Output

Print the answer.


Sample Input 1

abcdef

Sample Output 1

badcfe

Initially, S = abcdef.
Performing the operation for i = 1 swaps S_1 and S_2, making S = bacdef.
Performing the operation for i = 2 swaps S_3 and S_4, making S = badcef.
Performing the operation for i = 3 swaps S_5 and S_6, making S = badcfe.
Thus, badcfe should be printed.


Sample Input 2

aaaa

Sample Output 2

aaaa

Sample Input 3

atcoderbeginnercontest

Sample Output 3

taocedbrgeniencrnoetts
C - Everyone is Friends

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

1,2,\ldots,N の番号がついた N 人の人がいます。

M 回の舞踏会が行われました。 i (1\leq i \leq M) 回目の舞踏会には k_i 人が参加し、参加した人は人 x_{i,1},x_{i,2},\ldots,x_{i,k_i} でした。

どの二人も少なくとも 1 回同じ舞踏会に参加したか判定してください。

制約

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • 入力は全て整数

入力

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

出力

どの二人も少なくとも 1 回同じ舞踏会に参加した場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3 3
2 1 2
2 2 3
2 1 3

出力例 1

Yes

1 と人 2 は共に 1 回目の舞踏会に参加しています。

2 と人 3 は共に 2 回目の舞踏会に参加しています。

1 と人 3 は共に 3 回目の舞踏会に参加しています。

以上よりどの二人も少なくとも 1 回同じ舞踏会に参加したので、答えは Yes です。


入力例 2

4 2
3 1 2 4
3 2 3 4

出力例 2

No

1 と人 31 回も同じ舞踏会に参加していないので、答えは No です。

Score : 200 points

Problem Statement

There are N people numbered 1,2,\ldots,N.

M parties were held. k_i people attended the i-th (1\leq i \leq M) party, and they were People x_{i,1},x_{i,2},\ldots,x_{i,k_i}.

Determine if every two people attended the same party at least once.

Constraints

  • 2\leq N \leq 100
  • 1\leq M \leq 100
  • 2\leq k_i \leq N
  • 1\leq x_{i,1}<x_{i,2}<\ldots < x_{i,k_i}\leq N
  • All values in the input are integers.

Input

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

N M
k_1 x_{1,1} x_{1,2} \ldots x_{1,k_1}
\vdots
k_M x_{M,1} x_{M,2} \ldots x_{M,k_M}

Output

Print Yes if every two people attended the same party at least once; print No otherwise.


Sample Input 1

3 3
2 1 2
2 2 3
2 1 3

Sample Output 1

Yes

Both Person 1 and Person 2 attended the 1-st party.

Both Person 2 and Person 3 attended the 2-nd party.

Both Person 1 and Person 3 attended the 3-rd party.

Therefore, every two people attended the same party at least once, so the answer is Yes.


Sample Input 2

4 2
3 1 2 4
3 2 3 4

Sample Output 2

No

Person 1 and Person 3 did not attend the same party, so the answer is No.

D - Enlarged Checker Board

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

A 行、横 B 列のマスからなるタイルを縦 N 行、横 N 列に並べてできた、縦 (A\times N) 行、横 (B\times N) 列のマス目 X があります。
1\leq i,j \leq N について、上から i 行目、左から j 列目のタイルをタイル (i,j) とします。

X の各マスは以下のように塗られています。

  • 各タイルは白いタイルまたは黒いタイルである。
  • 白いタイルのすべてのマスは白で塗られ、黒いタイルのすべてのマスは黒で塗られている。
  • タイル (1,1) は白いタイルである。
  • 辺で隣接する 2 つのタイルは異なる色のタイルである。ただし、タイル (a,b) とタイル (c,d) が辺で隣接するとは、|a-c|+|b-d|=1 ( |x|x の絶対値とする)であることを言う。

マス目 X を出力の形式に従って出力してください。

制約

  • 1 \leq N,A,B \leq 10
  • 入力は全て整数

入力

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

N A B

出力

次の条件をみたす (A\times N) 個の文字列 S_1,\ldots,S_{A\times N} を改行区切りで出力せよ。

  • S_1,\ldots,S_{A\times N} はそれぞれ長さ (B\times N). または # からなる文字列である。
  • i,j (1 \leq i \leq A\times N,1 \leq j \leq B\times N) に対し、マス目 X の上から i 行目かつ左から j 列目のマスが白で塗られているならば S_ij 文字目は .であり、黒く塗られているならば # である。

入力例 1

4 3 2

出力例 1

..##..##
..##..##
..##..##
##..##..
##..##..
##..##..
..##..##
..##..##
..##..##
##..##..
##..##..
##..##..

入力例 2

5 1 5

出力例 2

.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....

入力例 3

4 4 1

出力例 3

.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.
.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.

入力例 4

1 4 4

出力例 4

....
....
....
....

Score : 200 points

Problem Statement

Tiles are aligned in N horizontal rows and N vertical columns. Each tile has a grid with A horizontal rows and B vertical columns. On the whole, the tiles form a grid X with (A\times N) horizontal rows and (B\times N) vertical columns.
For 1\leq i,j \leq N, Tile (i,j) denotes the tile at the i-th row from the top and the j-th column from the left.

Each square of X is painted as follows.

  • Each tile is either a white tile or a black tile.
  • Every square in a white tile is painted white; every square in a black tile is painted black.
  • Tile (1,1) is a white tile.
  • Two tiles sharing a side have different colors. Here, Tile (a,b) and Tile (c,d) are said to be sharing a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).

Print the grid X in the format specified in the Output section.

Constraints

  • 1 \leq N,A,B \leq 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print (A\times N) strings S_1,\ldots,S_{A\times N} that satisfy the following condition, with newlines in between.

  • Each of S_1,\ldots,S_{A\times N} is a string of length (B\times N) consisting of . and #.
  • For each i and j (1 \leq i \leq A\times N,1 \leq j \leq B\times N), the j-th character of S_i is . if the square at the i-th row from the top and j-th column from the left in grid X is painted white; the character is # if the square is painted black.

Sample Input 1

4 3 2

Sample Output 1

..##..##
..##..##
..##..##
##..##..
##..##..
##..##..
..##..##
..##..##
..##..##
##..##..
##..##..
##..##..

Sample Input 2

5 1 5

Sample Output 2

.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....
#####.....#####.....#####
.....#####.....#####.....

Sample Input 3

4 4 1

Sample Output 3

.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.
.#.#
.#.#
.#.#
.#.#
#.#.
#.#.
#.#.
#.#.

Sample Input 4

1 4 4

Sample Output 4

....
....
....
....
E - Min Difference

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

それぞれ N 個、M 個の正整数からなる 2 つの数列 A=(A_1,A_2, \ldots ,A_N)B=(B_1, \ldots ,B_M) が与えられます。

それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差の最小値、すなわち、 \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert を求めてください。

制約

  • 1 \leq N,M \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力は全て整数である。

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

答えを出力せよ。


入力例 1

2 2
1 6
4 9

出力例 1

2

それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差としてあり得るのは、 \lvert 1-4\rvert=3\lvert 1-9\rvert=8\lvert 6-4\rvert=2\lvert 6-9\rvert=34 つです。 この中で最小である 2 を出力します。


入力例 2

1 1
10
10

出力例 2

0

入力例 3

6 8
82 76 82 82 71 70
17 39 67 2 45 35 22 24

出力例 3

3

Score : 300 points

Problem Statement

You are given two sequences: A=(A_1,A_2, \ldots ,A_N) consisting of N positive integers, and B=(B_1, \ldots ,B_M) consisting of M positive integers.

Find the minimum difference of an element of A and an element of B, that is, \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert.

Constraints

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

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print the answer.


Sample Input 1

2 2
1 6
4 9

Sample Output 1

2

Here is the difference for each of the four pair of an element of A and an element of B: \lvert 1-4\rvert=3, \lvert 1-9\rvert=8, \lvert 6-4\rvert=2, and \lvert 6-9\rvert=3. We should print the minimum of these values, or 2.


Sample Input 2

1 1
10
10

Sample Output 2

0

Sample Input 3

6 8
82 76 82 82 71 70
17 39 67 2 45 35 22 24

Sample Output 3

3
F - T-shirts

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

AtCoder 社はロゴ入りの T シャツを販売しています。

高橋君の N 日間の予定が 0, 1, 2 のみからなる長さ N の文字列 S で与えられます。
具体的には、1\leq i\leq N をみたす整数 i について、

  • Si 文字目が 0 のとき、i 日目に何の予定も入っていません。
  • Si 文字目が 1 のとき、i 日目に高橋君は食事に行く予定があります。
  • Si 文字目が 2 のとき、i 日目に高橋君は競技プログラミングのイベントに行く予定が入っています。

高橋君は無地の T シャツを M 枚持っており、1 日目の直前の時点ですべて洗濯済みの状態となっています。
これに加えて、次の条件をみたすように行動できるように、高橋君は AtCoder のロゴ入りの T シャツを何枚か購入する事にしました。

  • 食事に行く日には、無地の T シャツ 1 枚またはロゴ入りの T シャツ 1 枚を着用する。
  • 競技プログラミングのイベントに行く日にはロゴ入りの T シャツ 1 枚を着用する。
  • 何の予定もない日には T シャツを着用しない。加えて、その時点で着用済みの T シャツを全て洗濯する。 洗濯した T シャツは翌日から着用できる。
  • 一度着用した T シャツは次に洗濯するまで着用できない。

N 日間のうち予定が入っている日すべてについて、条件をみたす T シャツを着用できるようにするために、高橋君は最低何枚のTシャツを購入する必要があるか求めてください。 新しく T シャツを購入する必要がないならば 0 を出力してください。
ただし、購入した T シャツも 1 日目の直前の時点ですべて洗濯済みの状態で存在するものとします。

制約

  • 1\leq M\leq N\leq 1000
  • S0, 1, 2 のみからなる長さ N の文字列
  • N,M は整数

入力

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

N M
S

出力

問題文の条件をみたすように行動するために 高橋君が購入する必要のある T シャツの枚数の最小値を出力せよ。
新しく購入する必要がないならば 0 を出力せよ。


入力例 1

6 1
112022

出力例 1

2

高橋君がロゴ入りの T シャツを 2 枚購入したとき、次のようにして高橋君は T シャツを着用することができます。

  • 1 日目、高橋君はロゴ入りの T シャツを着用して食事に行きます。
  • 2 日目、高橋君は無地の T シャツを着用して食事に行きます。
  • 3 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 4 日目、高橋君は予定がないため、着用した T シャツをすべて洗濯します。これにより、1,2,3 日目に着用した T シャツを再び着用することが可能になります。
  • 5 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 6 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。

高橋君がロゴ入りの T シャツを 1 枚以下しか購入しなかった場合には、 どのようにしても条件をみたすように T シャツを着用することができません。
よって、2 を出力します。


入力例 2

3 1
222

出力例 2

3

入力例 3

2 1
01

出力例 3

0

高橋君は新しく T シャツを購入する必要はありません。

Score : 300 points

Problem Statement

AtCoder Inc. sells T-shirts with its logo.

You are given Takahashi's schedule for N days as a string S of length N consisting of 0, 1, and 2.
Specifically, for an integer i satisfying 1\leq i\leq N,

  • if the i-th character of S is 0, he has no plan scheduled for the i-th day;
  • if the i-th character of S is 1, he plans to go out for a meal on the i-th day;
  • if the i-th character of S is 2, he plans to attend a competitive programming event on the i-th day.

Takahashi has M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.

  • On days he goes out for a meal, he will wear a plain or logo T-shirt.
  • On days he attends a competitive programming event, he will wear a logo T-shirt.
  • On days with no plans, he will not wear any T-shirts. Also, he will wash all T-shirts worn at that point. He can wear them again from the next day onwards.
  • Once he wears a T-shirt, he cannot wear it again until he washes it.

Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N days. If he does not need to buy new T-shirts, print 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.

Constraints

  • 1\leq M\leq N\leq 1000
  • S is a string of length N consisting of 0, 1, and 2.
  • N and M are integers.

Input

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

N M
S

Output

Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print 0.


Sample Input 1

6 1
112022

Sample Output 1

2

If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:

  • On the first day, he wears a logo T-shirt to go out for a meal.
  • On the second day, he wears a plain T-shirt to go out for a meal.
  • On the third day, he wears a logo T-shirt to attend a competitive programming event.
  • On the fourth day, he has no plans, so he washes all the worn T-shirts. This allows him to reuse the T-shirts worn on the first, second, and third days.
  • On the fifth day, he wears a logo T-shirt to attend a competitive programming event.
  • On the sixth day, he wears a logo T-shirt to attend a competitive programming event.

If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2.


Sample Input 2

3 1
222

Sample Output 2

3

Sample Input 3

2 1
01

Sample Output 3

0

He does not need to buy new T-shirts.

G - Synchronized Players

実行時間制限: 4 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

NN 列のグリッドがあり、各マスは空きマスか障害物のあるマスのいずれかです。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

また、2 人のプレイヤーがグリッド上の相異なる空きマス上におり、各マスの情報は N 個の長さ N の文字列 S_1, S_2, \ldots, S_N として以下の形式で与えられます。

  • S_ij 文字目が P であるとき、(i, j) は空きマスであり、プレイヤーがいる

  • S_ij 文字目が . であるとき、(i, j) は空きマスであり、プレイヤーがいない

  • S_ij 文字目が # であるとき、(i, j) は障害物のあるマスである

以下の操作を繰り返し 2 人のプレイヤーを同じマスに集めるために必要な操作回数の最小値を求めてください。ただし、操作の繰り返しにより 2 人のプレイヤーを同じマスに集めることができない場合には -1 を出力してください。

  • 上下左右のいずれかの方向を決める。そして各プレイヤーはともにその方向に隣接するマスへの移動を試みる。各プレイヤーは移動先のマスが存在し、かつ空きマスであるならば移動し、そうでないならば移動しない。

制約

  • N2 以上 60 以下の整数
  • S_i は長さ NP, ., # からなる文字列
  • S_ij 文字目が P であるような組 (i, j) の個数はちょうど 2

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

5
....#
#..#.
.P...
..P..
....#

出力例 1

3

はじめに (3, 2) にいるプレイヤーをプレイヤー 1(4, 3) にいるプレイヤーをプレイヤー 2 とします。

例えば以下のようにすることで、3 回の操作で 2 人のプレイヤーが同じマスに集まります。

  • 左を選択する。プレイヤー 1(3, 1) に移動し、プレイヤー 2(4, 2) に移動する。

  • 上を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 2) に移動する。

  • 左を選択する。プレイヤー 1 は移動せず、プレイヤー 2(3, 1) に移動する。


入力例 2

2
P#
#P

出力例 2

-1

入力例 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

出力例 3

10

Score: 400 points

Problem Statement

There is an N \times N grid, where each cell is either empty or contains an obstacle. Let (i, j) denote the cell at the i-th row from the top and the j-th column from the left.

There are also two players on distinct empty cells of the grid. The information about each cell is given as N strings S_1, S_2, \ldots, S_N of length N, in the following format:

  • If the j-th character of S_i is P, then (i, j) is an empty cell with a player on it.

  • If the j-th character of S_i is ., then (i, j) is an empty cell without a player.

  • If the j-th character of S_i is #, then (i, j) contains an obstacle.

Find the minimum number of moves required to bring the two players to the same cell by repeating the following operation. If it is impossible to bring the two players to the same cell by repeating the operation, print -1.

  • Choose one of the four directions: up, down, left, or right. Then, each player attempts to move to the adjacent cell in that direction. Each player moves if the destination cell exists and is empty, and does not move otherwise.

Constraints

  • N is an integer between 2 and 60, inclusive.
  • S_i is a string of length N consisting of P, ., and #.
  • There are exactly two pairs (i, j) where the j-th character of S_i is P.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

5
....#
#..#.
.P...
..P..
....#

Sample Output 1

3

Let us call the player starting at (3, 2) Player 1 and the player starting at (4, 3) Player 2.

For example, doing the following brings the two players to the same cell in three moves:

  • Choose left. Player 1 moves to (3, 1), and Player 2 moves to (4, 2).

  • Choose up. Player 1 does not move, and Player 2 moves to (3, 2).

  • Choose left. Player 1 does not move, and Player 2 moves to (3, 1).


Sample Input 2

2
P#
#P

Sample Output 2

-1

Sample Input 3

10
..........
..........
..........
..........
....P.....
.....P....
..........
..........
..........
..........

Sample Output 3

10
H - Complete Binary Tree

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 450

問題文

1 から N までの番号が付けられた N 頂点からなる木があります。 各 i\ (2 \leq i \leq N) について、頂点 i と頂点 \lfloor \frac{i}{2} \rfloor を結ぶ辺が張られています。 逆に、これら以外の辺は存在しません。

この木において、頂点 X との距離が K である頂点の数を求めてください。 ただし、2 頂点 u,v の距離は、頂点 u,v を結ぶ単純パスに含まれる辺の個数として定義されます。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\leq T \leq 10^5
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq N
  • 0\leq K \leq N-1
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。 ここで、\mathrm{test}_ii 番目のテストケースを意味する。

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

各テストケースは以下の形式で与えられる。

N X K 

出力

T 行出力せよ。

i\ (1 \leq i \leq T) 行目には、i 番目のテストケースに対する答えを整数として出力せよ。


入力例 1

5
10 2 0
10 2 1
10 2 2
10 2 3
10 2 4

出力例 1

1
3
4
2
0

N=10 のとき、木は以下の図のようになります。

このとき、

  • 頂点 2 との距離が 0 である頂点は 21 つです。
  • 頂点 2 との距離が 1 である頂点は 1,4,53 つです。
  • 頂点 2 との距離が 2 である頂点は 3,8,9,104 つです。
  • 頂点 2 との距離が 3 である頂点は 6,72 つです。
  • 頂点 2 との距離が 4 である頂点は存在しません。

入力例 2

10
822981260158260522 52 20
760713016476190629 2314654 57
1312150450968417 1132551176249851 7
1000000000000000000 1083770654 79
234122432773361868 170290518806790 23
536187734191890310 61862 14
594688604155374934 53288633578 39
1000000000000000000 120160810 78
89013034180999835 14853481725739 94
463213054346948152 825589 73

出力例 2

1556480
140703128616960
8
17732923532771328
65536
24576
2147483640
33776997205278720
7881299347898368
27021597764222976

Score : 450 points

Problem Statement

There is a tree with N vertices numbered 1 to N. For each i\ (2 \leq i \leq N), there is an edge connecting vertex i and vertex \lfloor \frac{i}{2} \rfloor. There are no other edges.

In this tree, find the number of vertices whose distance from vertex X is K. Here, the distance between two vertices u and v is defined as the number of edges in the simple path connecting vertices u and v.

You have T test cases to solve.

Constraints

  • 1\leq T \leq 10^5
  • 1\leq N \leq 10^{18}
  • 1\leq X \leq N
  • 0\leq K \leq N-1
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \mathrm{test}_i represents the i-th test case:

T
\mathrm{test}_1
\mathrm{test}_2
\vdots
\mathrm{test}_T

Each test case is given in the following format:

N X K 

Output

Print T lines.

The i-th line (1 \leq i \leq T) should contain the answer to the i-th test case as an integer.


Sample Input 1

5
10 2 0
10 2 1
10 2 2
10 2 3
10 2 4

Sample Output 1

1
3
4
2
0

The tree for N=10 is shown in the following figure.

Here,

  • There is 1 vertex, 2, whose distance from vertex 2 is 0.
  • There are 3 vertices, 1,4,5, whose distance from vertex 2 is 1.
  • There are 4 vertices, 3,8,9,10, whose distance from vertex 2 is 2.
  • There are 2 vertices, 6,7, whose distance from vertex 2 is 3.
  • There are no vertices whose distance from vertex 2 is 4.

Sample Input 2

10
822981260158260522 52 20
760713016476190629 2314654 57
1312150450968417 1132551176249851 7
1000000000000000000 1083770654 79
234122432773361868 170290518806790 23
536187734191890310 61862 14
594688604155374934 53288633578 39
1000000000000000000 120160810 78
89013034180999835 14853481725739 94
463213054346948152 825589 73

Sample Output 2

1556480
140703128616960
8
17732923532771328
65536
24576
2147483640
33776997205278720
7881299347898368
27021597764222976
I - Keep Connect

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

2 以上の整数 N および素数 P が与えられます。
下図のような 2N 頂点 (3N-2) 辺のグラフ G を考えます。

より具体的には、頂点を順に頂点 1, 頂点 2, \ldots, 頂点 2N、 辺を順に辺 1, 辺 2, \ldots, 辺 (3N-2) とすると、各辺は次のように頂点を結んでいます。

  • 1\leq i\leq N-1 について、辺 i は頂点 i と頂点 i+1 を結んでいる。
  • 1\leq i\leq N-1 について、辺 (N-1+i) は頂点 N+i と頂点 N+i+1 を結んでいる。
  • 1\leq i\leq N について、辺 (2N-2+i) は頂点 i と頂点 N+i を結んでいる。

i=1,2,\ldots ,N-1 について、次の問題を解いてください。

G3N-2 本の辺からちょうど i 本の辺を取り除く方法であって、辺を取り除いた後のグラフも連結であるようなものの個数を P で割ったあまりを求めよ。

制約

  • 2 \leq N \leq 3000
  • 9\times 10^8 \leq P \leq 10^9
  • N は整数である。
  • P は素数である。

入力

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

N P

出力

N-1 個の整数を空白区切りで出力せよ。 ただし、k 番目の整数は i=k に対する答えである。


入力例 1

3 998244353

出力例 1

7 15

N=3 の場合について、取り除いた後のグラフも連結となるように、ちょうど 1 本の辺を取り除く方法は次の 7 通りです。

取り除いた後のグラフも連結となるように、ちょうど 2 本の辺を取り除く方法は次の 15 通りです。

よって、これらを P=998244353 で割ったあまりである 7, 15 をこの順に出力します。


入力例 2

16 999999937

出力例 2

46 1016 14288 143044 1079816 6349672 29622112 110569766 330377828 784245480 453609503 38603306 44981526 314279703 408855776

P で割ったあまりを出力することに注意してください。

Score : 500 points

Problem Statement

You are given an integer N greater than or equal to 2 and a prime P.
Consider the graph G with 2N vertices and (3N-2) edges shown in the figure below.

More specifically, the edges connect the vertices as follows, where the vertices are labeled as Vertex 1, Vertex 2, \ldots, Vertex 2N, and the edges are labeled as Edge 1, Edge 2, \ldots, Edge (3N-2).

  • For each 1\leq i\leq N-1, Edge i connects Vertex i and Vertex i+1.
  • For each 1\leq i\leq N-1, Edge (N-1+i) connects Vertex N+i and Vertex N+i+1.
  • For each 1\leq i\leq N, Edge (2N-2+i) connects Vertex i and Vertex N+i.

For each i=1,2,\ldots ,N-1, solve the following problem.

Find the number of ways, modulo P, to remove exactly i of the 3N-2 edges of G so that the resulting graph is still connected.

Constraints

  • 2 \leq N \leq 3000
  • 9\times 10^8 \leq P \leq 10^9
  • N is an integer.
  • P is a prime.

Input

Input is given from Standard Input in the following format:

N P

Output

Print N-1 integers, the i-th of which is the answer for i=k, separated by spaces.


Sample Input 1

3 998244353

Sample Output 1

7 15

In the case N=3, there are 7 ways, shown below, to remove exactly one edge so that the resulting graph is still connected.

There are 15 ways, shown below, to remove exactly two edges so that the resulting graph is still connected.

Thus, these numbers modulo P=998244353 should be printed: 7 and 15, in this order.


Sample Input 2

16 999999937

Sample Output 2

46 1016 14288 143044 1079816 6349672 29622112 110569766 330377828 784245480 453609503 38603306 44981526 314279703 408855776

Be sure to print the numbers modulo P.