Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
文字列 S,T が与えられます。S は英小文字からなり、T は S に英小文字を 1 つ挿入して作られたことがわかっています。
挿入された文字は T の先頭から何番目の文字であるか求めてください。
複数の候補が考えられる場合はいずれか 1 つを求めてください。
制約
- 1 \leq |S| \leq 5\times 10^5
- S は英小文字からなる
- T は S に英小文字を 1 つ挿入して作られた文字列である
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
答えを出力せよ。なお、答えが複数考えられる場合はどれを出力しても正解となる。
入力例 1
atcoder atcorder
出力例 1
5
T の先頭から 5 番目の文字 r が挿入された文字です。
入力例 2
million milllion
出力例 2
5
T の先頭から 3,4,5 番目の文字のいずれかが挿入された文字です。
よって、3,4,5 のいずれかを出力すると正解となります。
入力例 3
vvwvw vvvwvw
出力例 3
3
Score : 300 points
Problem Statement
You are given strings S and T. S consists of lowercase English letters, and T is obtained by inserting a lowercase English letter into S.
Find the position of the inserted character in T.
If there are multiple candidates, find any of them.
Constraints
- 1 \leq |S| \leq 5\times 10^5
- S consists of lowercase English letters.
- T is obtained by inserting a lowercase English letter into S.
Input
The input is given from Standard Input in the following format:
S T
Output
Print an integer i, representing that the inserted character is the i-th character from the beginning of T. If there are multiple possible answers, printing any of them is accepted.
Sample Input 1
atcoder atcorder
Sample Output 1
5
The 5-th character from the beginning of T, r, is inserted.
Sample Input 2
million milllion
Sample Output 2
5
One of the 3-rd, 4-th, and 5-th characters from the beginning of T is inserted. Thus, printing any one of 3, 4, and 5 is accepted.
Sample Input 3
vvwvw vvvwvw
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正整数 D が与えられます。
非負整数 x,y に対する |x^2+y^2-D| の最小値を求めてください。
制約
- 1\leq D \leq 2\times 10^{12}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
D
出力
答えを出力せよ。
入力例 1
21
出力例 1
1
x=4,y=2 のとき |x^2+y^2-D| = |16+4-21|=1 となります。
|x^2+y^2-D|=0 を満たすような非負整数 x,y は存在しないので、答えは 1 です。
入力例 2
998244353
出力例 2
0
入力例 3
264428617
出力例 3
32
Score : 300 points
Problem Statement
You are given a positive integer D.
Find the minimum value of |x^2+y^2-D| for non-negative integers x and y.
Constraints
- 1\leq D \leq 2\times 10^{12}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
D
Output
Print the answer.
Sample Input 1
21
Sample Output 1
1
For x=4 and y=2, we have |x^2+y^2-D| = |16+4-21|=1.
There are no non-negative integers x and y such that |x^2+y^2-D|=0, so the answer is 1.
Sample Input 2
998244353
Sample Output 2
0
Sample Input 3
264428617
Sample Output 3
32
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。 上から i 行目、左から j 列目のマスを (i,j) と表記します。
各マスはスタートマス・ゴールマス・空きマス・障害物マスのいずれかであり、その情報は H 個の長さ W の文字列 S_1,S_2,\dots,S_H によって表されます。
具体的には、マス (i,j) は S_i の j 文字目が S であるときスタートマス、G であるときゴールマス、. であるとき空きマス、# であるとき障害物マスです。
ここで、スタートマスとゴールマスはちょうど 1 つずつ存在することが保証されます。
あなたは今スタートマスにいます。 あなたの目標は、今いるマスと辺で隣接するマスに移動することを繰り返してゴールマスへ行くことです。 ただし、障害物マスやグリッドの外に移動することはできず、また縦移動と横移動を 1 回ずつ交互に行わなければなりません。(最初の移動の向きは任意です。)
ゴールマスへ行くことが可能であるか判定し、可能ならば移動回数の最小値を求めてください。
より形式的には、以下の条件をすべて満たすマスの列 (i_1,j_1),(i_2,j_2),\dots,(i_k,j_k) が存在するか判定し、存在するならば k-1 の最小値を求めてください。
- すべての 1\leq l\leq k について、1\leq i_l\leq H かつ 1\leq j_l\leq W であり、(i_l,j_l) は障害物マスでない
- (i_1,j_1) はスタートマス
- (i_k,j_k) はゴールマス
- すべての 1\leq l\leq k-1 について、|i_l-i_{l+1}|+|j_l-j_{l+1}|=1
- すべての 1\leq l\leq k-2 について、i_l\neq i_{l+1} ならば i_{l+1}=i_{l+2}
- すべての 1\leq l\leq k-2 について、j_l\neq j_{l+1} ならば j_{l+1}=j_{l+2}
制約
- 1\leq H,W \leq 1000
- H,W は整数
- S_i は
S,G,.,#からなる長さ W の文字列 - スタートマスとゴールマスはちょうど 1 つずつ存在する
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
ゴールマスへ行くことが可能ならば移動回数の最小値を、不可能ならば -1 を出力せよ。
入力例 1
3 5 .S#.G ..... .#...
出力例 1
7

