E - Slot Strategy 2 (Easy)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

この問題は G 問題の簡易版です。

3 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。ここで、S_i は数字のみからなる長さ M の文字列です。

それぞれのリールには対応するボタンがついています。高橋君は各非負整数 t について、スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す、または何もしないことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、i 番目のリールは S_i(t \bmod M)+1 文字目を表示して止まります。
ただし、t \bmod MtM で割ったあまりを表します。

高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
そのようなことが不可能であればそのことを報告してください。

制約

  • 1 \leq M \leq 100
  • M は整数
  • S_i は数字のみからなる長さ M の文字列

入力

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

M
S_1
S_2
S_3

出力

全てのリールを止めた上で、表示されている文字が全て同じであるようにすることができないなら -1 を出力せよ。
できるなら、スロットが回り始めてからそのような状態にするまでに最小で何秒かかるか出力せよ。


入力例 1

10
1937458062
8124690357
2385760149

出力例 1

6

高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。

  • スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2(0 \bmod 10)+1=1 文字目である 8 を表示して止まります。
  • スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3(2 \bmod 10)+1=3 文字目である 8 を表示して止まります。
  • スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1(6 \bmod 10)+1=7 文字目である 8 を表示して止まります。

5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。


入力例 2

20
01234567890123456789
01234567890123456789
01234567890123456789

出力例 2

20

全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。


入力例 3

5
11111
22222
33333

出力例 3

-1

表示されている文字が全て同じであるようにリールを止めることはできません。
このとき -1 を出力してください。

Score : 300 points

Problem Statement

This problem is an easier version of Problem G.

There is a slot machine with three reels.
The arrangement of symbols on the i-th reel is represented by the string S_i. Here, S_i is a string of length M consisting of digits.

Each reel has a corresponding button. For each non-negative integer t, Takahashi can either choose and press one button or do nothing exactly t seconds after the reels start spinning.
If he presses the button corresponding to the i-th reel exactly t seconds after the reels start spinning, the i-th reel will stop and display the ((t \bmod M)+1)-th character of S_i.
Here, t \bmod M denotes the remainder when t is divided by M.

Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.

Constraints

  • 1 \leq M \leq 100
  • M is an integer.
  • S_i is a string of length M consisting of digits.

Input

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

M
S_1
S_2
S_3

Output

If it is impossible to stop all the reels so that all the displayed characters are the same, print -1.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.


Sample Input 1

10
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can stop each reel as follows so that 6 seconds after the reels start spinning, all the reels display 8.

  • Press the button corresponding to the second reel 0 seconds after the reels start spinning. The second reel stops and displays 8, the ((0 \bmod 10)+1=1)-st character of S_2.
  • Press the button corresponding to the third reel 2 seconds after the reels start spinning. The third reel stops and displays 8, the ((2 \bmod 10)+1=3)-rd character of S_3.
  • Press the button corresponding to the first reel 6 seconds after the reels start spinning. The first reel stops and displays 8, the ((6 \bmod 10)+1=7)-th character of S_1.

There is no way to make the reels display the same character in 5 or fewer seconds, so print 6.


Sample Input 2

20
01234567890123456789
01234567890123456789
01234567890123456789

Sample Output 2

20

Note that he must stop all the reels and make them display the same character.


Sample Input 3

5
11111
22222
33333

Sample Output 3

-1

It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1.

F - Filling 3x3 array

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

6 個の整数 h_1, h_2, h_3, w_1, w_2, w_3 が与えられます。
縦横 3 \times 3 のマス目に、以下の条件をすべて満たすように各マスに正の整数を 1 つずつ書きこむことを考えます。

  • i=1,2,3 について、上から i 行目に書きこんだ数の和が h_i になる。
  • j=1,2,3 について、左から j 列目に書きこんだ数の和が w_j になる。

例えば (h_1, h_2, h_3) = (5, 13, 10), (w_1, w_2, w_3) = (6, 13, 9) のとき、以下の 3 通りの書きこみ方はすべて条件を満たしています。(条件を満たす書きこみ方は他にもあります)

image

さて、条件を満たす書きこみ方は全部で何通り存在しますか?

制約

  • 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
  • 入力される値はすべて整数

入力

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

h_1 h_2 h_3 w_1 w_2 w_3

出力

