Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
1,2,\ldots,N の番号のついた N 人の人に合計 K 枚のカードを配ります。
人 A から始めて 人 A,A+1,A+2,\ldots,N,1,2,\ldots の順に 1 枚ずつカードを配るとき、最後のカードは誰に配られるでしょうか?
厳密には、人 x(1 \leq x < N) の次には人 x+1 にカードを配り、人 N の次には人 1 にカードを配ります。
制約
- 1 \leq N,K \leq 1000
- 1 \leq A \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A
出力
最後のカードが配られた人の番号を出力せよ。
入力例 1
3 3 2
出力例 1
1
人 2、人 3、人 1 の順にカードを配ります。
入力例 2
1 100 1
出力例 2
1
入力例 3
3 14 2
出力例 3
3
Score : 100 points
Problem Statement
We will hand out a total of K cards to N people numbered 1, 2, \ldots, N.
Beginning with Person A, we will give the cards one by one to the people in this order: A, A+1, A+2, \ldots, N, 1, 2, \ldots. Who will get the last card?
Formally, after Person x(1 \leq x < N) gets a card, Person x+1 will get a card. After Person N gets a card, Person 1 gets a card.
Constraints
- 1 \leq N,K \leq 1000
- 1 \leq A \leq N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K A
Output
Print a number representing the person who will get the last card.
Sample Input 1
3 3 2
Sample Output 1
1
The cards are given to Person 2, 3, 1 in this order.
Sample Input 2
1 100 1
Sample Output 2
1
Sample Input 3
3 14 2
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君はたこ焼きを作ってすぬけ君に振る舞うことにしました。高橋君はすぬけ君に、たこ焼きを食べたいならば左手のみを挙げて、そうでないならば右手のみを挙げるよう指示しました。
すぬけ君が挙げた手の情報は整数 L, R によって与えられます。 すぬけ君は L = 1 のとき、またそのときに限り左手を挙げており、R = 1 のとき、またそのときに限り右手を挙げています。すぬけ君は指示に従わず、両手を挙げることも手を挙げないこともあります。
すぬけ君が片方の手のみを挙げている場合、すぬけ君がたこ焼きを食べたいならば Yes を、そうでないならば No を出力してください。すぬけ君が両手を挙げているか、手を挙げていないときは Invalid と出力してください。
ただし、すぬけ君が片方の手のみを挙げている場合は必ず指示に従った行動を取っているものとします。
制約
- L, R は 0 または 1
入力
入力は以下の形式で標準入力から与えられる。
L R
出力
問題文の指示に従って Yes, No, Invalid のいずれかを出力せよ。
入力例 1
1 0
出力例 1
Yes
すぬけ君はたこ焼きを食べたいので左手のみを挙げています。
入力例 2
1 1
出力例 2
Invalid
すぬけ君は両手を挙げています。
Score : 100 points
Problem Statement
Takahashi decided to make takoyaki (octopus balls) and serve it to Snuke. Takahashi instructed Snuke to raise only his left hand if he wants to eat takoyaki, and only his right hand otherwise.
You are given the information about which hand Snuke is raising as two integers L and R. He is raising his left hand if and only if L = 1, and raising his right hand if and only if R = 1. He might not follow the instructions and could raise both hands or not raise any hand at all.
If Snuke is raising only one hand, print Yes if he wants to eat takoyaki, and No if he does not. If he is raising both hands or not raising any hand, print Invalid.
Assume that if Snuke is raising only one hand, he is always following the instructions.
Constraints
- Each of L and R is 0 or 1.
Input
The input is given from Standard Input in the following format:
L R
Output
Print Yes, No, or Invalid according to the instructions in the problem statement.
Sample Input 1
1 0
Sample Output 1
Yes
Snuke wants to eat takoyaki, so he is raising only his left hand.
Sample Input 2
1 1
Sample Output 2
Invalid
Snuke is raising both hands.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 R 行横 C 列の盤面があります。上から i 行目、左から j 列目のマスを (i,j) と表します。
(i,j) の現在の状態が文字 B_{i,j} として与えられます。
. は空きマス、# は壁があるマスを表し、
1, 2,\dots, 9 はそれぞれ威力 1,2,\dots,9 の爆弾があるマスを表します。
次の瞬間に、全ての爆弾が同時に爆発します。 爆弾が爆発すると、爆弾があるマスからのマンハッタン距離がその爆弾の威力以下であるような全てのマス(その爆弾があるマス自体を含む)が空きマスに変わります。 ここで、(r_1,c_1) から (r_2,c_2) までのマンハッタン距離は |r_1-r_2|+|c_1-c_2| です。
爆発後の盤面を出力してください。
制約
- 1\leq R,C \leq 20
- R,C は整数
- B_{i,j} は
.,#,1,2,\dots,9のいずれかである
入力
入力は以下の形式で標準入力から与えられる。
R C
B_{1,1}B_{1,2}\dots B_{1,C}
\vdots
B_{R,1}B_{R,2}\dots B_{R,C}
出力
爆発後の盤面を R 行で出力せよ。盤面の表し方は入力と同じ形式を用いること (R と C を出力する必要はない)。
入力例 1
4 4 .1.# ###. .#2. #.##
出力例 1
...# #... .... #...

