A - Two Dimensional Invader

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

配点 : 500 点

問題文

あなたは 2 次元平面上に存在する魔法戦士であり,最初は点 (0, 0) に位置します.

これから N 回のイベントが起こります.i 回目 (1 \leq i \leq N) のイベントでは点 (X_i, Y_i) にモンスターが 1 体現れ,あなたはこのモンスターを剣で倒すか魔法で倒すかを選択します.

  • モンスターを剣で倒すことを選択した場合,あなたは点 (X_i, Y_i) に移動します.この際,あなたが移動する前に位置していた点と点 (X_i, Y_i) のユークリッド距離の 2 乗のコストがかかります.
  • モンスターを魔法で倒すことを選択した場合,Z_i のコストがかかります.この際,移動は行いません.

最終的に必要なコストの総和の最小値を求めて下さい.

ユークリッド距離とは 点 (a, b) と点 (c, d) のユークリッド距離を \sqrt{(a - c)^2 + (b - d)^2} と定義します.

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

制約

  • 1 \leq T \leq 10
  • 1 \leq N \leq 250\,000
  • 0 \leq X_i < 500 (1 \leq i \leq N)
  • 0 \leq Y_i < 500 (1 \leq i \leq N)
  • 1 \leq Z_i \leq 10^9 (1 \leq i \leq N)
  • T 個のテストケースにおける N の総和は 250\,000 以下
  • 入力される値はすべて整数である

入力

入力は以下の形式で標準入力から与えられる.ここで,\mathrm{case}_i (1 \leq i \leq T) は i 番目のテストケースを意味する.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_N Y_N Z_N

出力

T 行出力せよ.i 行目 (1 \leq i \leq T) には \mathrm{case}_i に対する答えを出力せよ.


入力例 1

2
3
0 4 28
2 9 11
4 5 26
7
6 0 96
0 2 54
9 0 36
0 7 81
6 5 91
2 8 71
7 6 40

出力例 1

44
231
  • 1 番目のテストケースについて,1 回目のイベントと 3 回目のイベントではモンスターを剣で倒すことを選択し,2 回目のイベントではモンスターを魔法で倒すことを選択するのが最適です.各イベントでは以下のように進行します.
    • 1 回目のイベントでは,点 (0, 0) から点 (0, 4) に移動します.この際,コストが 16 かかります.
    • 2 回目のイベントでは,コストが 11 かかります.
    • 3 回目のイベントでは,点 (0, 4) から点 (4, 5) に移動します.この際,コストが 17 かかります.
  • 最終的に必要なコストの総和は 16 + 11 + 17 = 44 です.

Score : 500 points

Problem Statement

You are a magic warrior on a two-dimensional plane, initially located at point (0, 0).

N events will now occur. In the i-th event (1 \leq i \leq N), one monster appears at point (X_i, Y_i), and you choose whether to defeat this monster with a sword or with magic.

  • If you choose to defeat the monster with a sword, you move to point (X_i, Y_i). This incurs a cost equal to the square of the Euclidean distance between the point where you were located before moving and point (X_i, Y_i).
  • If you choose to defeat the monster with magic, a cost of Z_i is incurred. In this case, you do not move.

Find the minimum total cost eventually required.

What is Euclidean distance? The Euclidean distance between point (a, b) and point (c, d) is defined as \sqrt{(a - c)^2 + (b - d)^2}.

Solve T test cases per input file.

Constraints

  • 1 \leq T \leq 10
  • 1 \leq N \leq 250\,000
  • 0 \leq X_i < 500 (1 \leq i \leq N)
  • 0 \leq Y_i < 500 (1 \leq i \leq N)
  • 1 \leq Z_i \leq 10^9 (1 \leq i \leq N)
  • The sum of N over the T test cases is at most 250\,000.
  • All input values are integers.

Input

The input is given from Standard Input in the following format. Here, \mathrm{case}_i (1 \leq i \leq T) denotes the i-th test case.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_N Y_N Z_N

Output

Output T lines. The i-th line (1 \leq i \leq T) should contain the answer for \mathrm{case}_i.


Sample Input 1

