実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
-10^{18} 以上 10^{18} 以下の整数 N が与えられます。
以下の条件を満たす 0 以上 998244353 未満の整数 x を求めてください。なお、答えが一意に定まることが証明できます。
- N-x は 998244353 の倍数
制約
- N は -10^{18} 以上 10^{18} 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
998244354
出力例 1
1
998244354-1 = 998244353 は 998244353 の倍数なので条件を満たします。
入力例 2
-9982443534
出力例 2
998244349
-9982443534-998244349= -10980687883 は 998244353 の倍数なので条件を満たします。
Score : 200 points
Problem Statement
You are given an integer N between -10^{18} and 10^{18} (inclusive).
Find an integer x between 0 and 998244353 - 1 (inclusive) that satisfies the following condition. It can be proved that such an integer is unique.
- N-x is a multiple of 998244353.
Constraints
- N is an integer between -10^{18} and 10^{18} (inclusive).
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
998244354
Sample Output 1
1
998244354-1 = 998244353 is a multiple of 998244353, so the condition is satisfied.
Sample Input 2
-9982443534
Sample Output 2
998244349
-9982443534-998244349= -10980687883 is a multiple of 998244353, so the condition is satisfied.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 L,R と、英小文字のみからなる文字列 S が与えられます。
S の L 文字目から R 文字目までの部分を反転した(すなわち、 L 文字目から R 文字目までの文字の並びを逆にした)文字列を出力してください。
制約
- S は英小文字のみからなる。
- 1 \le |S| \le 10^5 (|S| は S の長さ)
- L,R は整数
- 1 \le L \le R \le |S|
入力
入力は以下の形式で標準入力から与えられる。
L R S
出力
問題文の指示通り出力せよ。
入力例 1
3 7 abcdefgh
出力例 1
abgfedch
abcdefgh の 3 文字目から 7 文字目までの部分を反転すると、 abgfedch となります。
入力例 2
1 7 reviver
出力例 2
reviver
操作を行った結果が元の文字列と同一であることもあります。
入力例 3
4 13 merrychristmas
出力例 3
meramtsirhcyrs
Score : 200 points
Problem Statement
You are given integers L, R, and a string S consisting of lowercase English letters.
Print this string after reversing (the order of) the L-th through R-th characters.
Constraints
- S consists of lowercase English letters.
- 1 \le |S| \le 10^5 (|S| is the length of S.)
- L and R are integers.
- 1 \le L \le R \le |S|
Input
Input is given from Standard Input in the following format:
L R S
Output
Print the specified string.
Sample Input 1
3 7 abcdefgh
Sample Output 1
abgfedch
After reversing the 3-rd through 7-th characters of abcdefgh, we have abgfedch.
Sample Input 2
1 7 reviver
Sample Output 2
reviver
The operation may result in the same string as the original.
Sample Input 3
4 13 merrychristmas
Sample Output 3
meramtsirhcyrs
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
整数 X が与えられます。この X に以下を施すことを「操作」と呼びます。
- 以下の 2 つのうちどちらかを選択し、実行する。
- X に 1 を加算する。
- X から 1 を減算する。
初項 A 、公差 D 、項数 N の等差数列 S に含まれる数を「良い数」と呼びます。
「操作」を 0 回以上何度でも使って X を「良い数」にする時、必要な「操作」の最小回数を求めてください。
制約
- 入力は全て整数
- -10^{18} \le X,A \le 10^{18}
- -10^6 \le D \le 10^6
- 1 \le N \le 10^{12}
入力
入力は以下の形式で標準入力から与えられる。
X A D N
出力
答えを整数として出力せよ。
入力例 1
6 2 3 3
出力例 1
1
A=2,D=3,N=3 であるため、 S=(2,5,8) です。
X=6 を「良い数」にするためには、 X から 1 を減算することを 1 度行えば良いです。
0 回の操作で X を「良い数」にすることはできません。
入力例 2
0 0 0 1
出力例 2
0
D=0 である場合もあります。また、操作を 1 回も必要としない場合もあります。
入力例 3
998244353 -10 -20 30
出力例 3
998244363
入力例 4
-555555555555555555 -1000000000000000000 1000000 1000000000000
出力例 4
444445
Score : 300 points
Problem Statement
You are given an integer X. The following action on this integer is called an operation.
- Choose and do one of the following.
- Add 1 to X.
- Subtract 1 from X.
The terms in the arithmetic progression S with N terms whose initial term is A and whose common difference is D are called good numbers.
Consider performing zero or more operations to make X a good number. Find the minimum number of operations required to do so.
Constraints
- All values in input are integers.
- -10^{18} \le X,A \le 10^{18}
- -10^6 \le D \le 10^6
- 1 \le N \le 10^{12}
Input
Input is given from Standard Input in the following format:
X A D N
Output
Print the answer as an integer.
Sample Input 1
6 2 3 3
Sample Output 1
1
Since A=2,D=3,N=3, we have S=(2,5,8).
You can subtract 1 from X once to make X=6 a good number.
It is impossible to make X good in zero operations.
Sample Input 2
0 0 0 1
Sample Output 2
0
We might have D=0. Additionally, no operation might be required.
Sample Input 3
998244353 -10 -20 30
Sample Output 3
998244363
Sample Input 4
-555555555555555555 -1000000000000000000 1000000 1000000000000
Sample Output 4
444445
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君はレベル N の赤い宝石を 1 個持っています。(他に宝石は持っていません。)
高橋君は次の操作を好きなだけ行うことができます。
- レベル n の赤い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n の青い宝石 X 個」に変換する。
- レベル n の青い宝石 (n は 2 以上) を「レベル n-1 の赤い宝石 1 個と、レベル n-1 の青い宝石 Y 個」に変換する。
高橋君はレベル 1 の青い宝石ができるだけたくさんほしいです。操作によって高橋君はレベル 1 の青い宝石を最大何個入手できますか?
制約
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y
出力
答えを出力せよ。
入力例 1
2 3 4
出力例 1
12
次のような変換を行うことで、高橋君はレベル 1 の青い宝石を 12 個手に入れることができます。
- まず、レベル 2 の赤い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個に変換します。
- 操作後の高橋君は、レベル 1 の赤い宝石 1 個とレベル 2 の青い宝石 3 個を持っています。
- 次に、レベル 2 の青い宝石 1 個を、レベル 1 の赤い宝石 1 個とレベル 1 の青い宝石 4 個に変換します。この変換を 3 回繰り返します。
- 操作後の高橋君は、レベル 1 の赤い宝石 4 個とレベル 1 の青い宝石 12 個を持っています。
- これ以上変換を行うことはできません。
12 個より多くレベル 1 の青い宝石を手に入れることはできないので、答えは 12 になります。
入力例 2
1 5 5
出力例 2
0
高橋君がレベル 1 の青い宝石を 1 個も手に入れられない場合もあります。
入力例 3
10 5 5
出力例 3
3942349900
答えが 32 bit 整数に収まらない場合があることに注意してください。
Score : 300 points
Problem Statement
Takahashi has a red jewel of level N. (He has no other jewels.)
Takahashi can do the following operations any number of times.
- Convert a red jewel of level n (n is at least 2) into "a red jewel of level (n-1) and X blue jewels of level n".
- Convert a blue jewel of level n (n is at least 2) into "a red jewel of level (n-1) and Y blue jewels of level (n-1)".
Takahashi wants as many blue jewels of level 1 as possible. At most, how many blue jewels of level 1 can he obtain by the operations?
Constraints
- 1 \leq N \leq 10
- 1 \leq X \leq 5
- 1 \leq Y \leq 5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y
Output
Print the answer.
Sample Input 1
2 3 4
Sample Output 1
12
Takahashi can obtain 12 blue jewels of level 1 by the following conversions.
- First, he converts a red jewel of level 2 into a red jewel of level 1 and 3 blue jewels of level 2.
- After this operation, Takahashi has 1 red jewel of level 1 and 3 blue jewels of level 2.
- Next, he repeats the following conversion 3 times: converting a blue jewel of level 2 into a red jewel of level 1 and 4 blue jewels of level 1.
- After these operations, Takahashi has 4 red jewels of level 1 and 12 blue jewels of level 1.
- He cannot perform a conversion anymore.
He cannot obtain more than 12 blue jewels of level 1, so the answer is 12.
Sample Input 2
1 5 5
Sample Output 2
0
Takahashi may not be able to obtain a blue jewel of level 1.
Sample Input 3
10 5 5
Sample Output 3
3942349900
Note that the answer may not fit into a 32-bit integer type.
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N = 2^{20} 項からなる数列 A = (A_0, A_1, \dots, A_{N - 1}) があります。はじめ、全ての要素は -1 です。
Q 個のクエリを順番に処理してください。i \, (1 \leq i \leq Q) 個目のクエリは t_i = 1 または t_i = 2 を満たす整数 t_i および整数 x_i で表され、内容は以下の通りです。
- t_i = 1 のとき、以下の処理を順番に行う。
- 整数 h を h = x_i で定める。
- A_{h \bmod N} \neq -1 である間、h の値を 1 増やすことを繰り返す。この問題の制約下でこの操作が有限回で終了することは証明できる。
- A_{h \bmod N} の値を x_i で書き換える。
- t_i = 2 のとき、その時点での A_{x_i \bmod N} の値を出力する。
なお、整数 a, b に対し、a を b で割った余りを a \bmod b と表します。
制約
- 1 \leq Q \leq 2 \times 10^5
- t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
- 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
- t_i = 2 であるような i \, (1 \leq i \leq Q) が 1 つ以上存在する。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
Q
t_1 x_1
\vdots
t_{Q} x_{Q}
出力
t_i = 2 であるようなクエリに対し、それぞれ答えを 1 行に出力せよ。そのようなクエリが 1 つ以上存在することは保証される。
入力例 1
4 1 1048577 1 1 2 2097153 2 3
出力例 1
1048577 -1
x_1 \bmod N = 1 であるので、1 番目のクエリによって A_1 = 1048577 となります。
2 番目のクエリにおいて、はじめ h = x_2 ですが、A_{h \bmod N} = A_{1} \neq -1 であるので h の値を 1 増やします。すると A_{h \bmod N} = A_{2} = -1 となるので、このクエリによって A_2 = 1 となります。
3 番目のクエリにおいて、A_{x_3 \bmod N} = A_{1} = 1048577 を出力します。
4 番目のクエリにおいて、A_{x_4 \bmod N} = A_{3} = -1 を出力します。
この問題において N = 2^{20} = 1048576 は定数であり、入力では与えられないことに注意してください。
Score : 400 points
Problem Statement
There is a sequence A = (A_0, A_1, \dots, A_{N - 1}) with N = 2^{20} terms. Initially, every term is -1.
Process Q queries in order. The i-th query (1 \leq i \leq Q) is described by an integer t_i such that t_i = 1 or t_i = 2, and another integer x_i, as follows.
- If t_i = 1, do the following in order.
- Define an integer h as h = x_i.
- While A_{h \bmod N} \neq -1, keep adding 1 to h. We can prove that this process ends after finite iterations under the Constraints of this problem.
- Replace the value of A_{h \bmod N} with x_i.
- If t_i = 2, print the value of A_{x_i \bmod N} at that time.
Here, for integers a and b, a \bmod b denotes the remainder when a is divided by b.
Constraints
- 1 \leq Q \leq 2 \times 10^5
- t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
- 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
- There is at least one i (1 \leq i \leq Q) such that t_i = 2.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
t_1 x_1
\vdots
t_{Q} x_{Q}
Output
For each query with t_i = 2, print the response in one line. It is guaranteed that there is at least one such query.
Sample Input 1
4 1 1048577 1 1 2 2097153 2 3
Sample Output 1
1048577 -1
We have x_1 \bmod N = 1, so the first query sets A_1 = 1048577.
In the second query, initially we have h = x_2, for which A_{h \bmod N} = A_{1} \neq -1, so we add 1 to h. Now we have A_{h \bmod N} = A_{2} = -1, so this query sets A_2 = 1.
In the third query, we print A_{x_3 \bmod N} = A_{1} = 1048577.
In the fourth query, we print A_{x_4 \bmod N} = A_{3} = -1.
Note that, in this problem, N = 2^{20} = 1048576 is a constant and not given in input.