E - Many Balls

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

空の箱があります。
髙橋君は以下の 2 種類の魔法を好きな順番で好きな回数使えます。

  • 魔法 A :箱の中にボールを 1 つ増やす
  • 魔法 B :箱の中のボールの数を 2 倍にする

合計 \mathbf{120} 回以内の魔法で、箱の中のボールの数をちょうど N 個にする方法を 1 つ教えてください。
なお、与えられた制約のもとで条件を満たす方法が必ず存在することが示せます。

魔法以外の方法でボールの数を変化させることはできません。

制約

  • 1 \leq N \leq 10^{18}
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N

出力

A , B のみからなる文字列 S を出力せよ。
Si 文字目が 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.

F - Min Difference

実行時間制限: 2 sec / メモリ制限: 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=34 つです。 この中で最小である 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
G - Snuke Panic (1D)

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君はすぬけ君たちを捕まえようとしています。

数直線上の座標 0,1,2,3,45 箇所に穴があり、すぬけ君たちの巣につながっています。

これから N 匹のすぬけ君が穴から出てきます。i 番目のすぬけ君は時刻 T_i に座標 X_i の穴から出てきて、大きさは A_i であることがわかっています。

高橋君は時刻 0 に座標 0 におり、数直線上を単位時間あたり 1 以下の速さで移動することができます。
すぬけ君が穴から出てきたのと同じ時刻に同じ座標に高橋君がいるとき、かつ、そのときに限り、高橋君はすぬけ君を捕まえることができます。
すぬけ君を捕まえるのにかかる時間は無視できます。

高橋君が適切に行動したとき、捕まえることができるすぬけ君の大きさの合計の最大値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
  • 0 \leq X_i \leq 4
  • 1 \leq A_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
T_1 X_1 A_1
T_2 X_2 A_2
\vdots
T_N X_N A_N

出力

答えを整数として出力せよ。


入力例 1

3
1 0 100
3 3 10
5 4 1

出力例 1

101

次のように行動するのが最適です。

  • 座標 0 で待機し、時刻 11 番目のすぬけ君を捕まえる
  • 座標 4 へ移動し、時刻 53 番目のすぬけ君を捕まえる

1 番目と 2 番目のすぬけ君を両方とも捕まえることはできないので、これが最大です。


入力例 2

3
1 4 1
2 4 1
3 4 1

出力例 2

0

高橋君はすぬけ君を 1 匹も捕まえることができません。


入力例 3

10
1 4 602436426
2 1 623690081
3 3 262703497
4 4 628894325
5 3 450968417
6 1 161735902
7 1 707723857
8 2 802329211
9 0 317063340
10 2 125660016

出力例 3

2978279323

Score : 400 points

Problem Statement

Takahashi is trying to catch many Snuke.

There are five pits at coordinates 0, 1, 2, 3, and 4 on a number line, connected to Snuke's nest.

Now, N Snuke will appear from the pits. It is known that the i-th Snuke will appear from the pit at coordinate X_i at time T_i, and its size is A_i.

Takahashi is at coordinate 0 at time 0 and can move on the line at a speed of at most 1.
He can catch a Snuke appearing from a pit if and only if he is at the coordinate of that pit exactly when it appears.
The time it takes to catch a Snuke is negligible.

Find the maximum sum of the sizes of Snuke that Takahashi can catch by moving optimally.

Constraints

  • 1 \leq N \leq 10^5
  • 0 < T_1 < T_2 < \ldots < T_N \leq 10^5
  • 0 \leq X_i \leq 4
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
T_1 X_1 A_1
T_2 X_2 A_2
\vdots
T_N X_N A_N

Output

Print the answer as an integer.


Sample Input 1

3
1 0 100
3 3 10
5 4 1

Sample Output 1

101

The optimal strategy is as follows.

  • Wait at coordinate 0 to catch the first Snuke at time 1.
  • Go to coordinate 4 to catch the third Snuke at time 5.

It is impossible to catch both the first and second Snuke, so this is the best he can.


Sample Input 2

3
1 4 1
2 4 1
3 4 1

Sample Output 2

0

Takahashi cannot catch any Snuke.


Sample Input 3

10
1 4 602436426
2 1 623690081
3 3 262703497
4 4 628894325
5 3 450968417
6 1 161735902
7 1 707723857
8 2 802329211
9 0 317063340
10 2 125660016

Sample Output 3

2978279323
H - Mex and Update

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 475

問題文

長さ N の数列 A=(A_1,A_2,\dots,A_N) が与えられます。
以下の Q 個のクエリに、与えられる順番で対応してください。

k 番目のクエリは以下の形式で与えられます。

i_k x_k
  • まず、 A_{i_k} = x_k と変更する。この変更は以降のクエリにも引き継がれる。
  • その後、 A\rm{mex} を出力する。
    • A\rm{mex} とは、 A に含まれない最小の非負整数を指す。

制約

  • 入力は全て整数
  • 1 \le N,Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 1 \le i_k \le N
  • 0 \le x_k \le 10^9

入力

入力は以下の形式で標準入力から与えられる。

N Q
A_1 A_2 \dots A_N
i_1 x_1
i_2 x_2
\vdots
i_Q x_Q

出力

全体で Q 行出力せよ。
そのうち k 行目には k 番目のクエリに対する答えを整数として出力せよ。


