実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
縦 30 行、横 30 列のマス目に区切られた長方形の板チョコがあり、各マスは 1 片のチョコレートに対応します。
この板チョコに対して、以下の操作を順に行います。
- 一番下の 1 行を取り除く操作を B 回行う。
- 一番右の 1 列を取り除く操作を R 回行う。
全ての操作が終わった時点で、板チョコには何片のチョコレートが残っていますか?
制約
- 入力は全て整数
- 1 \le B,R \le 29
入力
入力は以下の形式で標準入力から与えられる。
B R
出力
答えを出力せよ。
入力例 1
8 8
出力例 1
484
- まず、一番下の 1 行を取り除く操作を 8 回行う。
- これが終わった時点で、チョコレートは縦 22 行、横 30 列です。
- 一番右の 1 列を取り除く操作を 8 回行う。
- これが終わった時点で、チョコレートは縦 22 行、横 22 列です。
- 残っているチョコレートは 484 片です。
入力例 2
10 24
出力例 2
120
入力例 3
21 7
出力例 3
207
Score : 100 points
Problem Statement
There is a rectangular chocolate bar divided into a grid with 30 rows and 30 columns, where each cell corresponds to one piece of chocolate.
The following operations are performed on this chocolate bar in order.
- Perform the operation of removing the bottommost row B times.
- Perform the operation of removing the rightmost column R times.
How many pieces of chocolate remain in the chocolate bar after all operations are finished?
Constraints
- All input values are integers.
- 1 \le B,R \le 29
Input
The input is given from Standard Input in the following format:
B R
Output
Print the answer.
Sample Input 1
8 8
Sample Output 1
484
- First, the operation of removing the bottommost row is performed 8 times.
- After this, the chocolate bar has 22 rows and 30 columns.
- Then, the operation of removing the rightmost column is performed 8 times.
- After this, the chocolate bar has 22 rows and 22 columns.
- The number of remaining pieces of chocolate is 484.
Sample Input 2
10 24
Sample Output 2
120
Sample Input 3
21 7
Sample Output 3
207
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
あなたはビリヤードのナインボールで遊んでいます。
このゲームでは、 1 から 9 までの番号がついたボールを 1 つずつ、計 9 個のボールを使用します。
現在、卓上には K 個のボールが残っており、各ボールの番号は A_1,A_2,\dots,A_K です。
次はあなたの手番であり、あなたは卓上に残っている最小の番号のボールを狙わなければなりません。ただし、卓上から 9 番のボールが無くなっている場合は、既にゲームが終了しています。
あなたが手番で狙うべきボールの番号を出力してください。 ただし、卓上に 9 番のボールがない場合、ゲームが既に終わっているため、代わりに -1 を出力してください。
制約
- 入力は全て整数
- 1 \le K \le 9
- 1 \le A_i \le 9
- A_i は相異なる
入力
入力は以下の形式で標準入力から与えられる。
K A_1 A_2 \dots A_K
出力
答えを出力せよ。
入力例 1
5 6 4 2 9 8
出力例 1
2
卓上に 2,4,6,8,9 番のボールが残っており、次に狙うべきは 2 番のボールです。
入力例 2
4 8 2 4 6
出力例 2
-1
卓上に 2,4,6,8 番のボールが残っていますが、 9 番のボールが残っていません。
この場合はゲームが既に終わっているため、答えは -1 です。
入力例 3
3 7 9 6
出力例 3
6
Score : 200 points
Problem Statement
You are playing nine-ball, a billiards game.
This game uses a total of 9 balls, one for each number from 1 to 9.
Currently, K balls remain on the table, and their numbers are A_1,A_2,\dots,A_K.
It is now your turn, and you must aim at the lowest-numbered ball remaining on the table. However, if the ball numbered 9 is no longer on the table, the game has already ended.
Print the number of the ball you should aim at on your turn. However, if the ball numbered 9 is not on the table, the game has already ended, so print -1 instead.
Constraints
- All input values are integers.
- 1 \le K \le 9
- 1 \le A_i \le 9
- The values A_i are distinct.
Input
The input is given from Standard Input in the following format:
K A_1 A_2 \dots A_K
Output
Print the answer.
Sample Input 1
5 6 4 2 9 8
Sample Output 1
2
The balls numbered 2,4,6,8,9 remain on the table, and the ball you should aim at next is the one numbered 2.
Sample Input 2
4 8 2 4 6
Sample Output 2
-1
The balls numbered 2,4,6,8 remain on the table, but the ball numbered 9 does not.
In this case, the game has already ended, so the answer is -1.
Sample Input 3
3 7 9 6
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
正の整数からなる長さ N の列 A=(A _ 1,A _ 2,\ldots,A _ N),B=(B _ 1,B _ 2,\ldots,B _ N) が与えられます。
列 A に対して次の操作をちょうど \mathbf{1} 回だけ行います。
- A の要素を一つ選んで取り出し、残った列の好きな位置に挿入する。より厳密には、以下の 2 つの操作を順に行う。
- 1\le i\le N を満たす整数 i を一つ選び、長さ N-1 の列 X=(X _ 1,X _ 2,\ldots,X _ {N-1}) を (A _ 1,A _ 2,\ldots,A _ {i-1},A _ {i+1},A _ {i+2},\ldots,A _ N) とし、整数 Y を A _ i とする。
- 1\le j\le N を満たす整数 j を一つ選び、長さ N の列 (X _ 1,X _ 2,\ldots,X _ {j-1},Y,X _ j,X _ {j+1},\ldots,X _ {N-1}) を新たな A とする。
要素を挿入するとき、列の左端や右端に挿入してもよいことに注意してください。
操作ののち A を B と一致させることができるか判定してください。
制約
- 1\le N\le2\times10 ^ 5
- 1\le A _ i\le N\ (1\le i\le N)
- 1\le B _ i\le N\ (1\le i\le N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ N
出力
操作をちょうど 1 回行って A と B を一致させられる場合 Yes と、そうでない場合 No と一行に出力せよ。
入力例 1
6 1 4 1 4 2 1 1 4 1 1 4 2
出力例 1
Yes
例えば、A _ 6 を取り出し、残った列の先頭から 3 項目と 4 項目の間に挿入することで、操作を行ったあとの A を (1,4,1,1,4,2) とすることができます。
よって、Yes と出力してください。
入力例 2
2 1 1 1 2
出力例 2
No
A にどのような操作を行っても B と一致させることはできません。
入力例 3
3 1 2 3 1 2 3
出力例 3
Yes
取り出した要素を、もともとあった位置に挿入しても構いません。
入力例 4
30 25 11 21 15 30 20 17 19 17 21 10 13 22 10 22 14 30 25 17 25 11 19 27 27 15 18 16 16 20 16 25 11 15 30 20 17 19 17 21 10 13 22 10 22 14 30 25 17 25 11 19 27 27 15 21 18 16 16 20 16
出力例 4
Yes
Score : 300 points
Problem Statement
You are given sequences A=(A _ 1,A _ 2,\ldots,A _ N) and B=(B _ 1,B _ 2,\ldots,B _ N) of length N consisting of positive integers.
The following operation is performed on the sequence A exactly once.
- Choose one element of A, take it out, and insert it at any position in the remaining sequence. More formally, perform the following two steps in order.
- Choose an integer i satisfying 1\le i\le N, let the sequence X=(X _ 1,X _ 2,\ldots,X _ {N-1}) of length N-1 be (A _ 1,A _ 2,\ldots,A _ {i-1},A _ {i+1},A _ {i+2},\ldots,A _ N), and let the integer Y be A _ i.
- Choose an integer j satisfying 1\le j\le N, and let the sequence (X _ 1,X _ 2,\ldots,X _ {j-1},Y,X _ j,X _ {j+1},\ldots,X _ {N-1}) of length N be the new A.
Note that the element may be inserted at the left end or the right end of the sequence.
Determine whether it is possible to make A equal to B after the operation.
Constraints
- 1\le N\le2\times10 ^ 5
- 1\le A _ i\le N\ (1\le i\le N)
- 1\le B _ i\le N\ (1\le i\le N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ N
Output
Print Yes in one line if it is possible to make A equal to B by performing the operation exactly once, and No otherwise.
Sample Input 1
6 1 4 1 4 2 1 1 4 1 1 4 2
Sample Output 1
Yes
For example, by taking out A _ 6 and inserting it between the 3-rd and 4-th elements of the remaining sequence, A after the operation becomes (1,4,1,1,4,2).
Thus, print Yes.
Sample Input 2
2 1 1 1 2
Sample Output 2
No
No matter what operation is performed on A, it cannot be made equal to B.
Sample Input 3
3 1 2 3 1 2 3
Sample Output 3
Yes
The element taken out may be inserted back at its original position.
Sample Input 4
30 25 11 21 15 30 20 17 19 17 21 10 13 22 10 22 14 30 25 17 25 11 19 27 27 15 18 16 16 20 16 25 11 15 30 20 17 19 17 21 10 13 22 10 22 14 30 25 17 25 11 19 27 27 15 21 18 16 16 20 16
Sample Output 4
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
正の整数からなる長さ N の列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。
列 A に対して次の操作をちょうど \mathbf{1} 回だけ行います。
- A の要素を一つ選んで取り出し、残った列の元あった場所かそれより前に挿入する。より厳密には、以下の 2 つの操作を順に行う。
- 1\le i\le N を満たす整数 i を一つ選び、長さ N-1 の列 X=(X _ 1,X _ 2,\ldots,X _ {N-1}) を (A _ 1,A _ 2,\ldots,A _ {i-1},A _ {i+1},A _ {i+2},\ldots,A _ N) とし、整数 Y を A _ i とする。
- 1\le j\le i を満たす整数 j を一つ選び、長さ N の列 (X _ 1,X _ 2,\ldots,X _ {j-1},Y,X _ j,X _ {j+1},\ldots,X _ {N-1}) を新たな A とする。
要素を挿入するとき、列の左端や右端に挿入できる場合があることに注意してください。
操作の結果として得られる列が何通りあるか求めてください。
操作で選んだ (i,j) が異なる場合でも、列として等しい場合は同じものとして数えます。
制約
- 1\le N\le2\times10 ^ 5
- 1\le A _ i\le N\ (1\le i\le N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A _ 1 A _ 2 \ldots A _ N
出力
答えを出力せよ。
入力例 1
6 1 4 1 4 2 1
出力例 1
12
例えば、i=6,j=4 として操作を行うことで (1,4,1,1,4,2) を得ることができます。
他にも、以下の 11 通りの列を得ることができます。
- (1,1,4,1,4,2)
- (1,1,4,4,2,1)
- (1,2,4,1,4,1)
- (1,4,1,2,4,1)
- (1,4,1,4,1,2)
- (1,4,1,4,2,1)
- (1,4,2,1,4,1)
- (1,4,4,1,2,1)
- (2,1,4,1,4,1)
- (4,1,1,4,2,1)
- (4,1,4,1,2,1)
よって、12 を出力してください。
入力例 2
5 1 1 1 1 1
出力例 2
1
異なる (i,j) によって得られる列でも、列として等しければ 1 つとして数えることに注意してください。
入力例 3
30 24 22 18 20 14 22 14 14 26 14 26 26 20 22 24 18 22 22 14 24 18 14 14 24 22 18 18 26 14 20
出力例 3
303
Score : 400 points
Problem Statement
You are given a sequence A=(A _ 1,A _ 2,\ldots,A _ N) of length N consisting of positive integers.
The following operation is performed on the sequence A exactly once.
- Choose one element of A, take it out, and insert it into the remaining sequence at its original position or earlier. More formally, perform the following two steps in order.
- Choose an integer i satisfying 1\le i\le N, let the sequence X=(X _ 1,X _ 2,\ldots,X _ {N-1}) of length N-1 be (A _ 1,A _ 2,\ldots,A _ {i-1},A _ {i+1},A _ {i+2},\ldots,A _ N), and let the integer Y be A _ i.
- Choose an integer j satisfying 1\le j\le i, and let the sequence (X _ 1,X _ 2,\ldots,X _ {j-1},Y,X _ j,X _ {j+1},\ldots,X _ {N-1}) of length N be the new A.
Note that in some cases the element can be inserted at the left end or the right end of the sequence.
Find the number of distinct sequences that can be obtained as a result of the operation.
Even if the pairs (i,j) chosen in the operation are different, the results are counted as the same if they are equal as sequences.
Constraints
- 1\le N\le2\times10 ^ 5
- 1\le A _ i\le N\ (1\le i\le N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A _ 1 A _ 2 \ldots A _ N
Output
Print the answer.
Sample Input 1
6 1 4 1 4 2 1
Sample Output 1
12
For example, performing the operation with i=6,j=4 yields (1,4,1,1,4,2).
Besides this, the following 11 sequences can be obtained.
- (1,1,4,1,4,2)
- (1,1,4,4,2,1)
- (1,2,4,1,4,1)
- (1,4,1,2,4,1)
- (1,4,1,4,1,2)
- (1,4,1,4,2,1)
- (1,4,2,1,4,1)
- (1,4,4,1,2,1)
- (2,1,4,1,4,1)
- (4,1,1,4,2,1)
- (4,1,4,1,2,1)
Thus, print 12.
Sample Input 2
5 1 1 1 1 1
Sample Output 2
1
Note that sequences obtained by different pairs (i,j) are counted as one if they are equal as sequences.
Sample Input 3
30 24 22 18 20 14 22 14 14 26 14 26 26 20 22 24 18 22 22 14 24 18 14 14 24 22 18 18 26 14 20
Sample Output 3
303
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 450 点
問題文
長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられるので、(空でない)連続部分列であって総和が L 以上 R 以下であるものの個数を答えてください。
ただし、 2 つの連続部分列が列として一致していたとしても、それらを取り出した位置が異なるのであれば、それらは区別して数えるものとします。
制約
- 入力は全て整数
- 1 \le N \le 3 \times 10^5
- -10^{18} \le L \le R \le 10^{18}
- -10^{12} \le A_i \le 10^{12}
入力
入力は以下の形式で標準入力から与えられる。
N L R A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
5 -2 3 3 -1 -4 -1 5
出力例 1
8
この入力では、 l 項目から r 項目までからなる連続部分列が条件を満たすような (l,r) の組は以下の 8 個です。
- (1,1),(1,2),(1,3),(1,5),(2,2),(2,5),(3,5),(4,4)
入力例 2
6 0 0 0 0 0 0 0 0
出力例 2
21
L=R である場合もあります。
入力例 3
10 -126566 92319 11087 76694 -94782 -28589 -77903 -75549 31253 60353 -69062 -49587
出力例 3
34
Score : 450 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_N) of length N. Find the number of (non-empty) contiguous subsequences whose sum is between L and R, inclusive.
Here, even if two contiguous subsequences are equal as sequences, they are counted separately if they are taken from different positions.
Constraints
- All input values are integers.
- 1 \le N \le 3 \times 10^5
- -10^{18} \le L \le R \le 10^{18}
- -10^{12} \le A_i \le 10^{12}
Input
The input is given from Standard Input in the following format:
N L R A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
5 -2 3 3 -1 -4 -1 5
Sample Output 1
8
For this input, there are 8 pairs (l,r) such that the contiguous subsequence from the l-th through r-th elements satisfies the condition:
- (1,1),(1,2),(1,3),(1,5),(2,2),(2,5),(3,5),(4,4)
Sample Input 2
6 0 0 0 0 0 0 0 0
Sample Output 2
21
It is possible that L=R.
Sample Input 3
10 -126566 92319 11087 76694 -94782 -28589 -77903 -75549 31253 60353 -69062 -49587
Sample Output 3
34
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) があります。
以下の Q 個のクエリを処理してください。
タイプ 1 :
1 i x
A_i を x に変更する。
タイプ 2 :
2 l r
A_l,A_{l+1},\dots,A_r に含まれる -1 をそれぞれ任意の正整数に置き換えることができるとき、 A_l,A_{l+1},\dots,A_r を全て等しくできるなら Yes 、そうでないなら No と出力する。
制約
- 入力は全て整数
- 1 \le N,Q \le 3 \times 10^5
- -1 \le A_i \le 10^6
- A_i \neq 0
- タイプ 1 のクエリは以下の制約を満たす
- 1 \le i \le N
- -1 \le x \le 10^6
- x \neq 0
- タイプ 2 のクエリは以下の制約を満たす
- 1 \le l \le r \le N
入力
入力は以下の形式で標準入力から与えられる。 ただし、 {\rm Query}_i は i 個目のクエリを表す。
N Q
A_1 A_2 \dots A_N
{\rm Query}_1
{\rm Query}_2
\vdots
{\rm Query}_Q
出力
タイプ 2 のクエリが与えられるごとに、そのクエリの答えを 1 行に出力せよ。
入力例 1
8 9 5 -1 3 -1 2 3 4 5 2 3 6 1 5 -1 2 3 6 1 7 5 2 7 8 2 1 8 1 3 5 1 6 -1 2 1 8
出力例 1
No Yes Yes No Yes
この入力には 9 個のクエリが含まれます。
以下では、最初 3 個のクエリについて説明します。
- 最初、 A=(5,-1,3,-1,2,3,4,5) です。
- 1 個目のクエリはタイプ 2 で、問われている部分列は (3,-1,2,3) です。これらを問題文中の操作で全て等しくすることはできないため、
Noと出力します。 - 2 個目のクエリはタイプ 1 で、 A_5 を -1 に変更します。その結果、 A=(5,-1,3,-1,-1,3,4,5) となります。
- 3 個目のクエリはタイプ 2 で、問われている部分列は (3,-1,-1,3) です。これらを問題文中の操作で全て等しくすることができるため、
Yesと出力します。
Score : 500 points
Problem Statement
There is an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Process the following Q queries.
Type 1 :
1 i x
Change A_i to x.
Type 2 :
2 l r
Suppose that each -1 contained in A_l,A_{l+1},\dots,A_r can be replaced with an arbitrary positive integer. Print Yes if A_l,A_{l+1},\dots,A_r can all be made equal, and No otherwise.
Constraints
- All input values are integers.
- 1 \le N,Q \le 3 \times 10^5
- -1 \le A_i \le 10^6
- A_i \neq 0
- Queries of type 1 satisfy the following constraints:
- 1 \le i \le N
- -1 \le x \le 10^6
- x \neq 0
- Queries of type 2 satisfy the following constraints:
- 1 \le l \le r \le N
Input
The input is given from Standard Input in the following format, where {\rm Query}_i denotes the i-th query:
N Q
A_1 A_2 \dots A_N
{\rm Query}_1
{\rm Query}_2
\vdots
{\rm Query}_Q
Output
Each time a query of type 2 is given, print the answer to that query in one line.
Sample Input 1
8 9 5 -1 3 -1 2 3 4 5 2 3 6 1 5 -1 2 3 6 1 7 5 2 7 8 2 1 8 1 3 5 1 6 -1 2 1 8
Sample Output 1
No Yes Yes No Yes
This input contains 9 queries.
Below, we explain the first 3 queries.
- Initially, A=(5,-1,3,-1,2,3,4,5).
- The 1-st query is of type 2, and the subarray in question is (3,-1,2,3). These cannot all be made equal by the operation in the problem statement, so print
No. - The 2-nd query is of type 1, and changes A_5 to -1. As a result, A=(5,-1,3,-1,-1,3,4,5).
- The 3-rd query is of type 2, and the subarray in question is (3,-1,-1,3). These can all be made equal by the operation in the problem statement, so print
Yes.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 600 点
問題文
0, 1, ? からなる文字列 S,X,Y が与えられます。
以下の手順で得られる文字列を良い文字列と呼ぶことにします。
- x,y からなる列 t=(t _ 1,t _ 2,\ldots,t _ k) であって、t _ i=t _ {i+1}=y となるような整数 i\ (1\le i\lt k) が存在しないようなものを一つ選ぶ。
- t の各要素について、それが x なら文字列 X に、y なら文字列 Y に置き換えることで得られる列を s=(s _ 1,s _ 2,\ldots,s _ k) とする。s _ 1,s _ 2,\ldots,s _ k をこの順に連結した文字列を T とする。
- T に含まれる
?について、それぞれ0か1を選んで置き換える。
S に含まれる ? についてそれぞれ 0 か 1 を選んで置き換えることで得られる良い文字列の個数を 998244353 で割った余りを求めてください。
制約
- S,X,Y は
0,1,?からなる文字列 - 1\le |S|\le2000\ (ここで、|S| で S の長さを表す。)
- 1\le |X|\le8
- 1\le |Y|\le8
入力
入力は以下の形式で標準入力から与えられる。
S X Y
出力
答えを出力せよ。
入力例 1
?0??11?1? 0?1 ?1?
出力例 1
5
例えば、S に含まれる ? についてそれぞれ 0 か 1 を選んで置き換えることで得られる文字列 001111011 は良い文字列です。
手順を t=(x,y,x) から始めると、T={}0?1?1?0?1 となります。
T に含まれる ? をそれぞれ 0 か 1 に置き換えることで 001111011 を得ることができるので、001111011 は良い文字列です。
この他に、001011010, 001011011, 001011110, 001011111 の 4 つの文字列が条件を満たします。
これらの他に条件を満たす文字列は存在しないため、5 を出力してください。
入力例 2
0?10?1?01? ??? ?????
出力例 2
0
t において y が連続してはいけないことに注意してください。
入力例 3
????????????1???1??0??????1???????1????????1????????????????0 ?0?? ?
出力例 3
992356689
条件を満たす文字列の個数を 998244353 で割った余りを出力することに注意してください。
Score : 600 points
Problem Statement
You are given strings S,X,Y consisting of 0, 1, and ?.
A string that can be obtained by the following procedure is called a good string.
- Choose a sequence t=(t _ 1,t _ 2,\ldots,t _ k) consisting of x and y such that there is no integer i\ (1\le i\lt k) with t _ i=t _ {i+1}=y.
- Let s=(s _ 1,s _ 2,\ldots,s _ k) be the sequence obtained by replacing each element of t with the string X if it is x, and with the string Y if it is y. Let T be the string obtained by concatenating s _ 1,s _ 2,\ldots,s _ k in this order.
- Replace each
?in T with0or1of your choice.
Find the number, modulo 998244353, of good strings that can be obtained by replacing each ? in S with 0 or 1.
Constraints
- S,X,Y are strings consisting of
0,1, and?. - 1\le |S|\le2000 (Here, |S| denotes the length of S.)
- 1\le |X|\le8
- 1\le |Y|\le8
Input
The input is given from Standard Input in the following format:
S X Y
Output
Print the answer.
Sample Input 1
?0??11?1? 0?1 ?1?
Sample Output 1
5
For example, the string 001111011, which can be obtained by replacing each ? in S with 0 or 1, is a good string.
Starting the procedure with t=(x,y,x) gives T={}0?1?1?0?1.
By replacing each ? in T with 0 or 1, we can obtain 001111011, so 001111011 is a good string.
Besides this, the four strings 001011010, 001011011, 001011110, 001011111 satisfy the condition.
No other strings satisfy the condition, so print 5.
Sample Input 2
0?10?1?01? ??? ?????
Sample Output 2
0
Note that y must not appear consecutively in t.
Sample Input 3
????????????1???1??0??????1???????1????????1????????????????0 ?0?? ?
Sample Output 3
992356689
Note that you should print the number of strings satisfying the condition modulo 998244353.