2
3
0 4 28
2 9 11
4 5 26
7
6 0 96
0 2 54
9 0 36
0 7 81
6 5 91
2 8 71
7 6 40

Sample Output 1

44
231
  • For the first test case, it is optimal to choose to defeat the monster with a sword in the first and third events, and to choose to defeat the monster with magic in the second event. Each event proceeds as follows.
    • In the first event, you move from point (0, 0) to point (0, 4). This incurs a cost of 16.
    • In the second event, a cost of 11 is incurred.
    • In the third event, you move from point (0, 4) to point (4, 5). This incurs a cost of 17.
  • The total cost eventually required is 16 + 11 + 17 = 44.
B - Three Mex

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

配点 : 500 点

問題文

非負整数 A, B, C が与えられます.以下の 3 つの条件を満たす非負整数集合 X, Y が存在するか判定し,存在する場合はその一例を示してください.

  • X, Y の各要素は 2026 以下の非負整数である.
  • \mathrm{mex}(X)=A, \mathrm{mex}(Y)=B である.
  • 非負整数集合 Z を Z=\{x\oplus y\mid x\in X, y \in Y\} で定める.このとき,\mathrm{mex}(Z)=C である.ただしここで,\oplus はビット単位 \mathrm{XOR} 演算を表します.
\mathrm{mex}(S) とは 非負整数からなる有限集合 S に対し,s\notin S を満たす最小の非負整数 s を \mathrm{mex}(S) と定義します.
ビット単位 \mathrm{XOR} 演算とは

非負整数 x, y のビット単位 \mathrm{XOR},x \oplus y は,以下のように定義されます.

  • x \oplus y を二進表記した際の 2^k (k \geq 0) の位の数は,x, y を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1,そうでなければ 0 である.
例えば,3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110).

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

制約

  • 1 \leq T \leq 10
  • 0 \leq A \leq 1000
  • 0 \leq B \leq 1000
  • 0 \leq C \leq 1000
  • 入力される値はすべて整数である

入力

入力は以下の形式で標準入力から与えられる.ここで,\mathrm{case}_i (1 \leq i \leq T) は i 番目のテストケースを意味する.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

A B C

出力

\mathrm{case}_1, \mathrm{case}_2, \cdots, \mathrm{case}_T の順に答えを出力せよ.それぞれのテストケースにおいては,以下のように出力せよ.

  • 条件を満たす非負整数集合 X, Y が存在しない場合,No を出力して,改行せよ.
  • 条件を満たす非負整数集合 X, Y が存在する場合,その一例を以下の形式で出力せよ.X, Y の要素数をそれぞれ N, M とし,X の i 番目 (1 \leq i \leq N) の要素を X_i,Y の j 番目 (1 \leq j \leq M) の要素を Y_j とする.ここで,X_1, X_2, \cdots, X_N は相異なる必要がある.また,Y_1, Y_2, \cdots, Y_M も相異なる必要がある.条件を満たす非負整数集合 X, Y が複数存在する場合,いずれを出力しても正解と判定される.
Yes
N X_1 X_2 \cdots X_N
M Y_1 Y_2 \cdots Y_M

入力例 1

4
2 3 6
1 1 0
0 0 1
0 0 0

出力例 1

Yes
3 0 1 3
4 0 1 2 4
No
Yes
1 1
1 1
Yes
0
3 2026 10 4
  • 1 番目のテストケースについて,X = \{ 0, 1, 3 \}, Y = \{ 0, 1, 2, 4 \} より Z = \{0, 1, 2, 3, 4, 5, 7 \} となり,\mathrm{mex}(X) = 2, \mathrm{mex}(Y) = 3, \mathrm{mex}(Z) = 6 が満たされます.
  • 2 番目のテストケースについて,条件を満たす非負整数集合 X, Y は存在しません.
  • 4 番目のテストケースについて,X, Y は空集合としても構いません.

Score : 500 points

Problem Statement

You are given non-negative integers A, B, C. Determine whether there exist sets of non-negative integers X, Y satisfying the following three conditions, and if they exist, show one example.

  • Each element of X, Y is a non-negative integer at most 2026.
  • \mathrm{mex}(X)=A, \mathrm{mex}(Y)=B.
  • Define the set of non-negative integers Z as Z=\{x\oplus y\mid x\in X, y \in Y\}. Then, \mathrm{mex}(Z)=C. Here, \oplus denotes the bitwise \mathrm{XOR} operation.