入力例 1

8 5
2 0 2 2 1 1 2 5
4 3
4 4
6 3
8 1000000000
2 1

出力例 1

4
3
6
5
0

最初、数列 A(2,0,2,2,1,1,2,5) です。
この入力では、 5 つのクエリを処理します。

  • 1 番目のクエリで A_4 = 3 と変更し、 A=(2,0,2,3,1,1,2,5) となりました。
    • この時点で、 A\rm{mex}4 です。
  • 2 番目のクエリで A_4 = 4 と変更し、 A=(2,0,2,4,1,1,2,5) となりました。
    • この時点で、 A\rm{mex}3 です。
  • 3 番目のクエリで A_6 = 3 と変更し、 A=(2,0,2,4,1,3,2,5) となりました。
    • この時点で、 A\rm{mex}6 です。
  • 4 番目のクエリで A_8 = 1000000000 と変更し、 A=(2,0,2,4,1,3,2,1000000000) となりました。
    • この時点で、 A\rm{mex}5 です。
  • 5 番目のクエリで A_2 = 1 と変更し、 A=(2,1,2,4,1,3,2,1000000000) となりました。
    • この時点で、 A\rm{mex}0 です。

Score : 475 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_N) of length N.
Respond to the following Q queries in the order they are given.

The k-th query is given in the following format:

i_k x_k
  • First, change A_{i_k} to x_k. This change will carry over to subsequent queries.
  • Then, print the \rm{mex} of A.
    • The \rm{mex} of A is the smallest non-negative integer not contained in A.

Constraints

  • All input values are integers.
  • 1 \le N,Q \le 2 \times 10^5
  • 0 \le A_i \le 10^9
  • 1 \le i_k \le N
  • 0 \le x_k \le 10^9

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \dots A_N
i_1 x_1
i_2 x_2
\vdots
i_Q x_Q

Output

Print Q lines in total.
The k-th line should contain the answer to the k-th query as an integer.


Sample Input 1

8 5
2 0 2 2 1 1 2 5
4 3
4 4
6 3
8 1000000000
2 1

Sample Output 1

4
3
6
5
0

Initially, the sequence A is (2,0,2,2,1,1,2,5).
This input gives you five queries.

  • The first query changes A_4 to 3, making A=(2,0,2,3,1,1,2,5).
    • At this point, the \rm{mex} of A is 4.
  • The second query changes A_4 to 4, making A=(2,0,2,4,1,1,2,5).
    • At this point, the \rm{mex} of A is 3.
  • The third query changes A_6 to 3, making A=(2,0,2,4,1,3,2,5).
    • At this point, the \rm{mex} of A is 6.
  • The fourth query changes A_8 to 1000000000, making A=(2,0,2,4,1,3,2,1000000000).
    • At this point, the \rm{mex} of A is 5.
  • The fifth query changes A_2 to 1, making A=(2,1,2,4,1,3,2,1000000000).
    • At this point, the \rm{mex} of A is 0.
I - Minimum Bounding Box 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

H 行、横 W 列のグリッドがあります。

このグリッドから一様ランダムに K 個のマスを選びます。選んだマスを全て含むような(グリッドの軸に辺が平行な)最小の長方形に含まれるマスの個数がスコアとなります。

得られるスコアの期待値を \text{mod }998244353 で求めてください。

有理数 \text{mod }998244353 とは 求める期待値は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 1\leq H,W \leq 1000
  • 1\leq K \leq HW
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

H W K

出力

答えを出力せよ。


入力例 1

2 2 2

出力例 1

665496238

マス (1,1) とマス (2,2) が選ばれた場合、またはマス (1,2) とマス (2,1) が選ばれた場合の 2 通りではスコアは 4 となります。また、それ以外の 4 通りではスコアは 2 となります。

よって得られるスコアの期待値は \frac{4 \times 2 + 2 \times 4} {6} = \frac{8}{3} であり、665496238 \times 3 \equiv 8\pmod{998244353} なので 665496238 が答えとなります。


入力例 2

10 10 1

出力例 2

1

入力例 3

314 159 2653

出力例 3

639716353

Score : 500 points

Problem Statement

We have a grid with H rows and W columns.

We choose K cells in this grid uniformly at random. The score is the number of cells in the minimum rectangle (whose edges are parallel to the axes of the grid) that contains all of the chosen cells.

Find the expected score modulo 998244353.

What is rational number modulo 998244353? We can prove that the sought expected value is always a rational number. Moreover, under the Constraints of this problem, when the value is represented as \frac{P}{Q} by two coprime integers P and Q, we can prove that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find such R.

Constraints

  • 1\leq H,W \leq 1000
  • 1\leq K \leq HW
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

H W K

Output

Print the answer.


Sample Input 1

2 2 2

Sample Output 1

665496238

The score equals 4 in the following two cases: if cells (1,1) and (2,2) are chosen, or cells (1,2) and (2,1) are chosen. The other four cases yield a score of 2.

Thus, the expected score equals \frac{4 \times 2 + 2 \times 4} {6} = \frac{8}{3}. Since 665496238 \times 3 \equiv 8\pmod{998244353}, you should print 665496238.


Sample Input 2

10 10 1

Sample Output 2

1

Sample Input 3

314 159 2653

Sample Output 3

639716353