Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
正整数 N と、長さ N の 0 または 1 からなる数列 A=(A_1,A_2,\dots,A_N) が与えられます。
長さ N の英大文字のみからなる文字列 S であって、以下の操作を 0 回以上好きな回数行うことで A に 0 が含まれないようにできるものを 良い文字列 と呼びます。ここで、S_i (1\leq i\leq N) で S の i 文字目を表し、S_{N+1}=S_1,S_{N+2}=S_2,A_{N+1}=A_1 とします。
- 次のうちどちらかを選び、実行する。
- 1\leq i\leq N なる整数 i であって S_i=
A
、S_{i+1}=R
、S_{i+2}=C
となるものを選び、A_i,A_{i+1} を 1 に置き換える。 - 1\leq i\leq N なる整数 i であって S_{i+2}=
A
、S_{i+1}=R
、S_i=C
となるものを選び、A_i,A_{i+1} を 1 に置き換える。
- 1\leq i\leq N なる整数 i であって S_i=
良い文字列が存在するか判定してください。
制約
- 3\leq N\leq 200000
- A_i\in \lbrace 0,1 \rbrace (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
良い文字列が存在するならば Yes
と、存在しないならば No
と出力せよ。
正誤判定器は大文字と小文字を区別せず、どちらも受理する。例えば、答えが Yes
となるときに yes
や YES
、yEs
などと出力しても正解と判定される。
入力例 1
12 0 1 0 1 1 1 1 0 1 1 1 0
出力例 1
Yes
例えば RARCARCCRAGC
は良い文字列です。以下のように操作することで、A の全ての要素を 1 にすることが可能であるからです。
- はじめ、A=(0,1,0,1,1,1,1,0,1,1,1,0) である。
- i=2 として前者の操作をする。A=(0,1,1,1,1,1,1,0,1,1,1,0) となる。
- i=5 として前者の操作をする。A=(0,1,1,1,1,1,1,0,1,1,1,0) となる。
- i=8 として後者の操作をする。A=(0,1,1,1,1,1,1,1,1,1,1,0) となる。
- i=12 として後者の操作をする。A=(1,1,1,1,1,1,1,1,1,1,1,1) となる。
良い文字列が存在するので、Yes
と出力してください。
入力例 2
3 0 0 0
出力例 2
No
良い文字列は存在しません。
入力例 3
29 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 3
Yes
操作するまでもなく A に 0 は含まれていないので、英大文字からなる長さ 29 の文字列はすべて良い文字列です。
Score : 400 points
Problem Statement
You are given a positive integer N and a sequence A=(A_1,A_2,\dots,A_N) of length N, consisting of 0 and 1.
We call a string S of length N, consisting only of uppercase English letters, a good string if it is possible to perform the following operation any number of times (possibly zero) so that the sequence A contains no 0. Here, S_i (1\leq i\leq N) denotes the i-th character of S, and we define S_{N+1}=S_1, S_{N+2}=S_2, and A_{N+1}=A_1.
- Perform one of the following operations:
- Choose an integer i with 1\leq i\leq N such that S_i=
A
, S_{i+1}=R
, and S_{i+2}=C
, and replace each of A_i and A_{i+1} with 1. - Choose an integer i with 1\leq i\leq N such that S_{i+2}=
A
, S_{i+1}=R
, and S_i=C
, and replace each of A_i and A_{i+1} with 1.
- Choose an integer i with 1\leq i\leq N such that S_i=
Determine whether there exists a good string.
Constraints
- 3\leq N\leq 200000
- A_i\in \lbrace 0,1 \rbrace (1\leq i\leq 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_N
Output
If there exists a good string, print Yes
; otherwise, print No
.
The judge is case-insensitive; for example, if the correct answer is Yes
, outputs such as yes
, YES
, or yEs
will also be accepted.
Sample Input 1
12 0 1 0 1 1 1 1 0 1 1 1 0
Sample Output 1
Yes
For example, RARCARCCRAGC
is a good string. This is because it is possible to change all elements of A to 1 by performing the following operations:
- Initially, A=(0,1,0,1,1,1,1,0,1,1,1,0).
- Perform the first operation with i=2. Then, A=(0,1,1,1,1,1,1,0,1,1,1,0).
- Perform the first operation with i=5. Then, A=(0,1,1,1,1,1,1,0,1,1,1,0).
- Perform the second operation with i=8. Then, A=(0,1,1,1,1,1,1,1,1,1,1,0).
- Perform the second operation with i=12. Then, A=(1,1,1,1,1,1,1,1,1,1,1,1).
Since there exists a good string, output Yes
.
Sample Input 2
3 0 0 0
Sample Output 2
No
Good strings do not exist.
Sample Input 3
29 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 3
Yes
Since A already contains no 0, every string of length 29 consisting of uppercase English letters is a good string.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
フェネックとすぬけくんがボードゲームで遊んでいます。
正整数 N と、長さ N の正整数列 A=(A_1,A_2,\dots,A_N) が与えられます。また、集合 S があり、はじめ S は空集合です。
フェネックとすぬけくんは、フェネックから順に次の一連の操作を交互に行います。
- 1\leq A_i なる i を選ぶ。A_i から 1 を引き、i\notin S ならば S に i を追加する。
- S=\lbrace 1,2,\dots,N \rbrace となっていたら、最後に操作したプレイヤーを勝者としてゲームを終了する。
なお、勝者が決定してゲームが終了するまでは常にプレイヤーは操作が可能である(1\leq A_i なる i は存在する)ことが証明できます。
フェネックとすぬけくんは、どちらも自身が勝者になることを目指して最適な行動を行います。どちらが勝者になるか判定してください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9 (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
勝者がフェネックならば Fennec
と、すぬけくんならば Snuke
と出力せよ。
正誤判定器は大文字と小文字を区別せず、どちらも受理する。例えば、答えが Fennec
となるときに fennec
や FENNEC
、fEnNeC
などと出力しても正解と判定される。
入力例 1
3 1 9 2
出力例 1
Fennec
例えば、ゲームは以下のように進行します。
- はじめ、A=(1,9,2) で、S は空集合である。
- フェネックが 2 を選ぶ。A=(1,8,2)、S=\lbrace 2 \rbrace となる。
- すぬけくんが 2 を選ぶ。A=(1,7,2)、S=\lbrace 2 \rbrace となる。
- フェネックが 1 を選ぶ。A=(0,7,2)、S=\lbrace 1,2 \rbrace となる。
- すぬけくんが 2 を選ぶ。A=(0,6,2)、S=\lbrace 1,2 \rbrace となる。
- フェネックが 3 を選ぶ。A=(0,6,1)、S=\lbrace 1,2,3 \rbrace となる。フェネックを勝者としてゲームを終了する。
これは両者が最適に行動しているとは限りませんが、両者が最適に行動してもフェネックが勝者となることが示せます。
入力例 2
2 25 29
出力例 2
Snuke
入力例 3
6 1 9 2 25 2 9
出力例 3
Snuke
Score : 600 points
Problem Statement
Fennec and Snuke are playing a board game.
You are given a positive integer N and a sequence A=(A_1,A_2,\dots,A_N) of positive integers of length N. Also, there is a set S, which is initially empty.
Fennec and Snuke take turns performing the following operation in order, starting with Fennec.
- Choose an index i such that 1\leq A_i. Subtract 1 from A_i, and if i\notin S, add i to S.
- If S=\lbrace 1,2,\dots,N \rbrace, the game ends and the player who performed the last operation wins.
Note that it can be proven that until a winner is determined and the game ends, players can always make a move (there exists some i such that 1\leq A_i).
Both Fennec and Snuke play optimally to win. Determine who will win.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq A_i\leq 10^9 (1\leq i\leq 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_N
Output
Print Fennec
if Fennec wins, or Snuke
if Snuke wins.
The judge is case-insensitive; for example, if the correct answer is Fennec
, outputs such as fennec
, FENNEC
, or fEnNeC
will also be accepted.
Sample Input 1
3 1 9 2
Sample Output 1
Fennec
For example, the game may proceed as follows:
- Initially, A=(1,9,2) and S is empty.
- Fennec chooses index 2. Then, A=(1,8,2) and S=\lbrace 2 \rbrace.
- Snuke chooses index 2. Then, A=(1,7,2) and S=\lbrace 2 \rbrace.
- Fennec chooses index 1. Then, A=(0,7,2) and S=\lbrace 1,2 \rbrace.
- Snuke chooses index 2. Then, A=(0,6,2) and S=\lbrace 1,2 \rbrace.
- Fennec chooses index 3. Then, A=(0,6,1) and S=\lbrace 1,2,3 \rbrace. The game ends with Fennec declared the winner.
This sequence of moves may not be optimal; however, it can be shown that even when both players play optimally, Fennec will win.
Sample Input 2
2 25 29
Sample Output 2
Snuke
Sample Input 3
6 1 9 2 25 2 9
Sample Output 3
Snuke
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
この問題はインタラクティブな問題です。
正整数 N が与えられます。
すぬけくんは、(1,2,\dots,N) を並び替えた正整数列 P=(P_1,P_2,\dots,P_N) と、長さ N の正整数列 A=(A_1,A_2,\dots,A_N) を隠し持っています。ここで、P_1<P_2 が保証されます。
あなたは、すぬけくんに 2N 回までクエリを送ることができます。クエリは以下のようなものです。
- 1\leq s,t\leq N なる相異なる正整数 s,t を指定し、\displaystyle \sum_{i=\min(P_s,P_t)}^{\max(P_s,P_t)}A_i の値を教えてもらう。
P,A を特定してください。
制約
- 3\leq N\leq 5000
- 1\leq P_i\leq N (1\leq i\leq N)
- P_i\ne P_j (i\ne j)
- P_1<P_2
- 1\leq A_i\leq 10^9 (1\leq i\leq N)
- N,P_i,A_i は整数
- P,A はジャッジとの対話前に決定される
入出力
この問題はインタラクティブな問題です。
最初に、N が標準入力から与えられます。
N
次に、あなたはすぬけくんに 2N 回以下クエリを送ることができます。相異なる正整数 s,t を指定してクエリを送るとき、以下のように出力してください。末尾に改行を入れることを忘れないでください。
? s t
クエリを送ると、すぬけくんからの返答が以下の形式で標準入力から与えられます。
X
ここで、X は整数で、
- X\ne -1 のとき \displaystyle \sum_{i=\min(P_s,P_t)}^{\max(P_s,P_t)}A_i の値が X であることを表します。
- X=-1 のとき、s,t が制約を満たしていないか、クエリを送った回数が 2N 回を超えたことをします。
- このとき、プログラムはすでに不正解とみなされています。ただちにプログラムを終了してください。
P と A を特定することができたら、答えを以下の形式で出力してください。これはクエリの回数には計上されません。
! P_1 P_2 \dots P_N A_1 A_2 \dots A_N
特に P_1<P_2 であることに注意してください。 この出力の後、ただちにプログラムを終了してください。
上記のいずれの形式にも当てはまらない出力をした場合は、-1
が標準入力から与えられます。
-1
このときも、プログラムはすでに不正解とみなされています。ただちにプログラムを終了してください。
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力をflushしてください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 解答を出力したとき、または
-1
を標準入力から受け取ったとき、ただちにプログラムを終了してください。そうしなかった場合の判定結果は不定です。 - 余計な改行は不正なフォーマットの出力とみなされることに注意してください。
- この問題のジャッジシステムは適応的ではありません。 つまり、P と A はジャッジとの対話前に決定され、いかなるタイミングでも変更されることはありません。
入出力例
N=6,P=(2,4,6,5,3,1),A=(1,9,2,25,2,9) のときの対話の一例を示します。
入力 | 出力 | 説明 |
---|---|---|
6 |
まず整数 N が標準入力から与えられます。 | |
? 1 2 |
s=1,t=2 としてすぬけくんにクエリを送ります。 | |
36 |
出力は制約を満たしているので、\displaystyle \sum_{i=\min(P_1,P_2)}^{\max(P_1,P_2)}A_i の値である 36 が与えられます。 | |
? 2 5 |
s=2,t=5 としてすぬけくんにクエリを送ります。 | |
27 |
出力は制約を満たしているので、\displaystyle \sum_{i=\min(P_2,P_5)}^{\max(P_2,P_5)}A_i の値である 27 が与えられます。 | |
! 2 4 6 5 3 1 1 9 2 25 2 9 |
P と A が特定できたことを報告します。この出力の後、ただちにプログラムを終了することで正解と判定されます。 |
これは対話の一例であることに注意してください。特に、ここまでのクエリとその返答で P と A を確実に特定できるとは限りません。
Score : 600 points
Problem Statement
This problem is interactive.
You are given a positive integer N.
Snuke secretly holds a permutation P=(P_1,P_2,\dots,P_N) of (1,2,\dots,N) and a sequence A=(A_1,A_2,\dots,A_N) of positive integers of length N. It is guaranteed that P_1<P_2.
You may send up to 2N queries to Snuke. A query is of the following form:
- Specify two distinct positive integers s and t with 1\leq s,t\leq N, and you will be given the value of \displaystyle \sum_{i=\min(P_s,P_t)}^{\max(P_s,P_t)}A_i.
Determine P and A.
Constraints
- 3\leq N\leq 5000
- 1\leq P_i\leq N (1\leq i\leq N)
- P_i\ne P_j for i\ne j.
- P_1<P_2
- 1\leq A_i\leq 10^9 (1\leq i\leq N)
- N, P_i, and A_i are integers.
- P and A are fixed before the interaction with the judge.
Input/Output
This problem is interactive.
First, N is given from Standard Input:
N
Next, you may send at most 2N queries to Snuke. When sending a query by specifying two distinct positive integers s,t, output in the following format. Do not forget to include a newline at the end.
? s t
After sending a query, you will receive a response from Snuke in the following format:
X
Here, X is an integer:
- If X\ne -1, then X is the value of \displaystyle \sum_{i=\min(P_s,P_t)}^{\max(P_s,P_t)}A_i.
- If X=-1, then either s,t do not satisfy the constraints or you have sent more than 2N queries.
- In this case, your program is judged as incorrect and must terminate immediately.
Once you have determined P and A, output your answer in the following format. This output does not count as a query.
! P_1 P_2 \dots P_N A_1 A_2 \dots A_N
Note that P_1<P_2. After this output, terminate your program immediately.
If you output anything that does not follow the above formats, -1
will be given as input.
-1
In that case, your program is judged as incorrect and must terminate immediately.
Notes
- After every output, be sure to end with a newline and flush the standard output. Failure to do so may result in a TLE.
- When you output your answer, or receive
-1
from standard input, terminate your program immediately. Otherwise, the outcome is indeterminate. - Note that extra newlines may be considered as an output format error.
- The judge system for this problem is not adaptive. That is, P and A are determined before the interaction with the judge and will not be changed at any point.
Sample Interaction
For N=6, P=(2,4,6,5,3,1), and A=(1,9,2,25,2,9), here is an example of an interaction.
Input | Output | Explanation |
---|---|---|
6 |
First, the integer N is given from standard input. | |
? 1 2 |
A query is sent to Snuke with s=1, t=2. | |
36 |
Since the query satisfies the constraints, the value 36, which is equal to \displaystyle \sum_{i=\min(P_1,P_2)}^{\max(P_1,P_2)}A_i, is returned. | |
? 2 5 |
A query is sent to Snuke with s=2, t=5. | |
27 |
Since the query satisfies the constraints, the value 27, which is equal to \displaystyle \sum_{i=\min(P_2,P_5)}^{\max(P_2,P_5)}A_i, is returned. | |
! 2 4 6 5 3 1 1 9 2 25 2 9 |
You report that P and A have been determined. After this output, the program should terminate immediately, and it will be judged as correct. |
Note that this is just one example of an interaction. In particular, it is not guaranteed that P and A can be uniquely determined from the queries and responses shown above.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
正の有理数 x に対し、f(x) を以下のように定義します。
x を互いに素な正整数 P,Q を用いて \dfrac{P}{Q} と表したときの P\times Q の値
正整数 N と、長さ N-1 の正整数列 A=(A_1,A_2,\dots,A_{N-1}) が与えられます。
以下の条件をすべて満たす長さ N の正整数列 S=(S_1,S_2,\dots,S_N) を 良い数列 と呼ぶことにします。
- 1\leq i\leq N-1 なるすべての整数 i について、f\left(\dfrac{S_i}{S_{i+1}}\right)=A_i が成り立つ。
- \gcd(S_1,S_2,\dots,S_N)=1
数列の スコア を、その数列に含まれる要素の総積と定義します。
良い数列の個数は有限個であることが証明できます。良い数列すべてに対するスコアの総和を 998244353 で割った余りを求めてください。
制約
- 2\leq N\leq 1000
- 1\leq A_i\leq 1000 (1\leq i\leq N-1)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_{N-1}
出力
良い数列すべてに対するスコアの総和を 998244353 で割った余りを出力せよ。
入力例 1
6 1 9 2 2 9
出力例 1
939634344
例えば (2,2,18,9,18,2) や (18,18,2,1,2,18) は良い数列で、スコアはどちらも 23328 です。
良い数列は全部で 16 個存在し、それらすべてに対するスコアの総和は 939634344 です。
入力例 2
2 9
出力例 2
18
良い数列は 2 個存在し、どちらもスコアは 9 です。
入力例 3
25 222 299 229 22 999 922 99 992 22 292 222 229 992 922 22 992 222 222 99 29 92 999 2 29
出力例 3
192457116
総和を 998244353 で割った余りを求めることを忘れないでください。
Score : 600 points
Problem Statement
For a positive rational number x, define f(x) as follows:
Express x as \dfrac{P}{Q} using coprime positive integers P and Q. f(x) is defined as the value P\times Q.
You are given a positive integer N and a sequence A=(A_1,A_2,\dots,A_{N-1}) of positive integers of length N-1.
We call a sequence S=(S_1,S_2,\dots,S_N) of positive integers of length N a good sequence if it satisfies all of the following conditions:
- For every integer i with 1\leq i\leq N-1, it holds that f\left(\dfrac{S_i}{S_{i+1}}\right)=A_i.
- \gcd(S_1,S_2,\dots,S_N)=1.
Define the score of a sequence as the product of all its elements.
It can be proved that there are finitely many good sequences. Find the sum, modulo 998244353, of the scores of all good sequences.
Constraints
- 2\leq N\leq 1000
- 1\leq A_i\leq 1000 (1\leq i\leq N-1)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_{N-1}
Output
Print the sum, modulo 998244353, of the scores of all good sequences.
Sample Input 1
6 1 9 2 2 9
Sample Output 1
939634344
For example, both (2,2,18,9,18,2) and (18,18,2,1,2,18) are good sequences, and both have a score of 23328.
There are a total of 16 good sequences, and the sum of the scores of all of them is 939634344.
Sample Input 2
2 9
Sample Output 2
18
There are 2 good sequences, both with a score of 9.
Sample Input 3
25 222 299 229 22 999 922 99 992 22 292 222 229 992 922 22 992 222 222 99 29 92 999 2 29
Sample Output 3
192457116
Do not forget to compute the sum modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 800 点
問題文
整数 W,H,L,R,D,U が与えられます。
2 次元平面上に京都の町があります。
京都の町には、以下の条件をすべて満たす格子点 (x,y) に 1 つずつ区画が存在します。それ以外の点に区画はありません。
- 0\leq x\leq W
- 0\leq y\leq H
- x\lt L または R\lt x または y\lt D または U\lt y
すぬけくんは、以下のように京都の町を旅しました。
- まず、区画を 1 つ選んでそこに立つ。
- その後、0 回以上好きな回数以下の操作を行う。
- x 軸正方向または y 軸正方向に 1 移動する。ただし、移動後の点にも区画がなくてはならない。
すぬけくんが通った経路としてあり得るものの個数を 998244353 で割った余りを求めてください。
制約
- 0\leq L\leq R\leq W\leq 10^6
- 0\leq D\leq U\leq H\leq 10^6
- 区画が 1 つ以上存在する
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
W H L R D U
出力
答えを出力せよ。
入力例 1
4 3 1 2 2 3
出力例 1
192
以下のような経路が考えられます。ここで、経路は通った格子点を順に並べることで表しています。
- (3,0)
- (0,0)\rightarrow (1,0)\rightarrow (2,0)\rightarrow (2,1)\rightarrow (3,1)\rightarrow (3,2)\rightarrow (4,2)\rightarrow (4,3)
- (0,1)\rightarrow (0,2)
考えられる経路の個数は 192 個です。
入力例 2
10 12 4 6 8 11
出力例 2
4519189
入力例 3
192 25 0 2 0 9
出力例 3
675935675
経路の個数を 998244353 で割った余りを求めることを忘れないでください。
Score : 800 points
Problem Statement
You are given integers W,H,L,R,D,U.
A town of Kyoto is on the two-dimensional plane.
In the town, there is exactly one block at each lattice point (x,y) that satisfies all of the following conditions. There are no blocks at any other points.
- 0\leq x\leq W
- 0\leq y\leq H
- x<L or R<x or y<D or U<y
Snuke traveled through the town as follows.
- First, he chooses one block and stands there.
- Then, he performs the following operation any number of times (possibly zero):
- Move one unit in the positive direction of the x-axis or the positive direction of the y-axis. However, the point after moving must also have a block.
Print the number, modulo 998244353, of possible paths that Snuke could have taken.
Constraints
- 0\leq L\leq R\leq W\leq 10^6
- 0\leq D\leq U\leq H\leq 10^6
- There is at least one block.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
W H L R D U
Output
Print the answer.
Sample Input 1
4 3 1 2 2 3
Sample Output 1
192
The following are examples of possible paths. Here, a path is represented by listing the lattice points visited in order.
- (3,0)
- (0,0)\rightarrow (1,0)\rightarrow (2,0)\rightarrow (2,1)\rightarrow (3,1)\rightarrow (3,2)\rightarrow (4,2)\rightarrow (4,3)
- (0,1)\rightarrow (0,2)
There are 192 possible paths.
Sample Input 2
10 12 4 6 8 11
Sample Output 2
4519189
Sample Input 3
192 25 0 2 0 9
Sample Output 3
675935675
Do not forget to print the number of paths modulo 998244353.