条件を満たす書きこみ方が何通りあるかを出力せよ。


入力例 1

3 4 6 3 3 7

出力例 1

1

条件を満たす数の書きこみ方は次の 1 通りのみです。よって 1 を出力します。

image2


入力例 2

3 4 5 6 7 8

出力例 2

0

条件を満たす書きこみ方が存在しないこともあります。


入力例 3

5 13 10 6 13 9

出力例 3

120

入力例 4

20 25 30 22 29 24

出力例 4

30613

Score : 300 points

Problem Statement

You are given six integers: h_1, h_2, h_3, w_1, w_2, and w_3.
Consider writing a positive integer on each square of a 3 \times 3 grid so that all of the following conditions are satisfied:

  • For i=1,2,3, the sum of numbers written in the i-th row from the top is h_i.
  • For j=1,2,3, the sum of numbers written in the j-th column from the left is w_i.

For example, if (h_1, h_2, h_3) = (5, 13, 10) and (w_1, w_2, w_3) = (6, 13, 9), then all of the following three ways satisfy the conditions. (There are other ways to satisfy the conditions.)

image

How many ways are there to write numbers to satisfy the conditions?

Constraints

  • 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

h_1 h_2 h_3 w_1 w_2 w_3

Output

Print the number of ways to write numbers to satisfy the conditions.


Sample Input 1

3 4 6 3 3 7

Sample Output 1

1

The following is the only way to satisfy the conditions. Thus, 1 should be printed.

image2


Sample Input 2

3 4 5 6 7 8

Sample Output 2

0

There may not be a way to satisfy the conditions.


Sample Input 3

5 13 10 6 13 9

Sample Output 3

120

Sample Input 4

20 25 30 22 29 24

Sample Output 4

30613
G - Swapping Puzzle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

HW 列の 2 つのグリッド A, B が与えられます。

1 \leq i \leq H1 \leq j \leq W を満たす各整数の組 (i, j) について、 i 行目 j 列目にあるマスをマス (i, j) と呼ぶとき、 グリッド A の マス (i, j) には整数 A_{i, j} が、 グリッド B の マス (i, j) には整数 B_{i, j} がそれぞれ書かれています。

あなたは「下記の 2 つのうちのどちらか 1 つを行う」という操作を好きな回数( 0 回でもよい)だけ繰り返します。

  • 1 \leq i \leq H-1 を満たす整数 i を選び、グリッド A の i 行目と (i+1) 行目を入れ替える。
  • 1 \leq i \leq W-1 を満たす整数 i を選び、グリッド A の i 列目と (i+1) 列目を入れ替える。

上記の操作の繰り返しによって、グリッド A をグリッド B に一致させることが可能かどうかを判定してください。 さらに、一致させることが可能な場合は、そのために行う操作回数の最小値を出力してください。

ここで、グリッド A とグリッド B が一致しているとは、 1 \leq i \leq H1 \leq j \leq W を満たす全ての整数の組 (i, j) について、 グリッド A の マス (i, j) とグリッド B の マス (i, j) に書かれた整数が等しいこととします。

制約

  • 入力される値は全て整数
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

入力

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

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

出力

グリッド A をグリッド B に一致させることが不可能な場合は -1 を、 可能な場合はグリッド A をグリッド B に一致させるために行う操作回数の最小値を出力せよ。


入力例 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

出力例 1

3

初期状態のグリッド A の 4 列目と 5 列目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

続けて、グリッド A の 2 行目と 3 行目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

最後に、グリッド A の 2 列目と 3 列目を入れ替えると、グリッド A は下記の通りになり、グリッド B に一致します。

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

上に述べた 3 回の操作でグリッド A をグリッド B に一致させることができ、 これより少ない回数の操作でグリッド A をグリッド B に一致させることはできないため、 3 を出力します。


入力例 2

2 2
1 1
1 1
1 1
1 1000000000

出力例 2

-1

問題文中の操作をどのように行ってもグリッド A をグリッド B に一致させることは不可能であるため -1 を出力します。


入力例 3

3 3
8 1 6
3 5 7
4 9 2
8 1 6
3 5 7
4 9 2

出力例 3

0

グリッド A ははじめからグリッド B に一致しています。


入力例 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

出力例 4

20

Score : 425 points

Problem Statement