- (1,2) にある爆弾の爆発によって、上図の青いマスと紫のマスが空きマスに変わります。
- (3,3) にある爆弾の爆発によって、上図の赤いマスと紫のマスが空きマスに変わります。
この例のように、爆弾が効果を及ぼす範囲に被りがあることもあります。
入力例 2
2 5 ..#.# ###.#
出力例 2
..#.# ###.#
爆弾が 1 つもないこともあります。
入力例 3
2 3 11# ###
出力例 3
... ..#
入力例 4
4 6 #.#3#. ###.#. ##.### #1..#.
出力例 4
...... #..... #....# ....#.
Score : 200 points
Problem Statement
We have a board with R rows running horizontally and C columns running vertically. Let (i,j) denote the square at the i-th row from the top and j-th column from the left.
You are given characters B_{i,j} representing the current states of (i,j).
. represents an empty square; # represents a square with a wall; 1, 2,\dots, 9 represent a square with a bomb of power 1,2,\dots,9, respectively.
At the next moment, all bombs will explode simultaneously. When a bomb explodes, every square whose Manhattan distance from the square with the bomb is not greater than the power of the bomb will turn into an empty square. Here, the Manhattan distance from (r_1,c_1) to (r_2,c_2) is |r_1-r_2|+|c_1-c_2|.
Print the board after the explosions.
Constraints
- 1\leq R,C \leq 20
- R and C are integers.
- Each B_{i,j} is one of
.,#,1,2, \dots,9.
Input
The input is given from Standard Input in the following format:
R C
B_{1,1}B_{1,2}\dots B_{1,C}
\vdots
B_{R,1}B_{R,2}\dots B_{R,C}
Output
Print R lines representing the board after the explosions. Use the format used in the input (do not print R or C).
Sample Input 1
4 4 .1.# ###. .#2. #.##
Sample Output 1
...# #... .... #...

