Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
ABC400 を記念した式典において、400 人の高橋君を A 行 B 列の長方形状に隙間なく並べようとしています。
正整数 A が与えられるので、このような並べ方ができるような正整数 B の値を出力してください。ただし、そのような正整数 B が存在しない場合には -1 を出力してください。
制約
- A は 1 以上 400 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
A
出力
問題文の指示に従って B の値あるいは -1 を出力せよ。
入力例 1
10
出力例 1
40
400 人の高橋君を 10 行 40 列の長方形状に並べることができます。
入力例 2
11
出力例 2
-1
入力例 3
400
出力例 3
1
Score : 100 points
Problem Statement
In the ceremony commemorating ABC400, we want to arrange 400 people in a rectangular formation of A rows and B columns without any gaps.
You are given a positive integer A. Print the value of a positive integer B for which such an arrangement is possible. If there is no such positive integer B, print -1.
Constraints
- A is an integer between 1 and 400, inclusive.
Input
The input is given from Standard Input in the following format:
A
Output
Print the value of B or -1 as specified by the problem statement.
Sample Input 1
10
Sample Output 1
40
We can arrange 400 people in 10 rows and 40 columns.
Sample Input 2
11
Sample Output 2
-1
Sample Input 3
400
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 問の問題からなるコンテストが開催され、i\ (1\leq i\leq N) 問目の配点は A_i 点でした。
すぬけくんはこのコンテストに参加し、B_1,B_2,\ldots,B_M 問目の M 問を解きました。 すぬけくんの総得点を求めてください。
ただし、総得点とは解いた問題の配点の総和を意味するものとします。
制約
- 1\leq M \leq N \leq 100
- 1\leq A_i \leq 100
- 1\leq B_1 < B_2 < \ldots < B_M \leq N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
答えを整数として出力せよ。
入力例 1
3 2 10 20 30 1 3
出力例 1
40
すぬけくんは 1 問目と 3 問目を解きました。 配点はそれぞれ 10 点と 30 点なので、総得点は 10+30=40 点です。
入力例 2
4 1 1 1 1 100 4
出力例 2
100
入力例 3
8 4 22 75 26 45 72 81 47 29 4 6 7 8
出力例 3
202
Score : 100 points
Problem Statement
There was a contest with N problems. The i-th (1\leq i\leq N) problem was worth A_i points.
Snuke took part in this contest and solved M problems: the B_1-th, B_2-th, \ldots, and B_M-th ones. Find his total score.
Here, the total score is defined as the sum of the points for the problems he solved.
Constraints
- 1\leq M \leq N \leq 100
- 1\leq A_i \leq 100
- 1\leq B_1 < B_2 < \ldots < B_M \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
Print the answer as an integer.
Sample Input 1
3 2 10 20 30 1 3
Sample Output 1
40
Snuke solved the 1-st and 3-rd problems, which are worth 10 and 30 points, respectively. Thus, the total score is 10+30=40 points.
Sample Input 2
4 1 1 1 1 100 4
Sample Output 2
100
Sample Input 3
8 4 22 75 26 45 72 81 47 29 4 6 7 8
Sample Output 3
202
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 N が与えられるので、 2^k \le N となる最大の整数 k を求めてください。
制約
- N は 1 \le N \le 10^{18} を満たす整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
6
出力例 1
2
- k=2 は 2^2=4 \le 6 を満たします。
- k \ge 3 である全ての整数 k について 2^k > 6 となります。
以上より、答えは k=2 となります。
入力例 2
1
出力例 2
0
2^0=1 であることに注意してください。
入力例 3
1000000000000000000
出力例 3
59
入力が 32 bit 整数に収まらない場合があります。
Score : 200 points
Problem Statement
Given a positive integer N, find the maximum integer k such that 2^k \le N.
Constraints
- N is an integer satisfying 1 \le N \le 10^{18}.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
6
Sample Output 1
2
- k=2 satisfies 2^2=4 \le 6.
- For every integer k such that k \ge 3, 2^k > 6 holds.
Therefore, the answer is k=2.
Sample Input 2
1
Sample Output 2
0
Note that 2^0=1.
Sample Input 3
1000000000000000000
Sample Output 3
59
The input value may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 x に対し、f(x) を以下のように定義します。
- x を(先頭に余分な 0 をつけずに)十進表記して得られる文字列を s_x、s_x を反転して得られる文字列を \text{rev}(s_x) とおく。 f(x) の値は、\text{rev}(s_x) を整数の十進表記としてみなすことで得られる整数である。
例えば、x=13 のとき \text{rev}(s_x)=\ 31 より f(x)=31 であり、x=10 のとき \text{rev}(s_x)=\ 01 より f(x)=1 です。
特に、どのような正整数 x に対しても f(x) の値は正整数です。
正整数 X,Y が与えられます。 正整数列 A=(a_1,a_2,\dots,a_{10}) を以下のように定義します。
- a_1 = X
- a_2 = Y
- a_i = f(a_{i-1}+a_{i-2})\ (i\geq 3)
a_{10} の値を求めてください。
制約
- 1\leq X,Y \leq 10^5
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
X Y
出力
a_{10} の値を出力せよ。
入力例 1
1 1
出力例 1
415
A の各要素の値は以下の通りです。
- a_1=1
- a_2=1
- a_3=2
- a_4=3
- a_5=5
- a_6=8
- a_7=31
- a_8=93
- a_9=421
- a_{10}=415
よって 415 を出力します。
入力例 2
3 7
出力例 2
895
入力例 3
90701 90204
出力例 3
9560800101
Score : 200 points
Problem Statement
For a positive integer x, define f(x) as follows:
- Let s_x be the string obtained by representing x in decimal notation (without leading zeros), and let \text{rev}(s_x) be the string obtained by reversing s_x. The value of f(x) is the integer obtained by interpreting \text{rev}(s_x) as a decimal representation of an integer.
For example, when x=13, we have \text{rev}(s_x)= 31, so f(x)=31; when x=10, we have \text{rev}(s_x)= 01, so f(x)=1.
Particularly, for any positive integer x, the value of f(x) is a positive integer.
You are given positive integers X and Y. Define a sequence of positive integers A=(a_1,a_2,\dots,a_{10}) as follows:
- a_1 = X
- a_2 = Y
- a_i = f(a_{i-1}+a_{i-2})\ (i\geq 3)
Find the value of a_{10}.
Constraints
- 1\leq X,Y \leq 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
X Y
Output
Print the value of a_{10}.
Sample Input 1
1 1
Sample Output 1
415
The values of the elements of A are as follows:
- a_1=1
- a_2=1
- a_3=2
- a_4=3
- a_5=5
- a_6=8
- a_7=31
- a_8=93
- a_9=421
- a_{10}=415
Thus, print 415.
Sample Input 2
3 7
Sample Output 2
895
Sample Input 3
90701 90204
Sample Output 3
9560800101
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
空の箱があります。
髙橋君は以下の 2 種類の魔法を好きな順番で好きな回数使えます。
- 魔法 A :箱の中にボールを 1 つ増やす
- 魔法 B :箱の中のボールの数を 2 倍にする
合計 \mathbf{120} 回以内の魔法で、箱の中のボールの数をちょうど N 個にする方法を 1 つ教えてください。
なお、与えられた制約のもとで条件を満たす方法が必ず存在することが示せます。
魔法以外の方法でボールの数を変化させることはできません。
制約
- 1 \leq N \leq 10^{18}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
A , B のみからなる文字列 S を出力せよ。
S の i 文字目が A ならば、髙橋君が i 回目に使う魔法が魔法 A であることを表し、B ならば魔法 B であることを表す。
S の長さは \mathbf{120} 以下でなければならない。
入力例 1
5
出力例 1
AABA
ボールの数は、0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5 と変化します。
AAAAA などの答えも正解になります。
入力例 2
14
出力例 2
BBABBAAAB
ボールの数は、0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14 と変化します。
S の長さを最小化する必要はありません。
Score : 300 points
Problem Statement
We have an empty box.
Takahashi can cast the following two spells any number of times in any order.
- Spell A: puts one new ball into the box.
- Spell B: doubles the number of balls in the box.
Tell us a way to have exactly N balls in the box with at most \mathbf{120} casts of spells.
It can be proved that there always exists such a way under the Constraints given.
There is no way other than spells to alter the number of balls in the box.
Constraints
- 1 \leq N \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
Output
Print a string S consisting of A and B.
The i-th character of S should represent the spell for the i-th cast.
S must have at most \mathbf{120} characters.
Sample Input 1
5
Sample Output 1
AABA
This changes the number of balls as follows: 0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5.
There are also other acceptable outputs, such as AAAAA.
Sample Input 2
14
Sample Output 2
BBABBAAAB
This changes the number of balls as follows: 0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14.
It is not required to minimize the length of S.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
3\times3 のマス目に 1 から 9 までの数字が書き込まれており、上から i 行目、左から j 列目 (1\leq i\leq3,1\leq j\leq3) に書き込まれている数字は c _ {i,j} です。
異なるマスに同じ数字が書き込まれている場合もありますが、同じ数字が縦・横・斜めに 3 つ連続して書き込まれていることはありません。 より厳密には、c _ {i,j} について次の条件のすべてが成り立っていることが保証されます。
- どの 1\leq i\leq3 についても、c _ {i,1}=c _ {i,2}=c _ {i,3} ではない
- どの 1\leq j\leq3 についても、c _ {1,j}=c _ {2,j}=c _ {3,j} ではない
- c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
- c _ {3,1}=c _ {2,2}=c _ {1,3} ではない
高橋くんは、それぞれのマスに書かれている数字をランダムな順番で知ります。 高橋くんは、縦・横・斜めの列のうちの 1 つでも次の条件を満たしたときがっかりします。
- はじめに知ったほうの 2 マスに書かれた数字が同じであり、最後に知ったマスに書かれた数字がそれと異なる。
高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を求めてください。
制約
- c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
- c _ {i,1}=c _ {i,2}=c _ {i,3} ではない (1\leq i\leq3)
- c _ {1,j}=c _ {2,j}=c _ {3,j} ではない (1\leq j\leq3)
- c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
- c _ {1,3}=c _ {2,2}=c _ {3,1} ではない
入力
入力は以下の形式で標準入力から与えられる。
c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}
出力
高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を 1 行で出力せよ。 真の値からの絶対誤差が 10 ^ {-8} 以下であるとき、正答と判定される。
入力例 1
3 1 9 2 5 6 2 7 1
出力例 1
0.666666666666666666666666666667
例えば、高橋くんが c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 の順に知った場合、高橋くんはがっかりしてしまいます。