左図のように (1,2)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,4)\rightarrow(2,4)\rightarrow(2,5)\rightarrow(1,5) と移動することで、7 回の移動でゴールマスへ行くことができます。 6 回以下の移動でゴールマスへ行くことはできないので、答えは 7 です。
右図のように横移動を連続で行う経路(あるいは縦移動を連続で行う経路)はとれないことに注意してください。
入力例 2
3 5 ..#.G ..... S#...
出力例 2
-1
ゴールマスへ行くことはできません。
入力例 3
8 63 ............................................................... ..S...#............................#####..#####..#####..####G.. ..#...#................................#..#...#......#..#...... ..#####..####...####..####..#..#...#####..#...#..#####..#####.. ..#...#..#..#...#..#..#..#..#..#...#......#...#..#..........#.. ..#...#..#####..####..####..####...#####..#####..#####..#####.. ................#.....#........#............................... ................#.....#........#...............................
出力例 3
148
Score : 400 points
Problem Statement
You are given a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.
Each cell is one of the following: a start cell, a goal cell, an empty cell, or an obstacle cell. This information is described by H strings S_1,S_2,\dots,S_H, each of length W. Specifically, the cell (i,j) is a start cell if the j-th character of S_i is S, a goal cell if it is G, an empty cell if it is ., and an obstacle cell if it is #. It is guaranteed that there is exactly one start cell and exactly one goal cell.
You are currently on the start cell. Your objective is to reach the goal cell by repeatedly moving to a cell adjacent by an edge to the one you are in. However, you cannot move onto an obstacle cell or move outside the grid, and you must alternate between moving vertically and moving horizontally each time. (The direction of the first move can be chosen arbitrarily.)
Determine if it is possible to reach the goal cell. If it is, find the minimum number of moves required.
More formally, check if there exists a sequence of cells (i_1,j_1),(i_2,j_2),\dots,(i_k,j_k) satisfying all of the following conditions. If such a sequence exists, find the minimum value of k-1.
- For every 1 \leq l \leq k, it holds that 1 \leq i_l \leq H and 1 \leq j_l \leq W, and (i_l,j_l) is not an obstacle cell.
- (i_1,j_1) is the start cell.
- (i_k,j_k) is the goal cell.
- For every 1 \leq l \leq k-1, |i_l - i_{l+1}| + |j_l - j_{l+1}| = 1.
- For every 1 \leq l \leq k-2, if i_l \neq i_{l+1}, then i_{l+1} = i_{l+2}.
- For every 1 \leq l \leq k-2, if j_l \neq j_{l+1}, then j_{l+1} = j_{l+2}.
Constraints
- 1 \leq H,W \leq 1000
- H and W are integers.
- Each S_i is a string of length W consisting of
S,G,.,#. - There is exactly one start cell and exactly one goal cell.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
If it is possible to reach the goal cell, print the minimum number of moves. Otherwise, print -1.
Sample Input 1
3 5 .S#.G ..... .#...
Sample Output 1
7

As shown in the left figure, you can move as (1,2)\rightarrow(2,2)\rightarrow(2,3)\rightarrow(3,3)\rightarrow(3,4)\rightarrow(2,4)\rightarrow(2,5)\rightarrow(1,5), reaching the goal cell in 7 moves. It is impossible in 6 moves or fewer, so the answer is 7.
Note that you cannot take a path that moves horizontally (or vertically) consecutively without alternating as shown in the right figure.
Sample Input 2
3 5 ..#.G ..... S#...
Sample Output 2
-1
It is not possible to reach the goal cell.
Sample Input 3
8 63 ............................................................... ..S...#............................#####..#####..#####..####G.. ..#...#................................#..#...#......#..#...... ..#####..####...####..####..#..#...#####..#...#..#####..#####.. ..#...#..#..#...#..#..#..#..#..#...#......#...#..#..........#.. ..#...#..#####..####..####..####...#####..#####..#####..#####.. ................#.....#........#............................... ................#.....#........#...............................
Sample Output 3
148
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
配点 : 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_i を x で置き換える。
操作が行えなくなった方が負けで、負けなかった方が勝ちです。両者が勝ちを目指して最適な行動を取るとき、どちらが勝つか判定してください。
制約
- 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_3 を 2 にする。
- Bruno が A_1 を 1 にする。
- Anna が A_2 を 1 にする。
- Bruno が A_3 を 1 にする。
- 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