What is \mathrm{mex}(S)? For a finite set S of non-negative integers, \mathrm{mex}(S) is defined as the smallest non-negative integer s satisfying s\notin S.
What is the bitwise \mathrm{XOR} operation?

The bitwise \mathrm{XOR} of non-negative integers x and y, x \oplus y, is defined as follows.

  • When x \oplus y is written in binary, the digit in the 2^k (k \geq 0) place is 1 if exactly one of the digits in the 2^k place of x and y written in binary is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).

Solve T test cases per input file.

Constraints

  • 1 \leq T \leq 10
  • 0 \leq A \leq 1000
  • 0 \leq B \leq 1000
  • 0 \leq C \leq 1000
  • All input values are integers.

Input

The input is given from Standard Input in the following format. Here, \mathrm{case}_i (1 \leq i \leq T) denotes the i-th test case.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

A B C

Output

Output the answers in the order \mathrm{case}_1, \mathrm{case}_2, \cdots, \mathrm{case}_T. For each test case, output as follows.

  • If there do not exist sets of non-negative integers X, Y satisfying the conditions, output No followed by a newline.
  • If there exist sets of non-negative integers X, Y satisfying the conditions, output one example in the following format. Let N, M be the numbers of elements of X, Y, respectively, let X_i be the i-th element (1 \leq i \leq N) of X, and let Y_j be the j-th element (1 \leq j \leq M) of Y. Here, X_1, X_2, \cdots, X_N must be pairwise distinct. Also, Y_1, Y_2, \cdots, Y_M must be pairwise distinct. If there are multiple pairs of sets of non-negative integers X, Y satisfying the conditions, any of them will be judged as correct.
Yes
N X_1 X_2 \cdots X_N
M Y_1 Y_2 \cdots Y_M

Sample Input 1

4
2 3 6
1 1 0
0 0 1
0 0 0

Sample Output 1

Yes
3 0 1 3
4 0 1 2 4
No
Yes
1 1
1 1
Yes
0
3 2026 10 4
  • For the first test case, since X = \{ 0, 1, 3 \}, Y = \{ 0, 1, 2, 4 \}, we have Z = \{0, 1, 2, 3, 4, 5, 7 \}, which satisfies \mathrm{mex}(X) = 2, \mathrm{mex}(Y) = 3, \mathrm{mex}(Z) = 6.
  • For the second test case, there do not exist sets of non-negative integers X, Y satisfying the conditions.
  • For the fourth test case, X, Y may be empty sets.
C - One Sky, Many Stars

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

配点 : 600 点

問題文

長さが N の非負整数列 x = (x_1, x_2, \cdots, x_N), d = (d_1, d_2, \cdots, d_N) に対して,以下の問題 Stargazing を考えます.ここで,x_1 < x_2 < \cdots < x_N とします.

Stargazing
1 から N までの番号が付けられた N 人の人が数直線上にいます.人 i (1 \leq i \leq N) は座標 x_i にいます.また,数直線上には点とみなすことができる星が 1 つ以上存在します.
星の配置について,人 i は「自分に一番近い星までの距離は d_i である」と証言しています.なお,自分との距離が最短となる星が複数存在しても構いません.
N 人の証言に矛盾しないような星の配置が存在するか判定し,存在する場合は Yes を,そうでない場合は No を出力してください.

長さが N の非負整数列 A = (A_1, A_2, \cdots, A_N), B = (B_1, B_2, \cdots, B_N) が与えられます.ここで,A_1 < A_2 < \cdots < A_N が成立します.Q 個のクエリを処理してください.j 番目 (1 \leq j \leq Q) のクエリは以下のように表されます.

  • 1 以上 N 以下の整数 P_j と非負整数 S_j が与えられます.B_{P_j} を S_j に変更してください.その後,x, d をそれぞれ A, B とした場合の Stargazing を解いてください.なお,B_{P_j} の変更は以降のクエリにも引き継がれます.

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