- The explosion of the bomb at (1,2) will turn the blue squares and purple squares in the above figure into empty squares.
- The explosion of the bomb at (3,3) will turn the red squares and purple squares in the above figure into empty squares.
As seen in this sample, the explosion ranges of bombs may overlap.
Sample Input 2
2 5 ..#.# ###.#
Sample Output 2
..#.# ###.#
There may be no bombs.
Sample Input 3
2 3 11# ###
Sample Output 3
... ..#
Sample Input 4
4 6 #.#3#. ###.#. ##.### #1..#.
Sample Output 4
...... #..... #....# ....#.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は音楽プレイヤーを持っています。はじめ、音量は 0 であり、曲は停止中です。
これから、Q 回の操作を順に行います。i 回目の操作は整数 A_i によって表され、操作の内容は以下の通りです。
- A_i = 1 のとき、音量を 1 上げる。
- A_i = 2 のとき、現在の音量が 1 以上であれば音量を 1 下げ、0 であれば何もしない。
- A_i = 3 のとき、曲が停止中であれば曲を再生し、曲が再生中であれば曲を停止する。
i = 1, 2, \ldots, Q に対して、以下の問題を解いてください。
- i 回目の操作を終えた直後に音量 3 以上で音楽が再生されているか判定せよ。
制約
- 1 \leq Q \leq 2 \times 10^5
- A_i \in \lbrace 1, 2, 3 \rbrace
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
Q A_1 A_2 \vdots A_Q
出力
Q 行出力せよ。i 行目には、i 回目の操作を終えた直後に音量 3 以上で音楽が再生されているならば Yes を、そうでないならば No を出力せよ。
入力例 1
10 2 1 3 1 3 1 1 3 2 2
出力例 1
No No No No No No No Yes Yes No
-
1 回目の操作を終えた後、音量は 0 で曲は停止中です。
-
2 回目の操作を終えた後、音量は 1 で曲は停止中です。
-
3 回目の操作を終えた後、音量は 1 で曲は再生中です。
-
4 回目の操作を終えた後、音量は 2 で曲は再生中です。
-
5 回目の操作を終えた後、音量は 2 で曲は停止中です。
-
6 回目の操作を終えた後、音量は 3 で曲は停止中です。
-
7 回目の操作を終えた後、音量は 4 で曲は停止中です。
-
8 回目の操作を終えた後、音量は 4 で曲は再生中です。
-
9 回目の操作を終えた後、音量は 3 で曲は再生中です。
-
10 回目の操作を終えた後、音量は 2 で曲は再生中です。
Score : 200 points
Problem Statement
Takahashi has a music player. Initially, the volume is 0 and the music is stopped.
From now on, Q operations will be performed in order. The i-th operation is represented by an integer A_i, which means the following:
- If A_i = 1, increase the volume by 1.
- If A_i = 2, if the current volume is 1 or more, decrease it by 1; if it is 0, do nothing.
- If A_i = 3, if the music is stopped, play it; if the music is playing, stop it.
For i = 1, 2, \ldots, Q, solve the following problem:
- Determine whether the music is playing at volume 3 or more immediately after the i-th operation.
Constraints
- 1 \leq Q \leq 2 \times 10^5
- A_i \in \lbrace 1, 2, 3 \rbrace
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Q A_1 A_2 \vdots A_Q
Output
Output Q lines. The i-th line should contain Yes if the music is playing at volume 3 or more immediately after the i-th operation, and No otherwise.
Sample Input 1
10 2 1 3 1 3 1 1 3 2 2
Sample Output 1
No No No No No No No Yes Yes No
-
After the 1-st operation, the volume is 0 and the music is stopped.
-
After the 2-nd operation, the volume is 1 and the music is stopped.
-
After the 3-rd operation, the volume is 1 and the music is playing.
-
After the 4-th operation, the volume is 2 and the music is playing.
-
After the 5-th operation, the volume is 2 and the music is stopped.
-
After the 6-th operation, the volume is 3 and the music is stopped.
-
After the 7-th operation, the volume is 4 and the music is stopped.
-
After the 8-th operation, the volume is 4 and the music is playing.
-
After the 9-th operation, the volume is 3 and the music is playing.
-
After the 10-th operation, the volume is 2 and the music is playing.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は英小文字からなる文字列 T を青木君に向けて送りました。その結果、青木君は英小文字からなる文字列 T' を受信しました。
T' は T から一部が変更されてしまっている可能性があり、具体的には、下記の 4 つのうちのちょうど 1 つが成り立つことがわかっています。
- T' は、T と等しい。
- T' は、T のいずれか 1 つの位置(先頭と末尾も含む)に英小文字を 1 つ挿入して得られる文字列である。
- T' は、T からある 1 文字を削除して得られる文字列である。
- T' は、T のある 1 文字を別の英小文字に変更して得られる文字列である。
青木君が受信した文字列 T' と、英小文字からなる N 個の文字列 S_1, S_2, \ldots, S_N が入力として与えられるので、 S_1, S_2, \ldots, S_N のうち、高橋君が送った文字列 T と等しい可能性があるものをすべて求めてください。
制約
- N は整数
- 1 \leq N \leq 5 \times 10^5
- S_i と T' は英小文字からなる長さ 1 以上 5 \times 10^5 以下の文字列
- S_1, S_2, \ldots, S_N の長さの総和は 5 \times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
N T' S_1 S_2 \vdots S_N
出力
S_1, S_2, \ldots, S_N のうち T と等しい可能性があるものすべての添字を昇順に並べた列を (i_1, i_2, \ldots, i_K) とする。 この列の長さ K および列自体を、下記の形式にしたがって出力せよ。
K i_1 i_2 \ldots i_K
入力例 1
5 ababc ababc babc abacbc abdbc abbac
出力例 1
4 1 2 3 4
S_1, S_2, \ldots, S_5 のうち、T と等しい可能性があるものは S_1, S_2, S_3, S_4 の 4 つであることが下記の通りわかります。
- S_1 は T と等しい可能性があります。なぜなら、T' =
ababcは S_1 =ababcと等しいからです。 - S_2 は T と等しい可能性があります。なぜなら、T' =
ababcは S_2 =babcの先頭に文字aを挿入して得られる文字列だからです。 - S_3 は T と等しい可能性があります。なぜなら、T' =
ababcは S_3 =abacbcから 4 文字目のcを削除して得られる文字列だからです。 - S_4 は T と等しい可能性があります。なぜなら、T' =
ababcは S_4 =abdbcの 3 文字目のdをbに変更して得られる文字列だからです。 - S_5 は T と等しい可能性はありません。なぜなら、S_5 =
abbacを T としたとき、T' =ababcは問題文中の 4 つの条件をいずれも満たさないからです。
入力例 2
1 aoki takahashi
出力例 2
0
入力例 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
出力例 3
6 1 2 4 7 8 9
Score : 300 points
Problem Statement
Takahashi sent a string T consisting of lowercase English letters to Aoki. As a result, Aoki received a string T' consisting of lowercase English letters.
T' may have been altered from T. Specifically, exactly one of the following four conditions is known to hold.
- T' is equal to T.
- T' is a string obtained by inserting one lowercase English letter at one position (possibly the beginning and end) in T.
- T' is a string obtained by deleting one character from T.
- T' is a string obtained by changing one character in T to another lowercase English letter.
You are given the string T' received by Aoki and N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters. Find all the strings among S_1, S_2, \ldots, S_N that could equal the string T sent by Takahashi.
Constraints
- N is an integer.
- 1 \leq N \leq 5 \times 10^5
- S_i and T' are strings of length between 1 and 5 \times 10^5, inclusive, consisting of lowercase English letters.
- The total length of S_1, S_2, \ldots, S_N is at most 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
N T' S_1 S_2 \vdots S_N
Output
Let (i_1, i_2, \ldots, i_K) be the sequence of indices of all the strings among S_1, S_2, \ldots, S_N that could be equal to T, in ascending order. Print the length K of this sequence, and the sequence itself, in the following format:
K i_1 i_2 \ldots i_K
Sample Input 1
5 ababc ababc babc abacbc abdbc abbac
Sample Output 1
4 1 2 3 4
Among S_1, S_2, \ldots, S_5, the strings that could be equal to T are S_1, S_2, S_3, S_4, as explained below.
- S_1 could be equal to T, because T' =
ababcis equal to S_1 =ababc. - S_2 could be equal to T, because T' =
ababcis obtained by inserting the letteraat the beginning of S_2 =babc. - S_3 could be equal to T, because T' =
ababcis obtained by deleting the fourth charactercfrom S_3 =abacbc. - S_4 could be equal to T, because T' =
ababcis obtained by changing the third characterdin S_4 =abdbctob. - S_5 could not be equal to T, because if we take S_5 =
abbacas T, then T' =ababcdoes not satisfy any of the four conditions in the problem statement.
Sample Input 2
1 aoki takahashi
Sample Output 2
0
Sample Input 3
9 atcoder atoder atcode athqcoder atcoder tacoder jttcoder atoder atceoder atcoer
Sample Output 3
6 1 2 4 7 8 9
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
文字列 T が次の条件を満たすとき、T を良い括弧列と呼びます。
-
次の操作を 0 回以上繰り返すことで T を空文字列にすることができる。
- T に(連続な)部分文字列として含まれる
()を選び、これを取り除く。
- T に(連続な)部分文字列として含まれる
例えば () や (()()), および空文字列は良い括弧列ですが、)()( や ))) は良い括弧列ではありません。
文字列 S があります。S ははじめ空文字列です。
以下で説明されるクエリを与えられる順に Q 個処理してください。そして、各クエリの直後に S が良い括弧列であるかを判定してください。
クエリは次の 2 種類です。
1 c: 文字 c が与えられる。c は(または)である。c を S の末尾に追加する。2: S の末尾の文字を削除する。この時、S は空文字列でないことが保証される。
制約
- 1 \leq Q \leq 8 \times 10^5
- 1 種類目のクエリの c は
(または) - 2 種類目のクエリを与えられた時点で S は空でないことが保証される
- Q は整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{query}_i は i 番目のクエリを意味する。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の 2 種類のいずれかの形式で与えられる。
1 c
2
出力
Q 行出力せよ。i 行目には、i 番目のクエリを処理した直後の文字列 S が良い括弧列である場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
8 1 ( 2 1 ( 1 ) 2 1 ( 1 ) 1 )
出力例 1
No Yes No Yes No No No Yes
1 番目のクエリを処理した直後の S は ( で、これは良い括弧列ではありません。
2 番目のクエリを処理した直後の S は空文字列で、これは良い括弧列です。
3 番目のクエリを処理した直後の S は ( で、これは良い括弧列ではありません。
4 番目のクエリを処理した直後の S は () で、これは良い括弧列です。
5 番目のクエリを処理した直後の S は ( で、これは良い括弧列ではありません。
6 番目のクエリを処理した直後の S は (( で、これは良い括弧列ではありません。
7 番目のクエリを処理した直後の S は (() で、これは良い括弧列ではありません。
8 番目のクエリを処理した直後の S は (()) で、これは良い括弧列です。
Score : 300 points
Problem Statement
A string T is called a good bracket sequence if and only if it satisfies the following condition:
-
T can be made into an empty string by repeating the following operation zero or more times:
- Choose
()contained in T as a (contiguous) substring and remove it.
- Choose
For example, (), (()()), and the empty string are good bracket sequences, but )()( and ))) are not good bracket sequences.
There is a string S. Initially, S is an empty string.
Process Q queries in the order they are given. After each query, determine whether S is a good bracket sequence.
There are two types of queries:
1 c: A character c is given. c is either(or). Append c to the end of S.2: Remove the last character of S. It is guaranteed that S is not an empty string at this time.
Constraints
- 1 \leq Q \leq 8 \times 10^5
- c in queries of the first type is
(or). - It is guaranteed that S is not empty when a query of the second type is given.
- Q is an integer.
Input
The input is given from Standard Input in the following format, where \mathrm{query}_i denotes the i-th query.
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is given in one of the following two formats:
1 c
2
Output
Output Q lines. The i-th line should contain Yes if the string S immediately after processing the i-th query is a good bracket sequence, and No otherwise.
Sample Input 1
8 1 ( 2 1 ( 1 ) 2 1 ( 1 ) 1 )
Sample Output 1
No Yes No Yes No No No Yes
S immediately after processing the 1st query is (, which is not a good bracket sequence.
S immediately after processing the 2nd query is an empty string, which is a good bracket sequence.
S immediately after processing the 3rd query is (, which is not a good bracket sequence.
S immediately after processing the 4th query is (), which is a good bracket sequence.
S immediately after processing the 5th query is (, which is not a good bracket sequence.
S immediately after processing the 6th query is ((, which is not a good bracket sequence.
S immediately after processing the 7th query is ((), which is not a good bracket sequence.
S immediately after processing the 8th query is (()), which is a good bracket sequence.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
整数 W が与えられます。
あなたは以下の条件をすべて満たすようにいくつかのおもりを用意することにしました。
- おもりの個数は 1 個以上 300 個以下である。
- おもりの重さは 10^6 以下の正整数である。
- 1 以上 W 以下のすべての正整数は 良い整数 である。ここで、以下の条件を満たす正整数 n を良い整数と呼ぶ。
- 用意したおもりのうち \bf{3} 個以下 の異なるおもりを自由に選んで、選んだおもりの重さの和を n にすることができる。
条件を満たすようなおもりの組を 1 つ出力してください。
制約
- 1 \leq W \leq 10^6
- W は整数
入力
入力は以下の形式で標準入力から与えられる。
W
出力
N をおもりの個数、A_i を i 番目のおもりの重さとして、以下の形式で出力せよ。答えが複数存在する場合、どれを出力しても正解とみなされる。
N A_1 A_2 \dots A_N
ただし、N および A_1,A_2,\dots,A_N は以下の条件を満たす必要がある。
- 1 \leq N \leq 300
- 1 \leq A_i \leq 10^6
入力例 1
6
出力例 1
3 1 2 3
上の出力は重さ 1 のおもり、重さ 2 のおもり、重さ 3 のおもりの 3 個のおもりを用意しています。
この出力は条件を満たしています。特に 3 番目の条件について、以下のようにおもりを選ぶことで 1 以上 W 以下の整数すべてが良い整数であることが確認できます。
- 1 番目のおもりのみを選ぶと、重さの和は 1 になる。
- 2 番目のおもりのみを選ぶと、重さの和は 2 になる。
- 3 番目のおもりのみを選ぶと、重さの和は 3 になる。
- 1 番目と 3 番目のおもりを選ぶと、重さの和は 4 になる。
- 2 番目と 3 番目のおもりを選ぶと、重さの和は 5 になる。
- 1 番目、2 番目と 3 番目のおもりを選ぶと、重さの和は 6 になる。
入力例 2
12
出力例 2
6 2 5 1 2 5 1
同じ重さのおもりを 2 個以上用意しても良いです。
Score : 400 points
Problem Statement
You are given an integer W.
You are going to prepare some weights so that all of the conditions below are satisfied.
- The number of weights is between 1 and 300, inclusive.
- Each weight has a mass of positive integer not exceeding 10^6.
- Every integer between 1 and W, inclusive, is a good integer. Here, a positive integer n is said to be a good integer if the following condition is satisfied:
- We can choose at most 3 different weights from the prepared weights with a total mass of n.
Print a combination of weights that satisfies the conditions.
Constraints
- 1 \leq W \leq 10^6
- W is an integer.
Input
Input is given from Standard Input in the following format:
W
Output
Print in the following format, where N is the number of weights and A_i is the mass of the i-th weight. If multiple solutions exist, printing any of them is accepted.
N A_1 A_2 \dots A_N
Here, N and A_1,A_2,\dots,A_N should satisfy the following conditions:
- 1 \leq N \leq 300
- 1 \leq A_i \leq 10^6
Sample Input 1
6
Sample Output 1
3 1 2 3
In the output above, 3 weights with masses 1, 2, and 3 are prepared.
This output satisfies the conditions. Especially, regarding the 3-rd condition, we can confirm that every integer between 1 and W, inclusive, is a good integer.
- If we choose only the 1-st weight, it has a total mass of 1.
- If we choose only the 2-nd weight, it has a total mass of 2.
- If we choose only the 3-rd weight, it has a total mass of 3.
- If we choose the 1-st and the 3-rd weights, they have a total mass of 4.
- If we choose the 2-nd and the 3-rd weights, they have a total mass of 5.
- If we choose the 1-st, the 2-nd, and the 3-rd weights, they have a total mass of 6.
Sample Input 2
12
Sample Output 2
6 2 5 1 2 5 1
You may prepare multiple weights with the same mass.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
3 \times 3 のマス目があります。上から i 行目、左から j 列目 (1 \leq i,j \leq 3) のマスをマス (i,j) と表します。マス (i,j) には整数 A_{i,j} が書かれています。ここで、 \sum_{i=1}^3 \sum_{j=1}^3 A_{i,j} は奇数であることが保証されます。また、すべてのマスははじめ白で塗られています。
高橋君と青木君が、このマス目を使ってゲームを行います。ゲームでは、高橋君を先手として、二人が交互に以下の操作を行います。
- 白で塗られているマス (i,j)\,(1\leq i,j \leq 3) を選ぶ(操作が行われる時点で、そのようなマスは必ず存在することが示せる)。操作をしているプレイヤーが得点 A_{i,j} を得る。次に、操作をしているプレイヤーが高橋君ならば、マス (i,j) を赤で、青木君ならば青で塗る。
各操作のあと、次の判定を行います。
- 赤または青の同じ色で塗られたマスが縦・横・斜めのいずれかの方向に 3 つ連続する箇所があるか判定する。そのような箇所があれば、その時点でゲームを終了し、赤が 3 つ連続しているならば高橋君が、青が 3 つ連続しているならば青木君が勝利する。
- 白で塗られているマスが存在するか判定する。存在しなければ、その時点でゲームを終了し、その時点までに獲得した累計の得点が高い方のプレイヤーが勝利する。
ゲームは必ず有限回の操作で終了し、高橋君または青木君の一方が勝利することが示せます。両者が勝ちを目指して最適に行動するとき、どちらが勝つか判定してください。
制約
- |A_{i,j}| \leq 10^9
- \sum_{i=1}^3 \sum_{j=1}^3 A_{i,j} は奇数
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A_{1,1} A_{1,2} A_{1,3}
A_{2,1} A_{2,2} A_{2,3}
A_{3,1} A_{3,2} A_{3,3}
出力
高橋君が勝つならば Takahashi を、青木君が勝つならば Aoki を出力せよ。
入力例 1
0 0 0 0 1 0 0 0 0
出力例 1
Takahashi
高橋君が最初の手番で (2,2) を選択すると、その後どのように青木君が行動しても、高橋君が適切に行動することで、青で塗られたマスが 3 つ連続しないようにすることができます。赤で塗られたマスが 3 つ連続した場合は高橋君が勝ちます。赤で塗られたマスが 3 つ連続せずにゲームが終了した場合、その時点で高橋君は 1 点、青木君は 0 点を獲得しているため、どちらにせよ高橋君が勝ちます。
入力例 2
-1 1 0 -4 -2 -5 -4 -1 -5
出力例 2
Aoki
Score: 450 points
Problem Statement
There is a 3 \times 3 grid. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left (1 \leq i, j \leq 3). Cell (i, j) contains an integer A_{i,j}. It is guaranteed that \sum_{i=1}^3 \sum_{j=1}^3 A_{i,j} is odd. Additionally, all cells are initially painted white.
Takahashi and Aoki will play a game using this grid. Takahashi goes first, and they take turns performing the following operation:
- Choose a cell (i, j) (1\leq i, j \leq 3) that is still painted white (it can be shown that such a cell always exists at the time of the operation). The player performing the operation scores A_{i,j} points. Then, if the player is Takahashi, he paints the cell (i, j) red; if the player is Aoki, he paints it blue.
After each operation, the following checks are made:
- Check if there are three consecutive cells painted the same color (red or blue) in any row, column, or diagonal. If such a sequence exists, the game ends immediately, and the player whose color forms the sequence wins.
- Check if there are white cells left. If no white cells remain, the game ends, and the player with the higher total score wins.
It can be shown that the game will always end after a finite number of moves, and either Takahashi or Aoki will win. Determine which player wins if both play optimally for victory.
Constraints
- |A_{i,j}| \leq 10^9
- \sum_{i=1}^3 \sum_{j=1}^3 A_{i,j} is odd.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A_{1,1} A_{1,2} A_{1,3}
A_{2,1} A_{2,2} A_{2,3}
A_{3,1} A_{3,2} A_{3,3}
Output
If Takahashi wins, print Takahashi; if Aoki wins, print Aoki.
Sample Input 1
0 0 0 0 1 0 0 0 0
Sample Output 1
Takahashi
If Takahashi chooses cell (2,2) in his first move, no matter how Aoki plays afterward, Takahashi can always act to prevent three consecutive blue cells. If three consecutive red cells are formed, Takahashi wins. If the game ends without three consecutive red cells, at that point, Takahashi has scored 1 point and Aoki 0 points, so Takahashi wins either way.
Sample Input 2
-1 1 0 -4 -2 -5 -4 -1 -5
Sample Output 2
Aoki
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
縦 N 行、横 M 列のマス目に、黒い飛車の駒 B 個と白い飛車の駒 W 個を設置することを考えましょう。
以下の条件をすべて満たす設置の仕方を いい配置 と呼びます。
- B+W 個の駒すべてが設置されている。
- 1 つのマスに置かれている駒の数は高々 1 つである。
- ある白い駒と黒い駒の組であって、互いが互いを攻撃しているようなものが存在しない。すなわち、ある白い駒と黒い駒の組であって、一方が 1 手の移動によってもう片方が置かれているマスに到達できるようなものが存在しない。
ここで、飛車の駒は、今いる位置から上、下、右、左のいずれかの方向に伸びる直線上にあり、かつ他の駒を飛び越えずに到達できるマスに 1 手で移動することができます。
いい配置としてあり得るものは何通りありますか?答えは非常に大きくなることがあるので、998244353 で割ったあまりを出力してください。
同じ色の駒同士は区別しないものとします。
制約
- 1 \leq N,M \leq 50
- 1 \leq B,W \leq 2500
- B+W \leq N \times M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M B W
出力
答えを 998244353 で割ったあまりを出力せよ。
入力例 1
2 2 1 1
出力例 1
4
いい配置としてあり得るものは以下の 4 通りです。

入力例 2
1 2 1 1
出力例 2
0
いい配置としてあり得るものが存在しない場合もあります。
入力例 3
40 40 30 30
出力例 3
467620384
998244353 で割ったあまりを出力することに注意してください。
Score : 500 points
Problem Statement
Consider placing B black rooks and W white rooks on a grid with N horizontal rows and M vertical columns.
A placement of the rooks satisfying all of the following conditions is called a good placement.
- All B+W rooks are placed on the grid.
- At most one rook is placed in the same square.
- There is no pair of white rook and black rook attacking each other. That is, there is no pair of white rook and black rook such that one of them can reach the square in which the other is placed in one move.
Here, in one move, a rook can reach any square that is on a horizontal or vertical line from its current position and is reachable without jumping over another rook.
How many good placements are there? Since this count can be enormous, print it modulo 998244353.
Rooks in the same color are not distinguished.
Constraints
- 1 \leq N,M \leq 50
- 1 \leq B,W \leq 2500
- B+W \leq N \times M
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M B W
Output
Print the count modulo 998244353.
Sample Input 1
2 2 1 1
Sample Output 1
4
There are four good placements as follows.

Sample Input 2
1 2 1 1
Sample Output 2
0
There may be no good placement.
Sample Input 3
40 40 30 30
Sample Output 3
467620384
Be sure to print the count modulo 998244353.