You are given two grids, A and B, each with H rows and W columns.

For each pair of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, let (i, j) denote the cell in the i-th row and j-th column. In grid A, cell (i, j) contains the integer A_{i, j}. In grid B, cell (i, j) contains the integer B_{i, j}.

You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:

  • Choose an integer i satisfying 1 \leq i \leq H-1 and swap the i-th and (i+1)-th rows in grid A.
  • Choose an integer i satisfying 1 \leq i \leq W-1 and swap the i-th and (i+1)-th columns in grid A.

Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.

Here, grid A is identical to grid B if and only if, for all pairs of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, the integer written in cell (i, j) of grid A is equal to the integer written in cell (i, j) of grid B.

Constraints

  • All input values are integers.
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

Input

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

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

Output

If it is impossible to make grid A identical to grid B, output -1. Otherwise, print the minimum number of operations required to make grid A identical to grid B.


Sample Input 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

Sample Output 1

3

Swapping the fourth and fifth columns of the initial grid A yields the following grid:

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

Then, swapping the second and third rows yields the following grid:

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

Finally, swapping the second and third columns yields the following grid, which is identical to grid B:

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print 3.


Sample Input 2

2 2
1 1
1 1
1 1
1 1000000000

Sample Output 2

-1

There is no way to perform the operation to make grid A match grid B, so print -1.


Sample Input 3

3 3
8 1 6
3 5 7
4 9 2
8 1 6
3 5 7
4 9 2

Sample Output 3

0

Grid A is already identical to grid B at the beginning.


Sample Input 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

Sample Output 4

20
H - Critical Hit

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

最初、体力が N であるモンスターが 1 体います。
高橋君はモンスターに対し、モンスターの体力が 1 以上残っている限り繰り返し攻撃を行います。

高橋君は 1 回の攻撃で、\frac{P}{100} の確率でモンスターの体力を 2 減らし、 1-\frac{P}{100} の確率でモンスターの体力を 1 減らします。

モンスターの体力が 0 以下になるまでに行う攻撃回数の期待値を \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 N \leq 2\times 10^5
  • 0 \leq P \leq 100
  • 入力は全て整数

入力

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

N P

出力

高橋君の攻撃回数の期待値を \text{mod } 998244353 で出力せよ。


入力例 1

3 10

出力例 1

229596204

高橋君は 1 回の攻撃で、 \frac{10}{100}=\frac{1}{10} の確率でモンスターの体力を 2 減らし、 1-\frac{10}{100}=\frac{9}{10} の確率でモンスターの体力を 1 減らします。

  • 最初、モンスターの体力は 3 です。
  • 1 回目の攻撃の後、\frac{9}{10}の確率でモンスターの体力は 2\frac{1}{10}の確率でモンスターの体力は 1 となります。
  • 2 回目の攻撃の後、\frac{81}{100}の確率でモンスターの体力は 1\frac{18}{100} の確率でモンスターの体力は 0\frac{1}{100} の確率でモンスターの体力は -1 となります。 \frac{18}{100}+\frac{1}{100}=\frac{19}{100} の確率で体力は 0 以下となり、高橋君は 2 回で攻撃をやめます。
  • 2 回目の攻撃の後で体力が 1 残っている場合、3 回目の攻撃の後でモンスターの体力は必ず 0 以下となり、高橋君は 3 回で攻撃をやめます。

よって、期待値は 2\times \frac{19}{100}+3\times\left(1-\frac{19}{100}\right)=\frac{281}{100} となります。229596204 \times 100 \equiv 281\pmod{998244353} であるため、229596204 を出力します。


入力例 2

5 100

出力例 2

3

高橋君は 1 回の攻撃で、つねにモンスターの体力を 2 減らします。 2 回目の攻撃が終わった時点では体力が 5-2\times 2=1 残っているため、3 回目の攻撃を行う必要があります。


入力例 3

280 59

出力例 3

567484387

Score : 500 points

Problem Statement

There is a monster with initial stamina N.
Takahashi repeatedly attacks the monster while the monster's stamina remains 1 or greater.

An attack by Takahashi reduces the monster's stamina by 2 with probability \frac{P}{100} and by 1 with probability 1-\frac{P}{100}.

Find the expected value, modulo 998244353 (see Notes), of the number of attacks before the monster's stamina becomes 0 or less.