制約

  • 1 \leq T \leq 10
  • 1 \leq N \leq 150\,000
  • 0 \leq A_1 < A_2 < \cdots < A_N \leq 10^9
  • 0 \leq B_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq Q \leq 150\,000
  • 1 \leq P_j \leq N (1 \leq j \leq Q)
  • 0 \leq S_j \leq 10^9 (1 \leq j \leq Q)
  • T 個のテストケースにおける N の総和は 200\,000 以下
  • T 個のテストケースにおける Q の総和は 200\,000 以下
  • 入力される値はすべて整数である

入力

入力は以下の形式で標準入力から与えられる.ここで,\mathrm{case}_i (1 \leq i \leq T) は i 番目のテストケースを意味する.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N
Q
P_1 S_1
P_2 S_2
\vdots
P_Q S_Q

出力

\mathrm{case}_1, \mathrm{case}_2, \cdots, \mathrm{case}_T の順に答えを出力せよ.それぞれのテストケースにおいては,1, 2, \cdots, Q 番目のクエリにおける Stargazing の答えを改行区切りで出力せよ.


入力例 1

2
3
2 4 8
3 1 0
2
2 3
1 7
1
0
0
1
1 1

出力例 1

Yes
No
Yes

1 番目のテストケースについて,それぞれのクエリでは以下のようになります.

  • 1 番目のクエリで変更した後,A = (2, 4, 8), B = (3, 3, 0) です.x, d をそれぞれ A, B とした場合の Stargazing の答えは Yes です.例えば,以下の図のように星が座標 -1, 7, 8 にあるならば条件を満たします.
  • 2 番目のクエリで変更した後,A = (2, 4, 8), B = (7, 3, 0) です.x, d をそれぞれ A, B とした場合の Stargazing の答えは No です.条件を満たす星の配置が存在しないことが示せるためです.

Score : 600 points

Problem Statement

For length-N sequences of non-negative integers x = (x_1, x_2, \cdots, x_N), d = (d_1, d_2, \cdots, d_N), consider the following problem Stargazing. Here, x_1 < x_2 < \cdots < x_N.

Stargazing
There are N people numbered from 1 to N on a number line. Person i (1 \leq i \leq N) is at coordinate x_i. Also, there are one or more stars on the number line, which can be regarded as points.
Regarding the arrangement of the stars, person i testifies that "the distance to the star nearest to me is d_i." There may be multiple stars at the minimum distance from the person.
Determine whether there exists an arrangement of the stars that does not contradict the testimonies of the N people, and output Yes if it exists and No otherwise.

You are given length-N sequences of non-negative integers A = (A_1, A_2, \cdots, A_N), B = (B_1, B_2, \cdots, B_N). Here, A_1 < A_2 < \cdots < A_N holds. Process Q queries. The j-th query (1 \leq j \leq Q) is represented as follows.

  • You are given an integer P_j between 1 and N, inclusive, and a non-negative integer S_j. Change B_{P_j} to S_j. Then, solve Stargazing with x, d being A, B, respectively. The change of B_{P_j} carries over to subsequent queries.

Solve T test cases per input file.

Constraints

  • 1 \leq T \leq 10
  • 1 \leq N \leq 150\,000
  • 0 \leq A_1 < A_2 < \cdots < A_N \leq 10^9
  • 0 \leq B_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq Q \leq 150\,000
  • 1 \leq P_j \leq N (1 \leq j \leq Q)
  • 0 \leq S_j \leq 10^9 (1 \leq j \leq Q)
  • The sum of N over the T test cases is at most 200\,000.
  • The sum of Q over the T test cases is at most 200\,000.
  • All input values are integers.

Input

The input is given from Standard Input in the following format. Here, \mathrm{case}_i (1 \leq i \leq T) denotes the i-th test case.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_N
Q
P_1 S_1
P_2 S_2
\vdots
P_Q S_Q

Output

Output the answers in the order \mathrm{case}_1, \mathrm{case}_2, \cdots, \mathrm{case}_T. For each test case, output the answers to Stargazing for the 1, 2, \cdots, Q-th queries, separated by newlines.


Sample Input 1