対して、高橋くんが c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} の順に数字を知った場合、がっかりすることなくすべての数字を知ることができます。
高橋くんががっかりすることなくすべての数字を知ることができる確率は \dfrac 23 です。 絶対誤差が 10 ^ {-8} 以下であれば正答と判定されるため、0.666666657 や 0.666666676 のように出力しても正解になります。
入力例 2
7 7 6 8 6 8 7 7 6
出力例 2
0.004982363315696649029982363316
入力例 3
3 6 7 1 9 7 5 7 5
出力例 3
0.4
Score : 300 points
Problem Statement
There is a 3\times3 grid with numbers between 1 and 9, inclusive, written in each square. The square at the i-th row from the top and j-th column from the left (1\leq i\leq3,1\leq j\leq3) contains the number c _ {i,j}.
The same number may be written in different squares, but not in three consecutive cells vertically, horizontally, or diagonally. More precisely, it is guaranteed that c _ {i,j} satisfies all of the following conditions.
- c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
- c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
- c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
- c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.
Takahashi will see the numbers written in each cell in random order. He will get disappointed when there is a line (vertical, horizontal, or diagonal) that satisfies the following condition.
- The first two squares he sees contain the same number, but the last square contains a different number.
Find the probability that Takahashi sees the numbers in all the squares without getting disappointed.
Constraints
- c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
- c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
- c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
- c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
- c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.
Input
The input is given from Standard Input in the following format:
c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}
Output
Print one line containing the probability that Takahashi sees the numbers in all the squares without getting disappointed. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}.
Sample Input 1
3 1 9 2 5 6 2 7 1
Sample Output 1
0.666666666666666666666666666667
For example, if Takahashi sees c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 in this order, he will get disappointed.