Notes

We can prove that the sought expected value is always a finite 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 show that there exists a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Print such R.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq P \leq 100
  • All values in the input are integers.

Input

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

N P

Output

Find the expected value, modulo 998244353, of the number of Takahashi's attacks.


Sample Input 1

3 10

Sample Output 1

229596204

An attack by Takahashi reduces the monster's stamina by 2 with probability \frac{10}{100}=\frac{1}{10} and by 1 with probability \frac{100-10}{100}=\frac{9}{10}.

  • The monster's initial stamina is 3.
  • After the first attack, the monster's stamina is 2 with probability \frac{9}{10} and 1 with probability \frac{1}{10}.
  • After the second attack, the monster's stamina is 1 with probability \frac{81}{100}, 0 with probability \frac{18}{100}, and -1 with probability \frac{1}{100}. With probability \frac{18}{100}+\frac{1}{100}=\frac{19}{100}, the stamina becomes 0 or less, and Takahashi stops attacking after two attacks.
  • If the stamina remains 1 after two attacks, the monster's stamina always becomes 0 or less by the third attack, so he stops attacking after three attacks.

Therefore, the expected value is 2\times \frac{19}{100}+3\times\left(1-\frac{19}{100}\right)=\frac{281}{100}. Since 229596204 \times 100 \equiv 281\pmod{998244353}, print 229596204.


Sample Input 2

5 100

Sample Output 2

3

Takahashi's attack always reduces the monster's stamina by 2. After the second attack, the stamina remains 5-2\times 2=1, so the third one is required.


Sample Input 3

280 59

Sample Output 3

567484387
I - Union of Two Sets

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

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

あなたとジャッジは下記の手順を行います。 手順はフェイズ 1 とフェイズ 2 からなり、まずフェイズ 1 を行った直後、続けてフェイズ 2 を行います。