2
3
2 4 8
3 1 0
2
2 3
1 7
1
0
0
1
1 1

Sample Output 1

Yes
No
Yes

For the first test case, each query goes as follows.

  • After the change in the first query, A = (2, 4, 8), B = (3, 3, 0). The answer to Stargazing with x, d being A, B, respectively, is Yes. For example, if stars are at coordinates -1, 7, 8 as in the figure below, the conditions are satisfied.
  • After the change in the second query, A = (2, 4, 8), B = (7, 3, 0). The answer to Stargazing with x, d being A, B, respectively, is No. This is because it can be shown that there exists no arrangement of the stars satisfying the conditions.

D - Choose Your Role

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

配点 : 700 点

問題文

この問題は インタラクティブな問題 (あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題) です.

1 から N までの番号が付けられた N 個の山があります.また,それぞれの山には石が N - 1 個ずつあります.

これらを用いて先手と後手でゲームを行うことを考えます.ゲームでは先手から交互に手番を行います.

  • 先手は石が 1 つ以上存在する山を 1 個選ぶ.そこから 1 個以上 K 個以下の石を取り除く.
  • 後手は石が 1 つ以上存在する山を 1 個以上 K 個以下選ぶ.それらから石を 1 個ずつ取り除く.

先に操作ができなくなった方の負けです.あなたはこのゲームをジャッジと対戦します.まず,ジャッジにあなたが先手となるか後手となるかを宣言し,その後ゲームを行ってください.

制約

  • 2 \leq N \leq 200
  • 1 \leq K \leq N
  • 入力される値はすべて整数である

入出力

この問題はインタラクティブな問題 (あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題) です.

最初に,N, K が以下の形式で与えられるので標準入力から受け取ってください.

N K

先手として対戦する場合

  • まず First を出力して改行してください.
  • 次に,ゲームが終了するまで以下のやり取りを繰り返してください.
    • まず,あなたが取る先手の行動を出力してください.山 i から石を j 個取り除く場合,i, j をこの順に空白区切りで出力して改行してください.
    • 次に,ジャッジが取る後手の行動を受け取ってください.まず,整数 k を受け取ってください.意味は以下の通りです.
      • k = -1: あなたがこの問題で不正解と判定されたことを示します.ただちにプログラムを終了してください.
      • k = 0: あなたがこの問題で正解と判定されたことを示します.ただちにプログラムを終了してください.
      • k > 0: 続いて,k 個の整数 a_1, a_2, \cdots, a_k が空白区切りで与えられます.これは,後手が山 a_1, a_2, \cdots, a_k から 1 個ずつ石を取り除くことを示します.

後手として対戦する場合

  • まず Second を出力して改行してください.
  • 次に,ゲームが終了するまで以下のやり取りを繰り返してください.
    • まず,ジャッジが取る先手の行動を受け取ってください.2 つの整数 i, j を受け取ってください.意味は以下の通りです.
      • i = -1: このとき,j = -1 です.あなたがこの問題で不正解と判定されたことを示します.ただちにプログラムを終了してください.
      • i = 0: このとき,j = 0 です.あなたがこの問題で正解と判定されたことを示します.ただちにプログラムを終了してください.
      • i > 0: これは,先手が山 i から j 個の石を取り除いたことを示します.
    • 次に,あなたが取る後手の行動を出力してください.山 a_1, a_2, \cdots, a_k から 1 個ずつ石を取り除く場合,まず k を出力して改行してください.その後,a_1, a_2, \cdots, a_k を空白区切りで出力して改行してください.

注意点

  • 出力を行うたびに,末尾に改行を入れて標準出力を flush してください.そうしなかった場合,ジャッジ結果が TLE となる可能性があります.
  • あなたがゲームで勝利した場合,正解と判定されます.
  • あなたがゲームで負けた場合,もしくは不正な出力を行なった場合,不正解と判定されます.
  • あなたがこの問題で正解もしくは不正解と判定されたことをジャッジから受け取ったら,ただちにプログラムを終了してください.そうしない場合,ジャッジ結果は不定です.
  • 余計な改行は不正なフォーマットの出力とみなされることに注意してください.

入出力例

入出力例 1