On the other hand, if Takahashi sees c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} in this order, he will see all numbers without getting disappointed.
The probability that Takahashi sees all the numbers without getting disappointed is \dfrac 23. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}, so outputs such as 0.666666657 and 0.666666676 would also be accepted.
Sample Input 2
7 7 6 8 6 8 7 7 6
Sample Output 2
0.004982363315696649029982363316
Sample Input 3
3 6 7 1 9 7 5 7 5
Sample Output 3
0.4
Time Limit: 2.5 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のマス目があり、上から i 行目、左から j 列目のマスをマス (i, j) と表記します。
マス目の上には N 個のゴミが落ちており、i 番目のゴミはマス (X_i, Y_i) に落ちています。
Q 個のクエリが与えられるので、順に処理したときの各クエリの答えを求めてください。各クエリは、以下のいずれかです。
-
タイプ 1 :
1 xの形式で入力として与えられる。マス目の x 行目に落ちているゴミの個数を答えとして求める。その後、x 行目に落ちているゴミはすべて捨てられ、マス目上から取り除かれる。 -
タイプ 2 :
2 yの形式で入力として与えられる。マス目の y 列目に落ちているゴミの個数を答えとして求める。その後、y 列目に落ちているゴミはすべて捨てられ、マス目上から取り除かれる。
制約
- 1 \leq H, W, N \leq 2 \times 10^5
- 1 \leq X_i \leq H
- 1 \leq Y_i \leq W
- i \neq j のとき (X_i, Y_i) \neq (X_j, Y_j)
- 1 \leq Q \leq 2 \times 10^5
- タイプ 1 のクエリについて、1 \leq x \leq H
- タイプ 2 のクエリについて、1 \leq y \leq W
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ここで、\text{query}_i は i 番目のクエリであり、以下のいずれかの形式で与えられる。
1 x
2 y
出力
Q 行出力せよ。i 行目には i 番目のクエリの答えを出力せよ。
入力例 1
3 4 5 1 2 1 3 3 4 3 1 2 2 5 1 1 1 2 2 2 2 4 1 2
出力例 1
2 1 0 1 0
はじめ、ゴミはマス (1, 2), (1, 3), (3, 4), (3, 1), (2, 2) に落ちています。
1 番目のクエリでは、1 行目に落ちているゴミはマス (1, 2), (1, 3) に落ちているゴミの 2 つであるため答えは 2 となります。これらの 2 つのゴミは捨てられ、残ったゴミはマス (3, 4), (3, 1), (2, 2) に落ちているゴミです。
2 番目のクエリでは、2 行目に落ちているゴミはマス (2, 2) にあるゴミの 1 つであるため答えは 1 となります。このゴミは捨てられ、残ったゴミはマス (3, 4), (3, 1) に落ちているゴミです。
3 番目のクエリでは、2 列目に落ちているゴミは存在しないため答えは 0 となります。
4 番目のクエリでは、4 列目に落ちているゴミはマス (3, 4) にあるゴミの 1 つであるため答えは 1 となります。このゴミは捨てられ、残ったゴミはマス (3, 1) に落ちているゴミです。
5 番目のクエリでは、2 行目に落ちているゴミは存在しないため答えは 0 となります。
入力例 2
1 2 1 1 2 7 2 1 2 1 2 1 2 1 2 1 2 1 2 1
出力例 2
0 0 0 0 0 0 0
入力例 3
4 4 16 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 4 1 4 2 4 3 4 4 7 2 1 1 1 2 2 1 2 2 3 1 3 2 4
出力例 3
4 3 3 2 2 1 1
Score : 400 points
Problem Statement
There is 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.
There are N pieces of trash on the grid; the i-th piece is at cell (X_i, Y_i).
You are given Q queries, which you must process in order. Each query is of one of the following types:
-
Type 1: Given in the format
1 xin the input. Report the number of trash pieces in the x-th row. Then, all trash pieces in the x-th row are removed from the grid. -
Type 2: Given in the format
2 yin the input. Report the number of trash pieces in the y-th column. Then, all trash pieces in the y-th column are removed from the grid.
Constraints
- 1 \leq H, W, N \leq 2 \times 10^5
- 1 \leq X_i \leq H
- 1 \leq Y_i \leq W
- If i \neq j, then (X_i, Y_i) \neq (X_j, Y_j).
- 1 \leq Q \leq 2 \times 10^5
- For a type 1 query, 1 \leq x \leq H.
- For a type 2 query, 1 \leq y \leq W.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
H W N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i denotes the i-th query, which is given in one of the following formats:
1 x
2 y
Output
Output Q lines. The i-th line should contain the response to the i-th query.
Sample Input 1
3 4 5 1 2 1 3 3 4 3 1 2 2 5 1 1 1 2 2 2 2 4 1 2
Sample Output 1
2 1 0 1 0
Initially, trash pieces are at cells (1, 2), (1, 3), (3, 4), (3, 1), (2, 2).
In the 1st query, the 1st row contains two pieces of trash at (1, 2) and (1, 3), so report 2. These pieces are then removed; the remaining trash is at (3, 4), (3, 1), (2, 2).
In the 2nd query, the 2nd row contains one piece of trash at (2, 2), so report 1. This piece is then removed; the remaining trash is at (3, 4), (3, 1).
In the 3rd query, the 2nd column contains no trash, so report 0.
In the 4th query, the 4th column contains one piece of trash at (3, 4), so report 1. This piece is then removed; the remaining trash is at (3, 1).
In the 5th query, the 2nd row contains no trash, so report 0.
Sample Input 2
1 2 1 1 2 7 2 1 2 1 2 1 2 1 2 1 2 1 2 1
Sample Output 2
0 0 0 0 0 0 0
Sample Input 3
4 4 16 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4 4 1 4 2 4 3 4 4 7 2 1 1 1 2 2 1 2 2 3 1 3 2 4
Sample Output 3
4 3 3 2 2 1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 頂点 M 辺の単純連結無向グラフ G が与えられます。
G の頂点および辺はそれぞれ頂点 1,2,\ldots,N、辺 1,2,\ldots,M と番号づけられており、
辺 i は頂点 U_i と頂点 V_i を結んでいます。
辺で結ばれている頂点の間は双方向に時間 1 で移動することができます。
また、各頂点は安全か危険かのどちらかであり、その状態は S と D のみからなる長さ N の文字列 S によって与えられます。
具体的には、S の i 文字目 (1\leq i\leq N) が S のとき頂点 i は安全であり、D のとき頂点 i は危険です。
ここで、安全な頂点が 2 つ以上、危険な頂点が 1 つ以上あることが保証されます。
危険な頂点 v それぞれについて、次の値を求めてください。
ある安全な頂点から出発し、v を経由し、 出発した頂点と異なる 安全な頂点へ移動するのにかかる時間としてあり得る最小値
制約
- 3\leq N\leq 2\times 10^5
- N-1\leq M\leq 2\times 10^5
- 1\leq U_i,V_i\leq N
- U_i\neq V_i
- i\neq j ならば \{ U_i,V_i \}\neq \{ U_j,V_j \}
- S は
SとDのみからなる長さ N の文字列 - N,M,U_i,V_i はすべて整数
- G は連結である。
- 安全な頂点が 2 つ以上存在する。
- 危険な頂点が 1 つ以上存在する。
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 U_2 V_2 \vdots U_M V_M S
出力
G 上の危険な頂点の個数を K として、K 行出力せよ。
i 行目 (1\leq i\leq K) には、危険な頂点を頂点番号の昇順に並べた時に i 番目に来る頂点についての答えを出力せよ。
入力例 1
5 5 1 2 1 3 2 3 3 4 4 5 SSDDS
出力例 1
2 3
危険な頂点は(頂点番号について昇順に)頂点 3 と 4 です。
頂点 3 については、頂点 1 \to 頂点 3 \to 頂点 2 と移動するときなどが条件をみたします。
この移動にかかる時間は 2 であり、このときが最小です。
よって、1 行目に 2 を出力します。
頂点 4 については、頂点 1 \to 頂点 3 \to 頂点 4 \to 頂点 5 と移動するときなどが条件をみたします。
この移動にかかる時間は 3 であり、かかる時間が 2 以下の条件をみたす移動方法がないため、このときが最小です。
よって、2 行目に 3 を出力します。
入力例 2
3 2 1 2 2 3 SSD
出力例 2
3
危険な頂点は頂点 3 です。
頂点 1 \to 頂点 2 \to 頂点 3 \to 頂点 2 と移動するときなどが条件をみたします。
この移動にかかる時間は 3 であり、このときが最小です。
頂点 2 \to 頂点 3 \to 頂点 2 などの移動は、「出発した頂点と異なる」という条件をみたしていないことに注意してください。
Score : 450 points
Problem Statement
You are given a simple connected undirected graph G with N vertices and M edges.
The vertices and edges of G are numbered as vertices 1,2,\ldots,N and edges 1,2,\ldots,M, respectively,
and edge i connects vertices U_i and V_i.
You can move bidirectionally between vertices connected by an edge in time 1.
Additionally, each vertex is either safe or dangerous, and this state is given by a string S of length N consisting of S and D.
Specifically, vertex i is safe when the i-th character (1\leq i\leq N) of S is S, and vertex i is dangerous when it is D.
It is guaranteed that there are at least two safe vertices and at least one dangerous vertex.
For each dangerous vertex v, find the following value:
The minimum possible time to start from some safe vertex, pass through v, and move to a safe vertex different from the starting vertex.
Constraints
- 3\leq N\leq 2\times 10^5
- N-1\leq M\leq 2\times 10^5
- 1\leq U_i,V_i\leq N
- U_i\neq V_i
- If i\neq j, then \{ U_i,V_i \}\neq \{ U_j,V_j \}.
- S is a string of length N consisting of
SandD. - N,M,U_i,V_i are all integers.
- G is connected.
- There are at least two safe vertices.
- There is at least one dangerous vertex.
Input
The input is given from Standard Input in the following format:
N M U_1 V_1 U_2 V_2 \vdots U_M V_M S
Output
Let K be the number of dangerous vertices in G, and print K lines.
On the i-th line (1\leq i\leq K), print the answer for the i-th dangerous vertex when the dangerous vertices are arranged in ascending order of vertex number.
Sample Input 1
5 5 1 2 1 3 2 3 3 4 4 5 SSDDS
Sample Output 1
2 3
The dangerous vertices are (in ascending order of vertex number) vertices 3 and 4.
For vertex 3, moving from vertex 1 \to vertex 3 \to vertex 2 (for example) satisfies the condition.
The time required for this movement is 2, and this is the minimum.
Therefore, print 2 on the 1st line.
For vertex 4, moving from vertex 1 \to vertex 3 \to vertex 4 \to vertex 5 (for example) satisfies the condition.
The time required for this movement is 3, and there is no way to move that satisfies the condition with time 2 or less, so this is the minimum.
Therefore, print 3 on the 2nd line.
Sample Input 2
3 2 1 2 2 3 SSD
Sample Output 2
3
The dangerous vertex is vertex 3.
Moving from vertex 1 \to vertex 2 \to vertex 3 \to vertex 2 (for example) satisfies the condition.
The time required for this movement is 3, and this is the minimum.
Note that movements such as vertex 2 \to vertex 3 \to vertex 2 do not satisfy the condition that the destination is "different from the starting vertex".
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