(フェイズ 1

  • ジャッジから整数 N が与えられる。
  • あなたは 1 以上 50000 以下の整数 M を出力する。
  • さらにあなたは、すべての i = 1, 2, \ldots, M について 1 \leq l_i \leq r_i \leq N を満たす、M 個の整数の組 (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) を出力する(M 個の整数の組が相異なる必要はない)。

(フェイズ 2

  • ジャッジから整数 Q が与えられる。
  • その後、あなたとジャッジは下記の手順を Q 回繰り返す。
    • ジャッジからクエリとして 2 つの整数 L, R が与えられる。
    • それに対する応答として、あなたは 1 以上 M 以下の 2 つの整数 a, b を出力する( a = b でもよい)。 このとき、ab は下記の条件を満たさなければならない。もし満たさなかった場合は不正解となる。
      • 集合 \lbrace l_a, l_a+1, \ldots, r_a\rbrace と集合 \lbrace l_b, l_b+1, \ldots, r_b\rbrace の和集合が、集合 \lbrace L, L+1, \ldots, R\rbrace と一致する。

上記の手順を行った後、直ちにプログラムを終了することで正解となります。

制約

  • 1 \leq N \leq 4000
  • 1 \leq Q \leq 10^5
  • 1 \leq L \leq R \leq N
  • 入力はすべて整数

入出力

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

(フェイズ 1

  • まず、N が入力から与えられます。
  • 次に、1 以上 50000 以下の整数 M を出力してください。
  • その後、M 回にわたって (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) を出力してください。 具体的には、i = 1, 2, \ldots, M について、i 回目の出力では (l_i, r_i) を下記の形式で出力してください。
l_i r_i

(フェイズ 2

  • まず、Q が入力から与えられます。
  • 各クエリでは、クエリを表す整数 L, R が下記の形式で与えられます。
L R
  • 各クエリに対する応答では、2 つの整数 a, b を下記の形式で出力してください。
a b

注意点

  • 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 対話の途中で不正な出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。 特に、プログラムの実行中に実行時エラーが起こった場合に、ジャッジ結果が ではなく TLE になる可能性があることに注意してください。
  • フェイズ 2 を終了したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
  • フェイズ 2 で与えられる L, R は、あなたがフェイズ 1 で出力した (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) に応じて決定されます。

入出力例

以下は、N = 4, Q = 4 の場合の入出力例です。

入力 出力 説明
4 N が与えられます。
6 M を出力します。
3 3 (l_1, r_1) = (3, 3) を出力します。
4 4 (l_2, r_2) = (4, 4) を出力します。
1 1 (l_3, r_3) = (1, 1) を出力します。
2 4 (l_4, r_4) = (2, 4) を出力します。
1 3 (l_5, r_5) = (1, 3) を出力します。
2 2 (l_6, r_6) = (2, 2) を出力します。
4 Q が与えられます。
1 3 1 個目のクエリとして L = 1, R = 3 が与えられます。
1 5 1 個目のクエリに対する応答として a = 1, b = 5 を出力します。
3 4 2 個目のクエリとして L = 3, R = 4 が与えられます。
2 1 2 個目のクエリに対する応答として a = 2, b = 1 を出力します。
2 4 3 個目のクエリとして L = 2, R = 4 が与えられます。
4 4 3 個目のクエリに対する応答として a = 4, b = 4 を出力します。
1 1 4 個目のクエリとして L = 1, R = 1 が与えられます。
3 3 4 個目のクエリに対する応答として a = 3, b = 3 を出力します。

Score : 500 points

Problem Statement

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

You and the judge will follow the procedure below. The procedure consists of phases 1 and 2; phase 1 is immediately followed by phase 2.

(Phase 1)

  • The judge gives you an integer N.
  • You print an integer M between 1 and 50000, inclusive.
  • You also print M pairs of integers (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) such that 1 \leq l_i \leq r_i \leq N for every i = 1, 2, \ldots, M (the M pairs do not have to be distinct).

(Phase 2)

  • The judge gives you an integer Q.
  • You and the judge repeats the following Q times.
    • The judge gives you two integers L and R as a query.
    • You respond with two integers a and b between 1 and M, inclusive (possibly with a = b). Here, a and b must satisfy the condition below. Otherwise, your submission will be judged incorrect.
      • The union of the set \lbrace l_a, l_a+1, \ldots, r_a\rbrace and the set \lbrace l_b, l_b+1, \ldots, r_b\rbrace equals the set \lbrace L, L+1, \ldots, R\rbrace.

After the procedure above, terminate the program immediately to be judged correct.

Constraints

  • 1 \leq N \leq 4000
  • 1 \leq Q \leq 10^5
  • 1 \leq L \leq R \leq N
  • All values in the input are integers.

Input and Output

This is an interactive task, where your and the judge's programs interact via Standard Input and Output.

(Phase 1)

  • First, N is given from the input.
  • Next, an integer M between 1 and 50000, inclusive, should be printed.
  • Then, (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) should be printed, one at a time. Specifically, for each i = 1, 2, \ldots, M, the i-th output should be (l_i, r_i) in the following format:
l_i r_i

(Phase 2)

  • First, Q is given from the input.
  • In each query, integers L and R representing the query are given in the following format:
L R
  • In response to each query, two integers a and b should be printed in the following format:
a b

Cautions

  • At the end of each output, print a newline and flush Standard Output. Otherwise, you may get the TLE verdict.
  • If your program prints a malformed output or quits prematurely, the verdict will be indeterminate. Particularly, note that in case of a runtime error, the verdict may be or TLE instead of .
  • After phase 2, immediately terminate the program. Otherwise, the verdict will be indeterminate.
  • L and R given in phase 2 will be decided according to (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) you print in phase 1.

Sample Interaction

Below is a sample interaction with N = 4 and Q = 4.

Input Output Description
4 N is given.
6 You print M.
3 3 You print (l_1, r_1) = (3, 3).
4 4 You print (l_2, r_2) = (4, 4).
1 1 You print (l_3, r_3) = (1, 1).
2 4 You print (l_4, r_4) = (2, 4).
1 3 You print (l_5, r_5) = (1, 3).
2 2 You print (l_6, r_6) = (2, 2).
4 Q is given.
1 3 As the first query, L = 1 and R = 3 are given.
1 5 You respond with a = 1 and b = 5.
3 4 As the second query, L = 3 and R = 4 are given.
2 1 You respond with a = 2 and b = 1.
2 4 As the third query, L = 2 and R = 4 are given.
4 4 You respond with a = 4 and b = 4.
1 1 As the fourth query, L = 1 and R = 1 are given.
3 3 You respond with a = 3 and b = 3.