N = 3, K = 2 として先手として対戦するときのやり取りの一例を示します.

入力 出力 説明
3 2 まず整数 N, K が与えられます.
First 先手として対戦することをジャッジに出力します.
3 1 山 3 から石を 1 個取り除きます.
2
1 3
ジャッジは山 1, 3 から石を 1 個ずつ取り除きます.
1 1 山 1 から石を 1 個取り除きます.
1
2
ジャッジは山 2 から石を 1 個取り除きます.
2 1 山 2 から石を 1 個取り除きます.
0 ジャッジは操作ができなくなったので負けとなったため,あなたがゲームで勝利し正解と判定されたことを示します.

入出力例 2

N = 2, K = 1 として後手として対戦するときのやり取りの一例を示します.

入力 出力 説明
2 1 まず整数 N, K が与えられます.
Second 後手として対戦することをジャッジに出力します.
1 1 ジャッジは山 1 から石を 1 個取り除きます.
1
2
山 2 から石を 1 個取り除きます.
0 0 ジャッジは操作ができなくなったので負けとなったため,あなたがゲームで勝利し正解と判定されたことを示します.

これらはやり取りの一例であり,実際のジャッジがこのように動作することを保証するわけではありません.

Score : 700 points

Problem Statement

This is an interactive problem (where your program interacts with the judge system via Standard Input and Output).

There are N piles numbered from 1 to N. Each pile has N - 1 stones.

Consider a game played by the first player and the second player using these. In the game, the players take turns alternately, starting with the first player.

  • The first player chooses one pile that has one or more stones, and removes between 1 and K stones, inclusive, from it.
  • The second player chooses between 1 and K piles, inclusive, each of which has one or more stones, and removes one stone from each of them.

The player who first becomes unable to make a move loses. You will play this game against the judge. First, declare to the judge whether you will be the first player or the second player, and then play the game.

Constraints

  • 2 \leq N \leq 200
  • 1 \leq K \leq N
  • All input values are integers.

Input and Output

This is an interactive problem (where your program interacts with the judge system via Standard Input and Output).

First, N and K are given in the following format; receive them from Standard Input.

N K

When playing as the first player

  • First, output First followed by a newline.
  • Then, repeat the following interaction until the game ends.
    • First, output your move as the first player. If you remove j stones from pile i, output i and j in this order, separated by a space, followed by a newline.
    • Next, receive the judge's move as the second player. First, receive an integer k. It means the following.
      • k = -1: This indicates that you have been judged as incorrect for this problem. Terminate your program immediately.
      • k = 0: This indicates that you have been judged as correct for this problem. Terminate your program immediately.
      • k > 0: Subsequently, k integers a_1, a_2, \cdots, a_k are given, separated by spaces. This indicates that the second player removes one stone from each of piles a_1, a_2, \cdots, a_k.

When playing as the second player

  • First, output Second followed by a newline.
  • Then, repeat the following interaction until the game ends.
    • First, receive the judge's move as the first player. Receive two integers i and j. They mean the following.
      • i = -1: In this case, j = -1. This indicates that you have been judged as incorrect for this problem. Terminate your program immediately.
      • i = 0: In this case, j = 0. This indicates that you have been judged as correct for this problem. Terminate your program immediately.
      • i > 0: This indicates that the first player removed j stones from pile i.
    • Next, output your move as the second player. If you remove one stone from each of piles a_1, a_2, \cdots, a_k, first output k followed by a newline. Then, output a_1, a_2, \cdots, a_k, separated by spaces, followed by a newline.

Notes

  • For each output, add a newline at the end and flush Standard Output. Otherwise, you may receive a TLE verdict.
  • If you win the game, you will be judged as correct.
  • If you lose the game, or if you make a malformed output, you will be judged as incorrect.
  • Once you receive from the judge that you have been judged correct or incorrect for this problem, terminate your program immediately. Otherwise, the verdict is indeterminate.
  • Note that extra newlines are considered as a malformed output.

Sample Interaction

Sample Interaction 1

The following is an example of the interaction when N = 3, K = 2 and you play as the first player.

