Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
1583 以上 2023 以下の整数 Y が与えられます。
西暦 Y 年の日数を求めてください。
なお、制約の範囲内では西暦 Y 年の日数は以下の通りです。
-
Y が 4 の倍数でない年は 365 日
-
Y が 4 の倍数で、かつ 100 の倍数でない年は 366 日
-
Y が 100 の倍数で、かつ 400 の倍数でない年は 365 日
-
Y が 400 の倍数である年は 366 日
制約
- Y は 1583 以上 2023 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
Y
出力
西暦 Y 年の日数を整数として出力せよ。
入力例 1
2023
出力例 1
365
2023 は 4 の倍数でないので 365 日です。
入力例 2
1992
出力例 2
366
1992 は 4 の倍数で、かつ 100 の倍数でないので 366 日です。
入力例 3
1800
出力例 3
365
1800 は 100 の倍数で、かつ 400 の倍数でないので 365 日です。
入力例 4
1600
出力例 4
366
1600 は 400 の倍数なので 366 日です。
Score : 100 points
Problem Statement
You are given an integer Y between 1583 and 2023.
Find the number of days in the year Y of the Gregorian calendar.
Within the given range, the year Y has the following number of days:
-
if Y is not a multiple of 4, then 365 days;
-
if Y is a multiple of 4 but not a multiple of 100, then 366 days;
-
if Y is a multiple of 100 but not a multiple of 400, then 365 days;
-
if Y is a multiple of 400, then 366 days.
Constraints
- Y is an integer between 1583 and 2023, inclusive.
Input
The input is given from Standard Input in the following format:
Y
Output
Print the number of days in the year Y as an integer.
Sample Input 1
2023
Sample Output 1
365
2023 is not a multiple of 4, so it has 365 days.
Sample Input 2
1992
Sample Output 2
366
1992 is a multiple of 4 but not a multiple of 100, so it has 366 days.
Sample Input 3
1800
Sample Output 3
365
1800 is a multiple of 100 but not a multiple of 400, so it has 365 days.
Sample Input 4
1600
Sample Output 4
366
1600 is a multiple of 400, so it has 366 days.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる文字列 S が与えられます。
S の先頭から a 文字目と b 文字目を入れ替えて得られる文字列を出力してください。
制約
- S は英小文字からなる文字列
- S の長さ |S| は、 2 \leq |S| \leq 10 を満たす
- 1 \leq a < b \leq |S|
- a, b は整数
入力
入力は以下の形式で標準入力から与えられる。
S a b
出力
答えを出力せよ。
入力例 1
chokudai 3 5
出力例 1
chukodai
chokudai
の 3 文字目 o
と 5 文字目 u
を入れ替えると chukodai
となります。
入力例 2
aa 1 2
出力例 2
aa
この入力例では、S の 1 文字目と 2 文字目を入れ替えて得られる文字列は、元の S と同じになります。
入力例 3
aaaabbbb 1 8
出力例 3
baaabbba
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Swap the a-th and b-th characters from the beginning of S and print the resulting string.
Constraints
- S is a string consisting of lowercase English letters.
- The length of S, |S|, satisfies 2 \leq |S| \leq 10.
- 1 \leq a < b \leq |S|
- a and b are integers.
Input
Input is given from Standard Input in the following format:
S a b
Output
Print the answer.
Sample Input 1
chokudai 3 5
Sample Output 1
chukodai
After swapping the 3-rd character o
and 5-th character u
of chokudai
, we have chukodai
.
Sample Input 2
aa 1 2
Sample Output 2
aa
In this sample, after swapping the 1-st and 2-nd characters of S, we have the same string as S.
Sample Input 3
aaaabbbb 1 8
Sample Output 3
baaabbba
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。
- X の 10^i の位以下を四捨五入する。
- 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
- 具体例を挙げる。
- 273 の 10^1 の位以下を四捨五入すれば 300 となる。
- 999 の 10^2 の位以下を四捨五入すれば 1000 となる。
- 100 の 10^9 の位以下を四捨五入すれば 0 となる。
- 1015 の 10^0 の位以下を四捨五入すれば 1020 となる。
制約
- X,K は整数
- 0 \le X < 10^{15}
- 1 \le K \le 15
入力
入力は以下の形式で標準入力から与えられる。
X K
出力
答えを整数として出力せよ。
入力例 1
2048 2
出力例 1
2100
操作の過程で、 X は 2048 \rightarrow 2050 \rightarrow 2100 と変化します。
入力例 2
1 15
出力例 2
0
入力例 3
999 3
出力例 3
1000
入力例 4
314159265358979 12
出力例 4
314000000000000
X は 32bit 整数型に収まらない可能性があります。
Score : 200 points
Problem Statement
Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.
- Round X off to the nearest 10^i.
- Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
- Here are some examples:
- Rounding 273 off to the nearest 10^2 yields 300.
- Rounding 999 off to the nearest 10^3 yields 1000.
- Rounding 100 off to the nearest 10^{10} yields 0.
- Rounding 1015 off to the nearest 10^1 yields 1020.
Constraints
- X and K are integers.
- 0 \le X < 10^{15}
- 1 \le K \le 15
Input
The input is given from Standard Input in the following format:
X K
Output
Print the answer as an integer.
Sample Input 1
2048 2
Sample Output 1
2100
X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.
Sample Input 2
1 15
Sample Output 2
0
Sample Input 3
999 3
Sample Output 3
1000
Sample Input 4
314159265358979 12
Sample Output 4
314000000000000
X may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
整数 A,B が K 進法表記で与えられます。
A \times B を 10 進法表記で出力してください。
注記
K 進法表記については、Wikipedia「位取り記数法」 を参照してください。
制約
- 2 \leq K \leq 10
- 1 \leq A,B \leq 10^5
- A,B は K 進法表記で与えられる
入力
入力は以下の形式で標準入力から与えられる。
K A B
出力
答えを出力せよ。
入力例 1
2 1011 10100
出力例 1
220
2 進法表記の 1011
を 、10 進法表記すると 11 です。
2 進法表記の 10100
を、 10 進法表記すると 20 です。
11 \times 20 = 220 なので 220 を出力します。
入力例 2
7 123 456
出力例 2
15642
7 進法表記の 123
を 、10 進法表記すると 66 です。
7 進法表記の 456
を、 10 進法表記すると 237 です。
66 \times 237 = 15642 なので 15642 を出力します。
Score : 200 points
Problem Statement
You are given integers A and B, in base K.
Print A \times B in decimal.
Notes
For base-K representation, see Wikipedia's article on Positional notation.
Constraints
- 2 \leq K \leq 10
- 1 \leq A,B \leq 10^5
- A and B are in base-K representation.
Input
Input is given from Standard Input in the following format:
K A B
Output
Print the answer.
Sample Input 1
2 1011 10100
Sample Output 1
220
1011
in base 2 corresponds to 11 in base 10.
10100
in base 2 corresponds to 20 in base 10.
We have 11 \times 20 = 220, so print 220.
Sample Input 2
7 123 456
Sample Output 2
15642
123
in base 7 corresponds to 66 in base 10.
456
in base 7 corresponds to 237 in base 10.
We have 66 \times 237 = 15642, so print 15642.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。
1 i j
\colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる2 i
\colon 箱 i に入っているカードに書かれた数を昇順ですべて答える3 i
\colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える
ただし、以下の点に注意してください。
- 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
- 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する
制約
- 1 \leq N, Q \leq 2 \times 10^5
- 1 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- 2 番目の形式のクエリについて、
- 1 \leq i \leq N
- このクエリが与えられる時点で箱 i にカードが入っている
- 3 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
- 出力するべき数は合計で 2 \times 10^5 個以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。
1 i j
2 i
3 i
出力
2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。
入力例 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
出力例 1
1 2 1 1 2 1 4 4
クエリを順に処理していきます。
- カードに 1 を書き込み、箱 1 に入れます。
- カードに 2 を書き込み、箱 4 に入れます。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 2 です。
- 1, 2 の順に出力してください。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
- 1 を 2 度出力することに注意してください。
- 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
- 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
- 数 2 が書かれたカードが入っている箱は箱 4 です。
入力例 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
出力例 2
1 2 200000 1
Score : 300 points
Problem Statement
We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.
1 i j
\colon Write the number i on a blank card and put it into box j.2 i
\colon Report all numbers written on the cards in box i, in ascending order.3 i
\colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.
Here, note the following.
- In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
- In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- For a query of the first kind:
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- For a query of the second kind:
- 1 \leq i \leq N
- Box i contains some cards when this query is given.
- For a query of the third kind:
- 1 \leq i \leq 2 \times 10^5
- Some boxes contain a card with the number i when this query is given.
- At most 2 \times 10^5 numbers are to be reported.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:
1 i j
2 i
3 i
Output
Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.
Sample Input 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
Sample Output 1
1 2 1 1 2 1 4 4
Let us process the queries in order.
- Write 1 on a card and put it into box 1.
- Write 2 on a card and put it into box 4.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1 and 2.
- Print 1 and 2 in this order.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1, 1, and 2.
- Note that you should print 1 twice.
- Boxes 1 and 4 contain a card with the number 1.
- Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
- Boxes 4 contains a card with the number 2.
Sample Input 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
Sample Output 2
1 2 200000 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
それぞれ N 個、M 個の正整数からなる 2 つの数列 A=(A_1,A_2, \ldots ,A_N) と B=(B_1, \ldots ,B_M) が与えられます。
それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差の最小値、すなわち、 \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert を求めてください。
制約
- 1 \leq N,M \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
出力
答えを出力せよ。
入力例 1
2 2 1 6 4 9
出力例 1
2
それぞれの数列から 1 つずつ要素を選んだときの 2 つの値の差としてあり得るのは、 \lvert 1-4\rvert=3 、 \lvert 1-9\rvert=8 、 \lvert 6-4\rvert=2 、 \lvert 6-9\rvert=3 の 4 つです。 この中で最小である 2 を出力します。
入力例 2
1 1 10 10
出力例 2
0
入力例 3
6 8 82 76 82 82 71 70 17 39 67 2 45 35 22 24
出力例 3
3
Score : 300 points
Problem Statement
You are given two sequences: A=(A_1,A_2, \ldots ,A_N) consisting of N positive integers, and B=(B_1, \ldots ,B_M) consisting of M positive integers.
Find the minimum difference of an element of A and an element of B, that is, \displaystyle \min_{ 1\leq i\leq N}\displaystyle\min_{1\leq j\leq M} \lvert A_i-B_j\rvert.
Constraints
- 1 \leq N,M \leq 2\times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
Output
Print the answer.
Sample Input 1
2 2 1 6 4 9
Sample Output 1
2
Here is the difference for each of the four pair of an element of A and an element of B: \lvert 1-4\rvert=3, \lvert 1-9\rvert=8, \lvert 6-4\rvert=2, and \lvert 6-9\rvert=3. We should print the minimum of these values, or 2.
Sample Input 2
1 1 10 10
Sample Output 2
0
Sample Input 3
6 8 82 76 82 82 71 70 17 39 67 2 45 35 22 24
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。 以下、上から i 行目、左から j 列目のマスを (i,j) と表記します。 グリッドの各マスには英小文字が書かれており、(i,j) に書かれた文字は与えられる文字列 S_i の j 文字目と一致します。
すぬけくんは、辺で隣接するマスに移動することを繰り返して (1,1) から (H,W) まで移動しようと思っています。
訪れるマス (最初の (1,1) と 最後の (H,W) を含む)に書かれた文字が、
訪れる順に s
\rightarrow n
\rightarrow u
\rightarrow k
\rightarrow e
\rightarrow s
\rightarrow n
\rightarrow \dots
となるような経路が存在するか判定してください。
なお、2 つのマス (i_1,j_1),(i_2,j_2) は |i_1-i_2|+|j_1-j_2| = 1 を満たすとき、またそのときに限り「辺で隣接する」といいます。
より厳密には、マスの列 ((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) であって以下の条件を全て満たすものが存在するか判定してください。
- (i_1,j_1) = (1,1),(i_k,j_k) = (H,W)
- すべての t\ (1 \leq t < k) について、(i_t,j_t) と (i_{t+1},j_{t+1}) は辺で隣接する
- すべての t\ (1 \leq t \leq k) について、(i_t,j_t) に書かれた文字は
snuke
の ((t-1) \bmod 5) + 1 文字目と一致する
制約
- 2\leq H,W \leq 500
- H,W は整数
- S_i は英小文字からなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W S_1 S_2 \vdots S_H
出力
問題文中の条件を満たす経路が存在するならば Yes
を、存在しないならば No
を出力せよ。
入力例 1
2 3 sns euk
出力例 1
Yes
(1,1) \rightarrow (1,2) \rightarrow (2,2) \rightarrow (2,3) という経路は、訪れたマスに書かれた文字が訪れた順に
s
\rightarrow n
\rightarrow u
\rightarrow k
となるため条件を満たします。
入力例 2
2 2 ab cd
出力例 2
No
入力例 3
5 7 skunsek nukesnu ukeseku nsnnesn uekukku
出力例 3
Yes
Score : 400 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns. We denote by (i,j) the cell at the i-th row from the top and j-th column from the left. Each cell in the grid has a lowercase English letter written on it. The letter written on (i,j) equals the j-th character of a given string S_i.
Snuke will repeat moving to an adjacent cell sharing a side to travel from (1,1) to (H,W).
Determine if there is a path
in which the letters written on the visited cells (including initial (1,1) and final (H,W)) are
s
\rightarrow n
\rightarrow u
\rightarrow k
\rightarrow e
\rightarrow s
\rightarrow n
\rightarrow \dots, in the order of visiting.
Here, a cell (i_1,j_1) is said to be an adjacent cell of (i_2,j_2) sharing a side if and only if |i_1-i_2|+|j_1-j_2| = 1.
Formally, determine if there is a sequence of cells ((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) such that:
- (i_1,j_1) = (1,1),(i_k,j_k) = (H,W);
- (i_{t+1},j_{t+1}) is an adjacent cell of (i_t,j_t) sharing a side, for all t\ (1 \leq t < k); and
- the letter written on (i_t,j_t) coincides with the (((t-1) \bmod 5) + 1)-th character of
snuke
, for all t\ (1 \leq t \leq k).
Constraints
- 2\leq H,W \leq 500
- H and W are integers.
- S_i is a string of length W consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
H W S_1 S_2 \vdots S_H
Output
Print Yes
if there is a path satisfying the conditions in the problem statement; print No
otherwise.
Sample Input 1
2 3 sns euk
Sample Output 1
Yes
The path (1,1) \rightarrow (1,2) \rightarrow (2,2) \rightarrow (2,3) satisfies the conditions
because they have s
\rightarrow n
\rightarrow u
\rightarrow k
written on them, in the order of visiting.
Sample Input 2
2 2 ab cd
Sample Output 2
No
Sample Input 3
5 7 skunsek nukesnu ukeseku nsnnesn uekukku
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
この問題は インタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
正整数 N および 0 以上 2^N 未満の整数 L,R(L\leq R) が与えられます。 ジャッジシステムは、0 以上 99 以下の整数からなる長さ 2^N の数列 A = (A_0, A_1, \dots, A_{2^N-1}) を隠し持っています。
あなたの目標は A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを求めることです。ただし、あなたは数列 A の要素の値を直接知ることはできません。 その代わりに、ジャッジシステムに対して以下の質問を行うことができます。
- 2^i(j+1)\leq 2^N を満たすように非負整数 i,j を選ぶ。l=2^ij,r=2^i(j+1)-1 として A_l+A_{l+1}+\dots+A_{r} を 100 で割った余りを聞く。
どのような A であっても A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを特定することができる質問回数の最小値を m とします。m 回以内の質問を行って A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを求めてください。
制約
- 1\leq N\leq 18
- 0\leq L\leq R\leq 2^N-1
- 入力は全て整数
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
最初に、整数 N,L,R を標準入力から受け取ってください。
N L R
次に、A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを特定できるまで質問を繰り返してください。 質問は、以下の形式で標準出力に出力してください。
? i j
ここで、i,j は以下を満たす必要があります。
- i,j は非負整数
- 2^i(j+1)\leq 2^N
これに対する応答は、次の形式で標準入力から与えられます。
T
ここで、T は質問に対する答えで、l=2^ij,r=2^i(j+1)-1 としたとき A_l+A_{l+1}+\dots+A_{r} を 100 で割った余りです。
ただし、i,j が制約を満たしていないか、質問回数が m 回を超えた場合は T は -1
となります。
ジャッジが -1
を返した場合、プログラムはすでに不正解とみなされています。この場合、ただちにプログラムを終了してください。
A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りが特定出来たら、S を A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りとして以下の形式で出力してください。その後、ただちにプログラムを終了してください。
! S
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で誤った出力形式による出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
入出力例
以下は、N=3,L=1,R=5,A=(31,41,59,26,53,58,97,93) の場合の入出力例です。この場合 m=3 であるため、質問を 3 回まで行うことができます。
入力 | 出力 | 説明 |
---|---|---|
3 1 5 | まず整数 N,L,R が与えられます。 | |
? 0 1 | (i,j)=(0,1) として質問を行います。 | |
41 | l=1,r=1 であるため、質問の答えは A_1=41 を 100 で割った余りである 41 です。ジャッジはその値を返します。 | |
? 1 1 | (i,j) = (1,1) として質問を行います。 | |
85 | l=2,r=3 であるため、質問の答えは A_2+A_3=85 を 100 で割った余りである 85 です。ジャッジはその値を返します。 | |
? 1 2 | (i,j) = (1,2) として質問を行います。 | |
11 | l=4,r=5 であるため、質問の答えは A_4+A_5=111 を 100 で割った余りである 11 です。ジャッジはその値を返します。 | |
! 37 | 答えは 37 であるとわかったので、それを出力します。 |
Score : 500 points
Problem Statement
This is an interactive problem (where your program interacts with the judge via input and output).
You are given a positive integer N and integers L and R such that 0 \leq L \leq R < 2^N. The judge has a hidden sequence A = (A_0, A_1, \dots, A_{2^N-1}) consisting of integers between 0 and 99, inclusive.
Your goal is to find the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100. However, you cannot directly know the values of the elements in the sequence A. Instead, you can ask the judge the following question:
- Choose non-negative integers i and j such that 2^i(j+1) \leq 2^N. Let l = 2^i j and r = 2^i (j+1) - 1. Ask for the remainder when A_l + A_{l+1} + \dots + A_r is divided by 100.
Let m be the minimum number of questions required to determine the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100 for any sequence A. You need to find this remainder within m questions.
Constraints
- 1 \leq N \leq 18
- 0 \leq L \leq R \leq 2^N - 1
- All input values are integers.
Input and Output
This is an interactive problem (where your program interacts with the judge via input and output).
First, read the integers N, L, and R from Standard Input:
N L R
Then, repeat asking questions until you can determine the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100. Each question should be printed in the following format:
? i j
Here, i and j must satisfy the following constraints:
- i and j are non-negative integers.
- 2^i(j+1) \leq 2^N
The response to the question will be given in the following format from Standard Input:
T
Here, T is the answer to the question, which is the remainder when A_l + A_{l+1} + \dots + A_r is divided by 100, where l = 2^i j and r = 2^i (j+1) - 1.
If i and j do not satisfy the constraints, or if the number of questions exceeds m, then T will be -1
.
If the judge returns -1
, your program is already considered incorrect. In this case, terminate the program immediately.
Once you have determined the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100, print the remainder S in the following format and terminate the program immediately:
! S
Notes
- At the end of each output, print a newline and flush Standard Output. Otherwise, the verdict may be TLE.
- If your output is malformed or your program exits prematurely, the verdict will be indeterminate.
- After printing the answer, terminate the program immediately. Otherwise, the verdict will be indeterminate.
Example
Here is an example for N=3, L=1, R=5, and A=(31, 41, 59, 26, 53, 58, 97, 93). In this case, m=3, so you can ask up to three questions.
Input | Output | Description |
---|---|---|
3 1 5 | First, the integers N, L, and R are given. | |
? 0 1 | Ask the question with (i, j) = (0, 1). | |
41 | l=1 and r=1, so the response is the remainder when A_1=41 is divided by 100, which is 41. The judge returns this value. | |
? 1 1 | Ask the question with (i, j) = (1, 1). | |
85 | l=2 and r=3, so the response is the remainder when A_2 + A_3 = 85 is divided by 100, which is 85. The judge returns this value. | |
? 1 2 | Ask the question with (i, j) = (1, 2). | |
11 | l=4 and r=5, so the response is the remainder when A_4 + A_5 = 111 is divided by 100, which is 11. The judge returns this value. | |
! 37 | The answer is 37, so print this value. |
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
東西に続く道があり、道の上には N 人の高橋くんがいます。 道は原点と呼ばれる点から東西に十分長く続いています。
i 番目 (1\leq i\leq N) の高橋くんは、はじめ原点から東に X _ i メートル進んだところにいます。
高橋くんたちは道の上を東西に動くことができます。 具体的には、次の移動を好きなだけ行うことができます。
- 高橋くんを一人選ぶ。移動する先に他の高橋くんがいない場合、選んだ高橋くんを 1 メートル東に、もしくは西に移動させる。
高橋くんたちには合計 Q 個の用事があり、i 個目 (1\leq i\leq Q) の用事は次の形式で表されます。
- T _ i 番目の高橋くんが座標 G _ i に到着する。
Q 個の用事を先頭から順にすべて完了するために必要な移動回数の最小値を求めてください。
制約
- 1\leq N\leq2\times10 ^ 5
- 0\leq X _ 1\lt X _ 2\lt\dotsb\lt X _ N\leq10 ^ 8
- 1\leq Q\leq2\times10 ^ 5
- 1\leq T _ i\leq N\ (1\leq i\leq Q)
- 0\leq G _ i\leq10 ^ 8\ (1\leq i\leq Q)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X _ 1 X _ 2 \ldots X _ N Q T _ 1 G _ 1 T _ 2 G _ 2 \vdots T _ Q G _ Q
出力
答えを出力せよ。
入力例 1
5 10 20 30 40 50 4 3 45 4 20 1 35 2 60
出力例 1
239
高橋くんたちの最適な行動は以下のようになります(それぞれの高橋くんの座標は正確に描かれているとは限りません)。
それぞれの用事では、高橋くんたちは次のように移動しています。
- 4 番目の高橋くんが 6 回東に進み、3 番目の高橋くんが 15 回東に進む。
- 2 番目の高橋くんが 2 回西に進み、3 番目の高橋くんが 26 回西に進み、4 番目の高橋くんが 26 回西に進む。
- 4 番目の高橋くんが 18 回東に進み、3 番目の高橋くんが 18 回東に進み、2 番目の高橋くんが 18 回東に進み、1 番目の高橋くんが 25 回東に進む。
- 5 番目の高橋くんが 13 回東に進み、4 番目の高橋くんが 24 回東に進み、3 番目の高橋くんが 24 回東に進み、2 番目の高橋くんが 24 回東に進む。
高橋くんたちの移動回数の合計は 21+54+79+85=239 回となります。
移動回数の合計を 238 回以下としてすべての用事を完了することはできないため、239
を出力してください。
入力例 2
8 0 1 2 3 4 5 6 100000000 6 1 100000000 8 0 1 100000000 8 4 1 100000000 5 21006578
出力例 2
4294967297
途中で一部の高橋くんが原点より西側や、原点より 10 ^ 8+1 メートル以上東に進んだところに移動する必要がある場合があることに注意してください。
また、答えが 2 ^ {32} を超える場合があることに注意してください。
入力例 3
12 1558 3536 3755 3881 4042 4657 5062 7558 7721 8330 8542 9845 8 9 1694 7 3296 12 5299 5 5195 5 5871 1 2491 8 1149 8 2996
出力例 3
89644
Score : 550 points
Problem Statement
There is a road extending east and west, and N persons are on the road. The road extends infinitely long to the east and west from a point called the origin.
The i-th person (1\leq i\leq N) is initially at a position X_i meters east from the origin.
The persons can move along the road to the east or west. Specifically, they can perform the following movement any number of times.
- Choose one person. If there is no other person at the destination, move the chosen person 1 meter east or west.
They have Q tasks in total, and the i-th task (1\leq i\leq Q) is as follows.
- The T_i-th person arrives at coordinate G_i.
Find the minimum total number of movements required to complete all Q tasks in order.
Constraints
- 1\leq N\leq2\times10^5
- 0\leq X_1 < X_2 < \dotsb < X_N \leq10^8
- 1\leq Q\leq2\times10^5
- 1\leq T_i\leq N\ (1\leq i\leq Q)
- 0\leq G_i\leq10^8\ (1\leq i\leq Q)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X_1 X_2 \ldots X_N Q T_1 G_1 T_2 G_2 \vdots T_Q G_Q
Output
Print the answer.
Sample Input 1
5 10 20 30 40 50 4 3 45 4 20 1 35 2 60
Sample Output 1
239
An optimal sequence of movements for the persons is as follows (the positions of the persons are not necessarily drawn to scale):
For each task, the persons move as follows.
- The 4th person moves 6 steps east, and the 3rd person moves 15 steps east.
- The 2nd person moves 2 steps west, the 3rd person moves 26 steps west, and the 4th person moves 26 steps west.
- The 4th person moves 18 steps east, the 3rd person moves 18 steps east, the 2nd person moves 18 steps east, and the 1st person moves 25 steps east.
- The 5th person moves 13 steps east, the 4th person moves 24 steps east, the 3rd person moves 24 steps east, and the 2nd person moves 24 steps east.
The total number of movements is 21+54+79+85=239.
You cannot complete all tasks with a total movement count of 238 or less, so print 239
.
Sample Input 2
8 0 1 2 3 4 5 6 100000000 6 1 100000000 8 0 1 100000000 8 4 1 100000000 5 21006578
Sample Output 2
4294967297
Note that some persons may need to move to the west of the origin or more than 10^8 meters to the east of it.
Also, note that the answer may exceed 2^{32}.
Sample Input 3
12 1558 3536 3755 3881 4042 4657 5062 7558 7721 8330 8542 9845 8 9 1694 7 3296 12 5299 5 5195 5 5871 1 2491 8 1149 8 2996
Sample Output 3
89644