Input Output Explanation
3 2 First, the integers N, K are given.
First Output to the judge that you will play as the first player.
3 1 Remove one stone from pile 3.
2
1 3
The judge removes one stone from each of piles 1, 3.
1 1 Remove one stone from pile 1.
1
2
The judge removes one stone from pile 2.
2 1 Remove one stone from pile 2.
0 The judge has lost because it became unable to make a move, which indicates that you won the game and have been judged as correct.

Sample Interaction 2

The following is an example of the interaction when N = 2, K = 1 and you play as the second player.

Input Output Explanation
2 1 First, the integers N, K are given.
Second Output to the judge that you will play as the second player.
1 1 The judge removes one stone from pile 1.
1
2
Remove one stone from pile 2.
0 0 The judge has lost because it became unable to make a move, which indicates that you won the game and have been judged as correct.

These are merely examples of the interaction, and it is not guaranteed that the actual judge behaves in this way.

E - Odd Inversion

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

配点 : 800 点

問題文

長さが N の正整数列 A = (A_1, A_2, \cdots, A_N) が与えられます.ここで,A_1 \leq A_2 \leq \cdots \leq A_N が成立します.

以下の条件を満たす長さが N の正整数列 B としてありうる個数を 998\,244\,353 で割ったあまりを求めてください.

  • 1 \leq B_i \leq A_i (1 \leq i \leq N)
  • B の転倒数は奇数

ここで,B の転倒数とは 1 \leq i < j \leq N を満たす整数の組 (i, j) で B_i > B_j であるものの個数を表します.

1 つの入力ファイルにつき,T 個のテストケースを解いてください.

制約

  • 1 \leq T \leq 10\,000
  • 1 \leq N \leq 200\,000
  • 1 \leq A_1 \leq A_2 \leq \cdots \leq A_N \leq 10^9
  • T 個のテストケースにおける N の総和は 400\,000 以下
  • 入力される値はすべて整数である

入力

入力は以下の形式で標準入力から与えられる.ここで,\mathrm{case}_i (1 \leq i \leq T) は i 番目のテストケースを意味する.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各テストケースは以下の形式で与えられる.

N
A_1 A_2 \cdots A_N

出力

T 行出力せよ.i 行目 (1 \leq i \leq T) には \mathrm{case}_i に対する答えを出力せよ.


入力例 1

3
2
2 3
3
2 2 3
5
699498 707559 724350 759726 769395

出力例 1

1
3
610589585
  • 1 番目のテストケースについて,(2, 1) が条件を満たします.
  • 2 番目のテストケースについて,(1, 2, 1), (2, 1, 2), (2, 1, 3) が条件を満たします.

Score : 800 points

Problem Statement

You are given a sequence of positive integers A = (A_1, A_2, \cdots, A_N) of length N. Here, A_1 \leq A_2 \leq \cdots \leq A_N holds.

Find the number, modulo 998\,244\,353, of possible sequences of positive integers B of length N satisfying the following conditions.

  • 1 \leq B_i \leq A_i (1 \leq i \leq N)
  • The inversion number of B is odd.

Here, the inversion number of B is the number of pairs of integers (i, j) satisfying 1 \leq i < j \leq N such that B_i > B_j.

Solve T test cases per input file.

Constraints

  • 1 \leq T \leq 10\,000
  • 1 \leq N \leq 200\,000
  • 1 \leq A_1 \leq A_2 \leq \cdots \leq A_N \leq 10^9
  • The sum of N over the T test cases is at most 400\,000.
  • All input values are integers.

Input

The input is given from Standard Input in the following format. Here, \mathrm{case}_i (1 \leq i \leq T) denotes the i-th test case.

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each test case is given in the following format:

N
A_1 A_2 \cdots A_N

Output

Output T lines. The i-th line (1 \leq i \leq T) should contain the answer for \mathrm{case}_i.


Sample Input 1

3
2
2 3
3
2 2 3
5
699498 707559 724350 759726 769395

Sample Output 1

1
3
610589585
  • For the first test case, (2, 1) satisfies the conditions.
  • For the second test case, (1, 2, 1), (2, 1, 2), (2, 1, 3) satisfy the conditions.
F - Two Unbalanced Subtrees

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

配点 : 800 点

問題文

正整数 N が与えられます.

頂点数が 2^N - 1 の完全二分木があります.頂点には 1 から 2^N - 1 までの番号が付いています.

頂点 1 が根であり,頂点 i (1 \leq i < 2^{N - 1}) について,頂点 i は頂点 2i と頂点 2i + 1 を子として持ちます.

以下の条件を満たすように各頂点に 1 以上 2^N - 1 以下の整数を書き込む (ただし,書き込む 2^N - 1 個の整数は相異なるようにする) 方法のうち,頂点 1 に書き込まれる整数を最小化するものを一つ求めてください.

  • 頂点 i (1 \leq i < 2^{N - 1}) について,頂点 i に書き込まれた整数は,頂点 2i を根とする部分木の各頂点に書かれた整数の総和と,頂点 2i+1 を根とする部分木の各頂点に書かれた整数の総和の差の絶対値である.

この問題の制約下で条件を満たす整数の書き込み方が必ず存在することが証明できます.

制約

  • 2 \leq N \leq 18
  • 入力される値はすべて整数である

入力

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

N

出力

頂点 i (1 \leq i \leq 2^N - 1) に書き込む整数を P_i として以下の形式で出力せよ.

P_1 P_2 \cdots P_{2^N - 1}

(P_1, P_2, \cdots, P_{2^N - 1}) は (1, 2, \cdots, 2^N - 1) の順列である必要がある.条件を満たす書き込む方法のうち,頂点 1 に書き込まれる整数を最小化するものが複数存在する場合には,そのどれを出力しても正解と見なされる.


入力例 1

2

出力例 1

1 2 3 

この書き込み方が条件を満たすことは,以下のようにして確かめられます.また明らかに頂点 1 に書き込まれた整数は最小化されています.

  • i = 1: 頂点 2 を根とする部分木の各頂点に書かれた整数の総和は 2,頂点 3 を根とする部分木の各頂点に書かれた整数の総和は 3 であり,∣2 − 3∣ = 1 である.

頂点 1, 2, 3 にそれぞれ 2, 3, 1 を書く方法は,条件を満たしていますが頂点 1 に書き込まれた整数は最小値ではないので不正解と判定されます.

Score : 800 points

Problem Statement

You are given a positive integer N.

There is a perfect binary tree with 2^N - 1 vertices. The vertices are numbered from 1 to 2^N - 1.

Vertex 1 is the root, and for each vertex i (1 \leq i < 2^{N - 1}), vertex i has vertex 2i and vertex 2i + 1 as its children.

Among the ways to write an integer between 1 and 2^N - 1, inclusive, on each vertex (where the 2^N - 1 written integers must be pairwise distinct) such that the following condition is satisfied, find one that minimizes the integer written on vertex 1.

  • For each vertex i (1 \leq i < 2^{N - 1}), the integer written on vertex i is the absolute difference between the sum of the integers written on the vertices of the subtree rooted at vertex 2i and the sum of the integers written on the vertices of the subtree rooted at vertex 2i+1.

It can be proved that, under the constraints of this problem, a way of writing integers satisfying the condition always exists.

Constraints

  • 2 \leq N \leq 18
  • All input values are integers.

Input

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

N

Output

Let P_i be the integer written on vertex i (1 \leq i \leq 2^N - 1), and output in the following format:

P_1 P_2 \cdots P_{2^N - 1}

(P_1, P_2, \cdots, P_{2^N - 1}) must be a permutation of (1, 2, \cdots, 2^N - 1). If there are multiple ways of writing satisfying the condition that minimize the integer written on vertex 1, any of them will be considered correct.


Sample Input 1

2

Sample Output 1

1 2 3 

It can be verified as follows that this way of writing satisfies the condition. Also, the integer written on vertex 1 is clearly minimized.

  • i = 1: The sum of the integers written on the vertices of the subtree rooted at vertex 2 is 2, the sum of the integers written on the vertices of the subtree rooted at vertex 3 is 3, and ∣2 − 3∣ = 1.

The way of writing 2, 3, 1 on vertices 1, 2, 3, respectively, satisfies the condition, but the integer written on vertex 1 is not the minimum, so it will be judged as incorrect.