A - Sushi 2

Time Limit: 1 sec / Memory Limit: 256 MB

配点:200

問題文

E869120 は, AtCoder 回転寿司という店に行った.
この店には, N 個の寿司がある. 寿司にはそれぞれ 1, 2, 3, \cdots, N の番号がつけられている. 寿司 i は, 彼が来店してから a_i+kT 秒後 (k0 以上の整数) のみに取ることができる.

彼は, 寿司 1 → 寿司 2 → … → 寿司 N という順番で食べたいと思っている. しかし, 彼は貪欲なので, 寿司を一度取ってしまうとすぐに食べてしまう. 彼が N 個の寿司を食べ終わるまで, 来店してから最短何秒かかるか求めよ. ただし, 寿司を取る時間・食べる時間は無視して良いものとし, 彼は来店して 0 秒後に来る寿司も取ることができるものとする.

入力

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

N T
a_1 a_2 a_3 ... a_N

出力

全ての寿司を決められた順番で食べるとき, 食べ終わるまでにかかる最短の秒数を出力せよ.


制約

  • N1 以上 100 以下の整数.
  • T1 以上 100 以下の整数.
  • a_i0 以上 T-1 以下の整数 (1 \leq i \leq N).
  • 1 \leq i < j \leq N に対し, a_i \neq a_j.

小課題

小課題 1 [100 点]

  • N = 1.

小課題 2 [100 点]

  • 追加の制約はない.

入力例 1

1 6
4

出力例 1

4

この場合, 寿司 1 は来店してから 4, 10, 16, 22, 28, \cdots 秒後に取ることができる. 一番早いのは 4 秒後である.


入力例 2

3 10
3 7 2

出力例 2

12

寿司 1, 2, 3 をそれぞれ 3, 7, 12 秒後に取るのが最短である.


入力例 3

6 15
8 6 9 1 2 0

出力例 3

45

配点:200

Problem Statement

E869120 went to a famous sushi restaurant: "AtCoder Kuru-kuru Sushi".
In this restaurant, N sushi is being sold. They are conveniently numbered 1 through N. He can pick sushi i at a_i+kT (k is non-negative integer) seconds after he came at "AtCoder Kuru-kuru Sushi". (Note that he can also pick a sushi right after he came)

He want to eat N sushi in ascending order: from sushi 1 to sushi N. In addition, since his eating speed is very fast, he can eat a sushi in 0 second. Moreover, he always eat sushi immediately when he pick a sushi because he is greedy.
How many seconds are needed to eat all sushi in order?

Input

Input is given from Standard Input in the following format:

N T
a_1 a_2 a_3 ... a_N

Output

Print the minimum number of seconds to eat all sushi in ascending order.


Constraints

  • 1 \leq N \leq 100
  • 1 \leq T \leq 100
  • 0 \leq a_i \leq T-1
  • a_i \neq a_j (i \neq j)
  • All input values are integers.

Subtasks

Subtask 1 [100 points]

  • N = 1

Subtask 2 [100 points]

  • There are no additional constraints.

Sample Input 1

1 6
4

Sample Output 1

4

He can pick sushi 1 at 4, 10, 16, 22, \cdots seconds after he came at the restaurant.
If he pick sushi 1 at 4 seconds after he came, he can eat all sushi in ascending order in 4 seconds!


Sample Input 2

3 10
3 7 2

Sample Output 2

12

He can pick and eat sushi 1 at 3 seconds, sushi 2 at 7 seconds, sushi 3 at 12 seconds after he came at the restaurant.
Also, he can pick sushi 3 at 2 seconds after he came, but because he is greed, he eat the sushi immediately. So to eat sushi in ascending order, it took at least 12 seconds.


Sample Input 3

6 15
8 6 9 1 2 0

Sample Output 3

45
B - Emblem

Time Limit: 1 sec / Memory Limit: 256 MB

配点:300

問題文

E869120 君は, square869120Contest #5 の開催を記念するために, 1, 2, 3, \cdots, N+M の番号がつけられた N + M 個の円を用いて 2 次元平面上にエンブレムを作ろうとしている.
番号 1, 2, 3, \cdots, N の円は, 中心座標 (x_i, y_i) と半径 r_i が決まっている.
その一方で, 番号 N+1, N+2, N+3, \cdots, N+M の円は, 中心座標 (x_i, y_i) が決まっているが, 半径は決まっていない.
エンブレムに使われる円は接してもよいが, どの円も他の円と交わるまたは含んではならないとき, 最も小さい円の半径を最大化しなさい.

入力

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

N M
x_1 y_1 r_1
x_2 y_2 r_2
 :    :
x_N y_N r_N
x_{N+1} y_{N+1}
x_{N+2} y_{N+2}
 :    :
x_{N+M} y_{N+M}

出力

最も小さい円の半径としてありうる最大値を, 1 行で出力しなさい.
ただし, 相対誤差または絶対誤差が 10^{-6} 以内であるとき正解とみなされる.


制約

  • N0 以上 100 以下の整数.
  • M0 以上 100 以下の整数.
  • N + M \geq 2.
  • x_i, y_i \ (1 \leq i \leq N+M)-100 以上 100 以下の整数.
  • r_i1 以上 100 以下の整数.
  • 入力で与えられる座標はすべて異なる.
  • 番号 1, 2, 3, \cdots, N の円は交差せず, ある円がほかの円を含まない.
  • 番号 N+1, N+2, N+3, \cdots, N+M の円は, 番号 1, 2, 3, \cdots, N の円の内部または円周上にない.

小課題

小課題 1 [70 点]

  • N = 0.
  • M = 2.

小課題 2 [140 点]

  • N = 0.

小課題 3 [90 点]

  • 追加の制約はない.

入力例 1

0 2
6 3
2 4

出力例 1

2.061552812808830

この場合, 円の半径を 2 つとも \frac{\sqrt{17}}{2} に設定すれば, 2 つの円は接し, 最小の円の半径が \frac{\sqrt{17}}{2} = 2.06155\cdots になる.
例えば, 以下の図のようなエンブレムができる.


入力例 2

0 5
8 6
9 1
2 0
1 0
0 1

出力例 2

0.500000000000000

例えば, 以下の図のようなエンブレムができる.


入力例 3

3 0
5 2 3
-1 0 2
2 -6 4

出力例 3

2.000000000000000

この場合, 半径が自由に設定できる円はありません. よって, 最小の円の半径は 3 つの円の半径のうち最小である 2 になる.


入力例 4

1 1
0 0 5
6 -3

出力例 4

1.708203932499369

このとき, 番号 2 の円の半径を 3 \sqrt{5} - 5 に設定すれば, 最小の円の半径は 3 \sqrt{5} - 5 = 1.70820 \cdots になる.
例えば, 以下の図のようなエンブレムができる.

Max Score:300 points

Problem Statement

E869120 the coder is going to create an emblem which is consist of N+M circles conveniently numbered 1, 2, 3, \cdots, N+M, to celebrate the holding of "square869120Contest #5".
He has already decided the center point (x_i, y_i) and radius r_i, for circle i \ (1 \leq i \leq N).
He has also decided the center point (x_i, y_i) for circle i (N+1 \leq i \leq N+M), but he hasn't decided the radius of these circles.
Circle can contact with other circles, but no circle can intersect to or enclose any other circles.
Please maximize the radius of the smallest circle in the emblem.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1 r_1
x_2 y_2 r_2
 :    :
x_N y_N r_N
x_{N+1} y_{N+1}
x_{N+2} y_{N+2}
 :    :
x_{N+M} y_{N+M}

Output

Print the maximum value of "the radius of the smallest circle".
The output will be judged correct when the absolute or relative error is at most 10^{-6}.


Constraints

  • N is an integer between 0 and 100 (inclusive).
  • M is an integer between 0 and 100 (inclusive).
  • N + M \geq 2.
  • x_i, y_i \ (1 \leq i \leq N+M) are integers between -100 and 100 (inclusive).
  • r_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).
  • No pair of circle's center point are equal.
  • Circle which the radius has been decided neither intersect to nor enclose the other circle which the radius has been decided.
  • No center point of the circle whose the radius has not been decided is in or on a circle which the radius has been decided.

Subtasks

Subtask 1 [70 points]

  • N = 0.
  • M = 2.

Subtask 2 [140 points]

  • N = 0.

Subtask 3 [90 points]

  • There are no additional constraints.

Sample Input 1

0 2
6 3
2 4

Sample Output 1

2.061552812808830

In this case, if you set the radius of circle 1, 2 to \frac{\sqrt{17}}{2}, the two circles contacts, and the radius of the smallest circle will be maximized.
An example of emblem is as follows:


Sample Input 2

0 5
8 6
9 1
2 0
1 0
0 1

Sample Output 2

0.500000000000000

An example of emblem is as follows:


Sample Input 3

3 0
5 2 3
-1 0 2
2 -6 4

Sample Output 3

2.000000000000000

In this case, there is no circle which the radius has not been decided. So, obviously, the radius of the smallest circle is always 2.


Sample Input 4

1 1
0 0 5
6 -3

Sample Output 4

1.708203932499369

In this case, you can maximize the radius of smallest circle if you set the radius of circle 2 to 3 \sqrt{5} - 5.
An example of emblem is as follows:

C - Two Parentheses

Time Limit: 3 sec / Memory Limit: 256 MB

配点:500

問題文

E869120 と square1001 は, 同じ長さの「正しい括弧列」を持っていました. 「正しい括弧列」とは、以下のような文字列を指す.

  • () は正しい括弧列である.
  • X が正しい括弧列であるとき, (, X) をこの順につなげたものは正しい括弧列である.
  • XY が正しい括弧列であるとき, XY をこの順につなげたものは正しい括弧列である.
  • それ以外の括弧列は正しくない.

ここでは, E869120 の持つ括弧列を A, square1001 の持つ括弧列を B とする. chokudai は A, B に対して以下の操作を行った.

  • B の各文字について, ( であれば ), ) であれば ( に変えた. その後文字列 C (最初は空文字列) を定義し, 以下の 2 種類の操作のいずれかを A, B が両方空文字列となるまで繰り返した.
  • 文字列 C の末尾に A の先頭の一文字を挿入し, その後 A の先頭の一文字を削除する.
  • 文字列 C の末尾に B の先頭の一文字を挿入し, その後 B の先頭の一文字を削除する.

文字列 S が与えられる. S=C となる場合があり得るか, YesNo か判定しなさい.

入力

入力は以下の形式で標準入力から与えられる. ただし, Q 回の質問に答えなければならないことに注意せよ. i 回目の質問における S は、ここでは S_i である.

Q
S_1
S_2
 :
S_Q

出力

Q 行出力せよ.
i 行目には, i 回目の質問に対し, S=C となるような場合が存在するか, YesNo を出力すること.


制約

  • Q1 以上 50 以下の整数.
  • S_i1 以上 20 \ 000 文字以内の (, ) から成る文字列.
  • S_i の長さは 4 の倍数である.

小課題

小課題 1 [90 点]

  • |S| \leq 16 を満たす.

小課題 2 [100 点]

  • |S| \leq 50 を満たす.

小課題 3 [310 点]

  • 追加の制約はない.

入力例 1

4
(())
)()(
(())
)(()

出力例 1

No
Yes
No
Yes

例えば, 2 回目の質問 (S_2) では, A = "()", B = ")(" であり, BAAB の順番で C に挿入すれば, C= )()( となる.
その場合, A, B, C は以下のように遷移する.

  • A = "()", B = ")(", C = ""
  • A = "()", B = "(", C = ")"
  • A = ")", B = "(", C = ")("
  • A = "", B = "(", C = ")()"
  • A = "", B = "", C = ")()("

入力例 2

1
(())(()(

出力例 2

No

このケースでは、(5 個, )3 個存在する. 括弧列は () が同じ個数存在するので, これが S=C となることはあり得ない.


入力例 3

11
))(((()()())
))((())(())(
()())))(()((
)(()())()()(
)(())())(()(
)(()))()((()
)()()()())((
(())(()())()
)(())(()())(
((()())(()))
)))()())()()

出力例 3

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No

配点:500 points

The string A and B should have same length, but it was not mentioned in the problem statement. We are very sorry for inconvenience.

Problem Statement

E869120 and square1001 have correct bracket sequences with same length. "Correct bracket sequence" is defined as follows:

  • () is a correct bracket sequence.
  • If X is a correct bracket sequence, the concatenation of (, X, ) in this order is also a correct bracket sequence.
  • If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.
  • Every correct bracket sequence can be derived from the rules above.

Let the bracket sequence of E869120 as A, and the bracket sequence of square1001 as B. chokudai performed the following operations from A and B.
Note that A and B should have equal length.

  • For each character of B, if it is (, he changed to ). If it is ), he changed to (. After this operation, he defined an empty string C, and he performed either of the two operations while A or B is non-empty string.
  • Insert the first character of A into the end of string C. After this, delete the first character of A.
  • Insert the first character of B into the end of string C. After this, delete the first character of B.

You are given a string S. Please check if it is possible to be S=C or not.

Input

Input is given from Standard Input in the following format. Note: You should answer Q queries. The string S in i-th query is S_i.

Q
S_1
S_2
 :
S_Q

Output

Print Q lines.
In i-th line, print Yes if it is possible to be S_i = C, and print No if it is not possible to be S_i = C.


Constraints

  • 1 \leq Q \leq 50
  • 1 \leq |S_i| \leq 20 \ 000
  • S_i consists of ( and )
  • The length of S_i is divisible by 4.

Subtask

Subtask 1 [90 points]

  • |S| \leq 16.

Subtask 2 [100 points]

  • |S| \leq 50.

Subtask 3 [310 points]

  • There are no additional constraints.

Sample Input 1

4
(())
)()(
(())
)(()

Sample Output 1

No
Yes
No
Yes

In the 2nd query, if A is "()", B = "()" and chokudai did operations in order of BAAB, C should be )()(. In this case, A, B, C changes as follows:

  • A = "()", B = ")(", C = "" (After chokudai flips each character of B)
  • A = "()", B = "(", C = ")"
  • A = ")", B = "(", C = ")("
  • A = "", B = "(", C = ")()"
  • A = "", B = "", C = ")()("

Sample Input 2

1
(())(()(

Sample Output 2

No

In this case, there are 5 (s and 3 )s. Because the number of character ( and the number of character ) is the same for All correct bracket sequence, it is not possible to be S=C.


Sample Input 3

11
))(((()()())
))((())(())(
()())))(()((
)(()())()()(
)(())())(()(
)(()))()((()
)()()()())((
(())(()())()
)(())(()())(
((()())(()))
)))()())()()

Sample Output 3

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
D - Battle with E869120!

Time Limit: 1 sec / Memory Limit: 256 MB

配点:600

問題文

E869120 と対戦!
H \times W のマス目がある. 上から i 番目, 左から j 番目のマスを (i, j) と表す. E869120 君は, このマス目をつかう以下のゲームを考えた.
最初, マス (1, 1) に駒が置かれている. 各プレイヤーは交互に駒を 1 つずつ置いていくが, 駒を置けるマスは次の 2 つの条件を満たす必要がある.

  • このマスには駒が置かれていない.
  • 上下左右に隣り合ったマスのうち駒が置かれたマスが 1 つ以上ある.

先にマス (H, W) に駒を置いた方が勝ちである.
E869120 君はあなたにこのゲームで対戦を挑んできた. あなたは先手と後手の好きな方を選ぶことができる. あなたの目的はこのゲームに勝つことである.

入出力

この問題はインタラクティブ問題 (プログラムの入力と出力でジャッジとコミュニケーションをとる問題) であり, 入出力の形式が特殊である.

まず, マス目の行数と列数を表す 2 つの整数 HW が入力で与えられる.

H W

続いて, あなたのプログラムは先手と後手のどちらを選ぶかを出力する. 先手の場合は First と, 後手の場合は Second と出力すればよい. 末尾には改行を入れること. これを出力するとゲームが開始する.
ゲームは, 先手の場合, (☆) からスタートし, 後手の場合 (★) からスタートする. ゲームが終了するまで (☆) と (★) を繰り返す.

(☆) 自分のターンである. あなたは駒を置くマス (x, y) を決め, 次のような形式で出力し, 末尾には改行を入れること.

x y

ただし, 出力形式を間違えたりルールに違反するような置き方をしたときの結果は不定である. また, 出力の最後に flush しなければならず, そうしない場合 TLE となることがある.

(★) E869120 君のターンである. あなたは次のような形式で E869120 君が置いたマス (X, Y) を受け取る.

X Y

ただし, (X, Y) = (-1, -1) のとき終わりを表し, あなたがマス (H, W) に駒を置いた直後に入力される. これはあなたが勝ったことを表し, その後直ちにプログラムを終了しなければならない.
また, (X, Y) = (H, W) のとき E869120 君の勝ちなので, この場合も直ちにプログラムを終了しなければならない.

すべてのケースにおいてあなたのプログラムが勝った場合正答とみなされる. さて, あなたは E869120 君に勝てるかな?

制約

  • H1 以上 50 以下の整数.
  • W1 以上 50 以下の整数.
  • (H, W) \neq (1, 1).

小課題

小課題 1 [120 点]

  • H = 1.

小課題 2 [160 点]

  • 2 \leq H \leq 3.
  • 2 \leq W \leq 3.

小課題 3 [320 点]

  • 追加の制約はない.

入出力例

入力 出力 コメント
1 2 H=1,W=2 であることが分かる.
First あなたは先手を選ぶ.
1 2 マス (1,2) に駒を置く.
-1 -1 あなたの勝ちである.

Max Score:600 points

Problem Statement

Battle with E869120!
There is a grid with H rows and W columns. The square at the i-th row and j-th column will be denoted as (i, j). E869120 came up with following game which uses this grid.
At first, there are a token at (1, 1). Each player is supposed to place a token in turns, but they can place token in the square if and only if this square satisfies both following two condutions:

  • There are no token in this square.
  • Among the 4-adjacent (up, down, left, right) square, there are at least one square which contains token.

The player who placed a token in (H, W) wins.
E869120 offered you to play this game. You can choose whether you play first or E869120 play first. Your objective is to win this game.

Input and Output

This problem is "interactive task" (the task which has to take communications with input and output of program), so the format of input/output is special.

At first, the number of rows H and the columns W, are given in the input in following format:

H W

Then, your program must print whether you play first or E869120 plays first. If you play first, print First, otherwise, print Second. In the end you must put a line-break.

After that, the game starts. If you play first it will start from (☆), and if E869120 play first it will start from (★). It will repeat (☆) and (★) until the game ends.

(☆) It is your turn. Your program must decide where your program put a token. You must put line-break in the end.

x y

(★) It is E869120's turn. Your program will know the square (X, Y) which E869120 placed token from Standard Input in following format:

X Y

If (X, Y) = (-1, -1) it indicates the victory of your program, and it will be given after you put a token in (H, W). If (X, Y) = (H, W) it indicates that your program lost the game. In both case you have to terminate your program.

If your output format is invalid or the place that your program will put token is invalid, the verdict is undefined (it means it does not necessarily get WA). And also, you must flush Standard Output.**

Can you beat E869120?

Constraints

  • H, W are integers between 1 and 50 (inclusive).
  • (H, W) \neq (1, 1).

Subtasks

Subtask 1 [120 points]

  • H = 1.

Subtask 2 [160 points]

  • 2 \leq H \leq 3.
  • 2 \leq W \leq 3.

Subtask 3 [320 points]

  • There are no additional constraints.

Sample Input / Output

Input Output Comment
1 2 You can know that H=1 and W=2.
First You chooses that you play first.
1 2 You put a token in square (1,2).
-1 -1 It means you won.
E - Broken Skateboard

Time Limit: 3 sec / Memory Limit: 1024 MB

配点:800

問題文

E869120 は HW 列のマス目から成るスケートリンクに行くことを考えている. 左上のマスは (1, 1) 右下のマスは (H, W) である.
スケートリンクは 1 個のゴールのマス, いくつかの氷のマス, いくつかのコンクリートのマスから成る. 彼は特定の場所からスタートし, 最短の時間でゴールする予定である.

このスケートリンクは, 以下のような性質を持つ.

  • 氷のマスは滑るので, コンクリートのマスまたはゴールのマスにたどり着くまで同じ方向に進み続ける. ただしスケートリンクの外に出ると, ゲームオーバーである.
  • 人は, 止まっている時のみ進む方向を変えることができる.
  • ゴールのマスの上に来ると, ゲームクリアである.

しかし、彼の持っているスケートボードは既に破壊寸前である. よって, 以下のように速度が変わってしまう.

  • 彼は 1 マス動くのに k 秒かかる. 最初, k=1 である.
  • 彼がコンクリートのマスの上に来て, 停止すると, k1 増える.
  • 上下左右の 4 方向にしか動くことはできない.

彼はスケートリンクに行く前に, 全てのマス (i, j) について, マス (i, j) からスタートすると最短で何秒かかるかを知りたい.
彼の代わりにプログラムを書け.

入力

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

H W
c_{1,1}c_{1,2}c_{1,3} ... c_{1,W}
c_{2,1}c_{2,2}c_{2,3} ... c_{2,W}
c_{3,1}c_{3,2}c_{3,3} ... c_{3,W}
...
c_{H,1}c_{H,2}c_{H,3} ... c_{H,W}

1 行目には, スケートリンクの大きさ H, W が与えられる.
2 行目から H 行にわたって, スケートリンクの状態が与えられる. ただし, 各マスの状態は, 以下のような文字で表現される.

  • . は, 氷のマスを表す.
  • # は, コンクリートのマスを表す.
  • G は, ゴールのマスを表す.

出力

以下の形式で出力せよ.

d_{1,1} d_{1,2} d_{1,3} ... d_{1,W}
d_{2,1} d_{2,2} d_{2,3} ... d_{2,W}
d_{3,1} d_{3,2} d_{3,3} ... d_{3,W}
...
d_{H,1} d_{H,2} d_{H,3} ... d_{H,W}

d_{i,j} は, マス (i, j) から出発する場合のゴールするまでの最短時間を表す.
ただし, どんな方法を使ってもゴールできない場合, d_{i,j} の値は -1 となる.


制約

  • H1 以上 777 以下の整数.
  • W1 以上 777 以下の整数.
  • スケートリンクには 0 個以上 7 \ 777 個以下のコンクリートのマスが存在する.
  • スケートリンクにはゴールのマスがちょうど 1 個存在する.

小課題

小課題 1 [40 点]

  • H = 1.
  • W \leq 17.

小課題 2 [160 点]

  • H \leq 17.
  • W \leq 17.
  • スケートリンクには 77 個以下のコンクリートのマスが存在する.

小課題 3 [190 点]

  • H \leq 177.
  • W \leq 177.
  • スケートリンクには 377 個以下のコンクリートのマスが存在する.

小課題 3 [410 点]

  • 追加の制約はない.

入力例 1

6 6
......
.#....
......
......
......
...#.G

出力例 1

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


スケートリンクの状態は上図のようになっている.
マス (2, 4) からスタートすると, 上図のように 8 秒かかる.


入力例 2

1 8
#####G##

出力例 2

15 10 6 3 1 0 1 3


スケートリンクの状態は上図のようになっている.
マス (1, 1) からスタートすると, 上図のように 15 秒かかる.


入力例 3

9 9
....#..#.
#........
....#....
.#......#
...###...
##....#.#
....#...#
#....#...
.#.#..#.G

出力例 3

54 37 52 29 38 38 17 53 18
43 36 60 28 37 37 16 65 17
40 35 38 27 26 36 15 39 16
35 22 21 20 19 18 14 16 10
28 27 26 16 16 25 13 36 9
28 17 16 15 14 13 7 9 5
18 17 16 14 8 7 6 5 2
41 22 52 13 15 37 5 51 1
22 14 13 7 6 5 2 1 0

Max Score:800 points

Problem Statement

E869120 the Coder is thinking about going to a skating rink which size is H \times W. The upper-left cell is (1,1), and the lower-right cell is (H,W).
The skating rink consists of 1 goal cell, some ice cells and some concrete cells.

The skating rink has characteristics as follows:

  • Skaters move to the same direction while they're moving on ice cells.
  • Skaters can change direction if and only they are stopping.
  • If the skater came on the goal cell, he/she completes the game. However, if the skater came outside the skating rink, he/she will no longer able to complete the game.

Moreover, his skateboard is broken, so the moving speed will change as follows:

  • It takes k seconds to move one cell. Initially, k is 1.
  • If the skateboard come on a concrete cell, k will increase by 1.
  • He can move only 4 directions: up, down, left, right.

He want to know the minimum time to complete the game for each starting cell.
Please write the program instead of E869120.

Input

Input is given from Standard Input in the following format:

H W
c_{1,1}c_{1,2}c_{1,3} ... c_{1,W}
c_{2,1}c_{2,2}c_{2,3} ... c_{2,W}
c_{3,1}c_{3,2}c_{3,3} ... c_{3,W}
...
c_{H,1}c_{H,2}c_{H,3} ... c_{H,W}

In 1-st line, the size of the skating link H, W is given.
From 2-nd line to H+1-th line, the state of skating link is given. The state of each cell is represents as follows:

  • . means that the cell is an ice cell.
  • # means that the cell is a concrete cell.
  • G means that the cell is a goal cell.

Output

Print H lines in the following format:

d_{1,1} d_{1,2} d_{1,3} ... d_{1,W}
d_{2,1} d_{2,2} d_{2,3} ... d_{2,W}
d_{3,1} d_{3,2} d_{3,3} ... d_{3,W}
...
d_{H,1} d_{H,2} d_{H,3} ... d_{H,W}

d_{i,j} means the minimum time if he starts from cell (i,j). If he cannot reach the goal cell from (i,j), the value of d_{i,j} is -1.


Constraints

  • 1 \leq H \leq 777.
  • 1 \leq W \leq 777.
  • The number of concrete cells is not more than 7 \ 777.
  • The number of goal cells is exactly 1.

Subtasks

Subtask 1 [40 points]

  • H = 1.
  • W \leq 17.

Subtask 2 [160 points]

  • H \leq 17.
  • W \leq 17.
  • The number of concrete cells is not more than 77.

Subtask 3 [190 points]

  • H \leq 177.
  • W \leq 177.
  • The number of concrete cells is not more than 377.

Subtask 4 [410 points]

  • There are no additional constraints.

Sample Input 1

6 6
......
.#....
......
......
......
...#.G

Sample Output 1

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


The above-mentioned picture represents the skating rink.
If he start from (2, 4), it takes 8 seconds.


Sample Input 2

1 8
#####G##

Sample Output 2

15 10 6 3 1 0 1 3


The above-mentioned picture represents the skating rink.
If he start from (1, 1), it takes 8 seconds.


Sample Input 3

9 9
....#..#.
#........
....#....
.#......#
...###...
##....#.#
....#...#
#....#...
.#.#..#.G

Sample Output 3

54 37 52 29 38 38 17 53 18
43 36 60 28 37 37 16 65 17
40 35 38 27 26 36 15 39 16
35 22 21 20 19 18 14 16 10
28 27 26 16 16 25 13 36 9
28 17 16 15 14 13 7 9 5
18 17 16 14 8 7 6 5 2
41 22 52 13 15 37 5 51 1
22 14 13 7 6 5 2 1 0
F - Lunch Menu

Time Limit: 2 sec / Memory Limit: 256 MB

配点:1000

問題文

AtCoder カフェでは N 個の食事があり, 番号が 1 から N までつけられている. 食事 i \ (1 \leq i \leq N) のおいしさは a_i である.
square1001 君は, Q 日間 AtCoder カフェで食事をする. i \ (1 \leq i \leq Q) 日目の食事では, 番号が l_i 以上 r_i 以下の食事の中から最もおいしさの値が大きいものを選ぶ. また, そのような食事がない場合は食事をせずに帰る.

あなたは悪魔であり, 魔力で N 個中 M 個の食事をメニューからかき消して選べないようにすることができる. あなたの目的は square1001 君の選ぶ食事のおいしさの合計を最小化することである.
square1001 君の選ぶ食事のおいしさの合計の最小値を求めなさい.

入力

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

N M Q
a_1 \ a_2 \ a_3 \ \cdots \ a_N
l_1 \ r_1
l_2 \ r_2
l_3 \ r_3
 :  :
l_Q \ r_Q

出力

square1001 の選ぶ食事のおいしさの合計の最小値を, 1 行で出力しなさい.


制約

  • N1 以上 50 以下の整数.
  • M0 以上 N 以下の整数.
  • Q1 以上 50 以下の整数.
  • a_i \ (1 \leq i \leq N)1 以上 1 \ 000 \ 000 \ 000 以下の整数.
  • l_i, r_i1 \leq l_i \leq r_i \leq N を満たす整数.

小課題

小課題 1 [60 点]

  • N \leq 15.
  • Q = 1.

小課題 2 [60 点]

  • N \leq 15.

小課題 3 [250 点]

  • すべての i \ (1 \leq i \leq N) に対して, a_i = 1.

小課題 4 [630 点]

  • 追加の制約はない.

入力例 1

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

出力例 1

27

もしあなたが番号 2, 3 の食事を消すと, それぞれの日の美味しさの最大値は 4, 3, 8, 4, 8 となり, 合計が 27 となる.


入力例 2

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

出力例 2

21

入力例 3

8 5 3
1 1 1 1 1 1 1 1
2 5
1 3
6 8

出力例 3

1

Max Score:1000 points

Problem Statement

There are N foods in AtCoder Café, which is conveniently numbered 1 through N. The deliciousness of food i is exactly a_i.
square1001 is going to have Q lunches. In i-th lunch, he will select the most delicious food (the food which has highest deliciousness) among the food which the number is between l_i and r_i (inclusive). If there are no such food, he won't eat lunch this time.

You are devil and you can erase M foods from the lunch menu and their menu will no longer able to select. Your objective is to minimize the total deliciousness of food square1001 selects.
Calculate the minimum value of total deliciousness of food square1001 selects.

Input

Input is given from Standard Input in the following format:

N M Q
a_1 \ a_2 \ a_3 \ \cdots \ a_N
l_1 \ r_1
l_2 \ r_2
l_3 \ r_3
 :  :
l_Q \ r_Q

Output

Print the minimum value of total deliciousness of food square1001 selects, in one line.


Constraints.

  • N is an integer between 1 and 50 (inclusive).
  • M is an integer between 0 and N (inclusive).
  • Q is an integer between 1 and 50 (inclusive).
  • a_i \ (1 \leq i \leq N) is an integer between 1 and 1 \ 000 \ 000 \ 000 (inclusive).
  • l_i, r_i are integers which holds 1 \leq l_i \leq r_i \leq N.

Subtasks

Subtask 1 [60 points]

  • N \leq 15.
  • Q = 1.

Subtask 2 [60 points]

  • N \leq 15.

Subtask 3 [250 points]

  • a_i = 1 for all i \ (1 \leq i \leq N).

Subtask 4 [630 points]

  • There is no additional constraints.

Sample Input 1

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

Sample Output 1

27

If you delete food 2 and 3, the maximum deliciousness is 4, 3, 8, 4, 8 in order.
The total deliciousness is 27.


Sample Input 2

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

Sample Output 2

21

Sample Input 3

8 5 3
1 1 1 1 1 1 1 1
2 5
1 3
6 8

Sample Output 3

1
G - Snake Escaping 2

Time Limit: 1 sec / Memory Limit: 256 MB

配点:1200

問題文

22XX 年, AtCoder 王国には、毒蛇が大量に発生していた. 国王の chokudai は, 毒蛇を駆除するため, 見つかった毒蛇を全て倉庫の中に閉じ込めていたが, 3 年後, 再び毒蛇が王国に発生するようになっていた.
王国の square 公園には, 色が書かれているタイルがある. タイルは HW 列のマス目と考えてもよい. 毒蛇はタイルの色に擬態することによって, 再び倉庫に入れられないように逃走することにした.

さて, 今日も 1 つの毒蛇が公園に脱走していた. 毒蛇は |S| 個の節を持ち, 先頭から i 番目の節の色は, 文字列 Si 文字目である. SR, G, B から成り, それぞれ赤, 緑, 青を意味する.
この公園では, 毒蛇の隣り合った節は必ず上下左右に隣りあったマスにあるようにする.

取り敢えず, 毒蛇は「再び倉庫に入れられない」ことを信じたいので, あなたに, どんなタイルの状態と毒蛇の位置の組み合わせにおいて, 完全に擬態する, すなわちどの毒蛇の節もそのマスのタイルと同じ色となることができるかを教えてもらいたい.
その時, 毒蛇の色が与えられるので, あり得るタイルの色と毒蛇の位置の組み合わせを答えよ.

入力

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

S

出力

出力は以下の形式に従うこと.

H W
a_{1,1}a_{1, 2} ... a_{1, W}
a_{2,1}a_{2, 2} ... a_{2, W}
 :  :     :
a_{H,1}a_{H, 2} ... a_{H, W}
sx sy
V
  • 1 行目には, 公園の縦の長さ H, 横の長さ W を出力すること.
  • 2 行目から H 行にわたって, 公園の状態 a_{i, j} を出力すること. a_{i, j}R, G, B のどれかでなければならない.
  • H+2 行目には, 毒蛇の先頭の位置 (sx, sy) を出力すること. 1 \leq sx \leq H, 1 \leq sy \leq W でなければならない.
  • H+3 行目には, 毒蛇の 2 個目の節以降の動き方を出力すること. i 節目が i-1 節目の左にある場合 L, 右にある場合 R, 下にある場合 D, 上にある場合 U と出力すること.
  • H, W100 以下でなければならない.

制約

  • SR, G, B から成る, 1 文字以上 1 \ 000 \ 000 文字以下の文字列である.
  • S は, R, G, B が等確率で出現するようにランダムに作られた文字列である.

小課題

小課題 1 [80 点]

  • |S| \leq 10 \ 000 を満たす. (10 ケース)

小課題 2 [320 点]

  • |S| \leq 15 \ 000 を満たす. (10 + 10 = 20 ケース)

小課題 3 [800 点]

  • |S| = 1 \ 000 \ 000 を満たす. (20 ケース)

ただし, 小課題 3 における得点は以下のように計算される.

  • 全てのテストケースのうち最も大きかった max(H,W) の値を L とおく. その時, 得点は以下のようになる。
  • L \leq 30 のとき, 800 点.
  • 31 \leq L \leq 40 のとき, 1470 - 24L 点.
  • 41 \leq L \leq 50 のとき, 1270 - 19L 点.
  • 51 \leq L \leq 100 のとき, floor(620 \times 3^{1-\frac{L}{30}} + 20) 点.

また, すべての 30 \leq L \leq 100 に対する小課題 3 の得点は, このリンク からも見ることができる.


入力例 1

RGB

出力例 1

3 3
RGB
RGB
RGB
1 1
RR

例えば, 以下のように擬態できる.


入力例 2

GBRBGBR

出力例 2

4 3
GBR
RGR
RBR
RRR
1 1
RRLDDD

例えば, 以下のように擬態できる.


入力例 3

RGBGRGRBRR

出力例 3

3 4
RGBG
BRGR
RRRR
1 1
RRRDLLLRD

Max Score:1200 points

Problem Statement

In the year 22XX, there were many poisonous snakes in AtCoder Kingdom. The king of AtCoder Kingdom, chokudai, locked all poisonous snakes he found in storage to exterminate this snake, but three years later poisonus snakes started to emerge again.
In Square Park in AtCoder Kingdom, there are H \times W colored squares (red, green, or blue) which forms a grid with H rows and W columns.
Some escaped snakes are going to try to mimic the colored squares in Square Park, to avoid getting caught by the king, chokudai.

Today, a poisonous snake escaped to Square Park. This snake is divided into |S| part from head to tail, and color of the i-th part is S_i. S consists of 'R', 'G', and 'B', and it denotes red, green, and blue, respectively.
If the snake is in Square Park, the adjacent parts should be in adjacent cells. The snake can mimic the colored squares completely if and only if every part of the snake has same color as the square that this part is in.

The snake wants to believe that itself will not be locked in the storage again, so the snake asked you to construct the coloring of Square Park and the placement of the snake.

Input

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

S

Output

Print your answer in the following format:

H W
a_{1,1}a_{1, 2} ... a_{1, W}
a_{2,1}a_{2, 2} ... a_{2, W}
 :  :     :
a_{H,1}a_{H, 2} ... a_{H, W}
sx sy
V
  • In 1st line, print the number of row H and the number of column W of Square Park.
  • In j-th letter of i+1-th line (1 \leq i \leq H, \ 1 \leq j \leq W), print a_{i, j}, which denotes the color ('R', 'G', or 'B') of square (i, j).
  • In (H+2)-th line, print sx and sy, where (sx, sy) is the coordinate of the 1st part of the snake.
  • In (H+3)-th line, print string V with length |S|-1, whose i-th letter of V is the relation of place of i-th part and (i+1)-th part of the snake. If (i+1)-th part is at the left of i-th part, it is 'L'. For right, up, and down, it is 'R', 'U', and 'D', respectively.
  • Note that H and W should be less than or equal to 100.

Constraints

  • S is a string which consists of 'R', 'G', 'B' and the length is between 1 and 1 \ 000 \ 000 (inclusive).
  • S is randomly generated (the occurrence of 'R', 'G', and 'B' is almost equal)

Subtasks

Subtask 1 [80 points]

  • |S| \leq 10 \ 000. (10 cases)

Subtask 2 [320 points]

  • |S| \leq 15 \ 000. (10 + 10 = 20 cases)

Subtask 3 [800 points]

  • |S| = 1 \ 000 \ 000. (20 cases)

Scoring

Scoring in Subtask 3 is special.

Let L be the maximum value of max(H,W) among all subtask-3 cases. Your score will be calculated as follows:

  • If L \leq 30, you will get 800 points.
  • If 31 \leq L \leq 40, you will get 1470 - 24L points.
  • If 41 \leq L \leq 50, you will get 1270 - 19L points.
  • If 51 \leq L \leq 100, you will get floor(620 \times 3^{1-\frac{L}{30}} + 20) points.

You can see this google drive file to look scores of subtask 3 when 30 \leq L \leq 100.


Sample Input 1

RGB

Sample Output 1

3 3
RGB
RGB
RGB
1 1
RR

The snake can mimic as follows:


Sample Input 2

GBRBGBR

Sample Output 2

4 3
GBR
RGR
RBR
RRR
1 1
RRLDDD

The snake can mimic as follows:


Sample Input 3

RGBGRGRBRR

Sample Output 3

3 4
RGBG
BRGR
RRRR
1 1
RRRDLLLRD
H - Percepts of Atcoder

Time Limit: 3 sec / Memory Limit: 1024 MB

配点:1400

問題文

E869120 は, 文字列を square1001 に今年から Q 年間, 毎年プレゼントすることにした.
彼は, 文字列 S が大好きであった. そのため, プレゼントする文字列は, 文字列 S から取ることに決めた. また, 古から伝わる, AtCoder 社の教えに基づいて, プレゼントする文字列を決めることにした.
AtCoder 社の教えは, 以下のようなものである.

  • 友達に文字列をプレゼントするときは, 以下の方法で文字列を決めなさい. ここでは, あなたの大好きな文字列を S とおく.
  • 文字列 S の部分列の集合を T とおく. ただし, 文字列 S の部分列とは, S から 0 個以上の文字を取り去ってできた 1 文字以上の長さの文字列のことを指す.
  • 部分列の集合 T を辞書順でソートしたときに, K 番目となる文字列をプレゼントするべきである.

例えば, 大好きな文字列が aqua である場合, T は [a,aa,aq,aqa,aqu,aqua,au,aua,q,qa,qu,qua,u,ua] となる. K=10 のとき, 辞書順で 10 番目となる qa をプレゼントするべきである.

しかし, K の値は年ごとに変わってしまう.
そこで, i 年目の K の値 K_i が与えられるので, 各年ごとのプレゼントすべき文字列の最後の p_i 文字を求めてほしい.

入力

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

S
Q
K_1 p_1
K_2 p_2
K_3 p_3
...
K_Q p_Q

出力

Q 行出力すること.
i 行目には, i 年目にプレゼントすべき文字列の最後の p_i 文字を出力すること. ただし, 該当する文字列が存在しない場合 "-1" と出力せよ.
また, プレゼントすべき文字列が p_i 文字未満の場合, プレゼントすべき文字列をそのまま出力せよ. 例えば, プレゼントすべき文字列が "abcde" であり, p_i=7 のとき, この行には "abcde" と出力すること.


制約

  • Q1 以上 100 \ 000 以下の整数.
  • S は英小文字から成る, 1 文字以上 300 \ 000 文字以下の文字列.
  • p_i1 以上 |S| 以下の整数.
  • p_1+p_2+...+p_Q \leq 1 \ 000 \ 000
  • K_i1 以上 10^{18} 以下の整数.

小課題

小課題 1 [210 点]

  • Q=1 を満たす.

小課題 2 [320 点]

  • K_i \leq 1 \ 000 \ 000 を満たす.

小課題 3 [870 点]

  • 追加の制約はない.

入力例 1

aqua
16
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10 10
11 10
12 10
13 10
14 10
15 10
16 10

出力例 1

a
aa
aq
aqa
aqu
aqua
au
aua
q
qa
qu
qua
u
ua
-1
-1

問題文の例の通りです.


入力例 2

tourist
2
76 10
76 4

出力例 2

tourist
rist

2 個目のクエリにおいて, "tourist" の後ろ 4 文字は "rist" なので, これを出力すること.


入力例 3

eightsixnineonetwozero
6
869120 100
869120 10
869120 8
869120 6
869120 4
869120 2

出力例 3

eihinieonetwoze
ieonetwoze
onetwoze
etwoze
woze
ze

score: 1400 points

Problem Statement

E869120 the Coder decided to give a string to square1001 as a present each year for the next Q years.
Since E869120's favorite string is S, he also decided to "pick" the string, which he'll give as a present, from S.
Moreover, there's "AtCoder percepts" which has been told from ancient times, so he also decided to determine the string based on this percepts.

The contents of "AtCoder percepts" is as follows:

  • When you will give a string as a present, you should decide the string as follows.
  • Let T be the set of substring (not necessarily contiguous) of S.
  • You should give K-th string of T in lexicographical order.

For example, if his favorite string is aqua, T will be [a,aa,aq,aqa,aqu,aqua,au,aua,q,qa,qu,qua,u,ua]. If K=10, you should give qa as a present, because qa is the 10-th string in T.

However, the value of K will change every year. The i-th year of K will be K_i.
Please answer the last p_i characters of string which you should give to square1001 in i-th year.

Input

Input is given from Standard Input in the following format:

S
Q
K_1 p_1
K_2 p_2
K_3 p_3
...
K_Q p_Q

Output

  • Print Q lines.
  • In i-th line, print the last p_i characters of substring of the string which he should give in i-th year.
  • If there is no string which is applicable, print "-1".
  • If the string which he should give is less than p_i characters, print the string. For example, if p_i=7 and string = "abcde", print "abcde".

Constraints

  • 1 \leq Q \leq 100 \ 000.
  • 1 \leq |S| \leq 300 \ 000.
  • S consists of lowercase alphabets: from a to z.
  • 1 \leq p_i \leq |S|.
  • p_1+p_2+...+p_N \leq 1 \ 000 \ 000.
  • 1 \leq K_i \leq 10^{18}.
  • All input values are integers.

Subtasks

Subtask 1 [210 points]

  • Q=1.

Subtask 2 [320 points]

  • K_i \leq 1 \ 000 \ 000.

Subtask 3 [870 points]

  • There is no additional constraints.

Sample Input 1

aqua
16
1 10
2 10
3 10
4 10
5 10
6 10
7 10
8 10
9 10
10 10
11 10
12 10
13 10
14 10
15 10
16 10

Sample Output 1

a
aa
aq
aqa
aqu
aqua
au
aua
q
qa
qu
qua
u
ua
-1
-1

Please look the example in the problem statement.


Sample Input 2

tourist
2
76 10
76 4

Sample Output 2

tourist
rist

In 2-nd query, the answer is "tourist". But you should output the last 4 characters: "rist", because p_2 = 4.


Sample Input 3

eightsixnineonetwozero
6
869120 100
869120 10
869120 8
869120 6
869120 4
869120 2

Sample Output 3

eihinieonetwoze
ieonetwoze
onetwoze
etwoze
woze
ze
I - Collecting Gems is Fun

Time Limit: 2 sec / Memory Limit: 512 MB

配点:1500

問題文

ある夏の暑い日, E869120 は, AtCoder 公園に行った.
公園は, HW 列のグリッドで表される. 左上のマスは (1, 1), 右下のマスは (H, W) である.
また, この公園には N 個の宝石が落ちている. 同じマスに 2 個以上の宝石が落ちていることはない.

さて, 彼は公園にある宝石を全て集めようと思った.
彼は特別な嗅覚を持っており, 自分で事前に設定した「相対的な位置」に宝石が 1 つ以上あれば, 「宝石は近い位置にある」という情報が分かる.
ただし, 相対的な位置の例は, 以下のようなものである.

上の左から 1 番目の図において, E869120 が今マス (4, 4) にいる場合, マス (6, 5)(2, 4) にある場合は近いと判定される. なぜなら, 自分を (0, 0) として見たときにそれぞれの座標は (2, 1)(-2, 0) となり, これらは近いから.
しかし, マス (2, 3) にある宝石は「近い」と判定されない. なぜなら, 相対的に (-2, -1) は近いマスに含まれないから.

さて, 彼は最初マス (1, 1) にいる. 「近い宝石が 1 個以上ある」「そうでない」だけの情報で, 全ての宝石をできるだけ短い時間で集める方法を書いたプログラムを書け.
ただし, 彼は 1 マス {左, 右, 下, 上} のどれかの方向に移動するのに 1 秒かかり, 自分の今いるマスに宝石がある場合 0 秒で掘ることができると仮定してよい.

入出力

この問題はインタラクティブ問題 (プログラムの入力と出力でジャッジとコミュニケーションをとる問題) であり, 入出力の形式が特殊である.

まず, あなたは公園の縦の長さ H, 横の長さ W, 宝石の個数 N を以下のような形式で受け取る.

H W N

次に, あなたは「近いに含まれる相対的な位置」を設定しなければならない. 以下の形式で設定すること.

P
x_1 y_1
x_2 y_2
...
x_P y_P

1 行目に, 近いに含まれる相対的な位置の個数 P を出力しなければならない. 0 \leq P \leq 500 でなければならない.
2 行目から P 行にわたって, 相対的な位置 (x_i, y_i) を出力しなければならない. また, x_i,y_i の値の絶対値は 10^9 以下である必要がある. ただし, 自分のマスに宝石がある場合すぐに取ってしまうので, (x_i, y_i) \neq (0, 0) でなければならない.

次に, (☆) -> (★) -> (☆) -> (★) -> ... の順に以下のように処理を繰り返す.

(☆) コンピューターがあなたに情報を与える. 情報は以下のような形式で与えられる.

S

情報は以下のように分類される.

  • far: このマスに宝石はなく, かつあなたが設定した「近い位置」にも宝石はない.
  • close: このマスに宝石はないが, あなたが設定した「近い位置」に宝石はある.
  • get-far: このマスに宝石はあったので E869120 は宝石を取り, その後の状態で「近い位置」に宝石がない.
  • get-close: このマスに宝石はあったので E869120 は宝石を取り, その後の状態で「近い位置」に宝石がある.
  • get-clear: このマスに宝石があったので E869120 は宝石を取り, それで宝石が全て取られたときに必ず出力される. これを受け取ったらすぐにプログラムを終了させること.

(★) E869120 が移動する. 以下のような形式で移動の方向を出力すること.

c

cL のとき左に 1 マス, R のとき右に 1 マス, U のとき上に 1 マス, D のとき下に 1 マス動くことを意味する.
また, 公園の外に出るような移動はしてはならない.


注意

出力形式を間違えたり公園の外に出るような移動をした場合の結果は不定である. (WA になるとは限らない)
また, 出力の最後に flush しなければならず, そうしない場合 TLE となることがある.
E869120 は, 10 \ 000 回までしか移動をしてはならない.

制約

  • H = 100
  • W = 100
  • N = 200
  • 公園の中にある宝石は, 10000 個のマスからランダムに 200 個選ぶという方法で位置が決められている.

得点

この問題には 15 個テストケースがある. 1 個あたり 100 点で採点される.
移動した回数を L とおくとき, 各ケースの得点は, 以下のように定まる.

  • L \leq 2 \ 100 のとき, 100 点.
  • 2 \ 101 \leq L \leq 2 \ 260 のとき, floor(100 - \frac{L - 2100}{8}) 点.
  • 2 \ 261 \leq L \leq 2 \ 540 のとき, floor(80 - \frac{L - 2260}{14}) 点.
  • 2 \ 541 \leq L \leq 3 \ 000 のとき, floor(60 - \frac{L - 2540}{25}) 点.
  • 3 \ 001 \leq L \leq 5 \ 000 のとき, floor(41.6 - \frac{L - 3000}{125}) 点.
  • 5 \ 001 \leq L \leq 6 \ 000 のとき, 24 点.
  • 6 \ 001 \leq L \leq 7 \ 000 のとき, 21 点.
  • 7 \ 001 \leq L \leq 8 \ 000 のとき, 18 点.
  • 8 \ 001 \leq L \leq 9 \ 000 のとき, 15 点.
  • 9 \ 001 \leq L \leq 10 \ 000 のとき, 3 点.

移動回数と得点の関係を表したグラフは, 以下のようになる.

入出力例

以下の図は, この入出力例での公園の状態・「近い位置」の選び方(つまり, ここでは 8 近傍)を表している.

例えば, 以下のようなやり取りが行われる.

入力 出力 備考
4 4 4 H, W, N の値を入力.
8
-1 -1
-1 0
-1 1
0 -1
0 1
1 -1
1 0
1 1
「近い位置」に含まれる方向を入力.
get-far 今 (1, 1) にいるので, 宝石が取れる.
R (1, 2) に動く.
close マス (2, 3) にある宝石が「近い」.
D (2, 2) に動く.
close マス (2, 3), (3, 2) にある宝石が「近い」.
D (3, 2) に動く.
get-close (3, 2) の宝石を取った. マス (2, 3) にある宝石が「近い」.
R (3, 3) に動く.
close マス (2, 3), (2, 4) にある宝石が「近い」.
U (2, 3) に動く.
get-close マス (2, 3) にある宝石を取る. マス (2, 4) にある宝石が「近い」.
U (2, 4) に動く.
get-clear マス (2, 4) にある宝石を取る. これで全ての宝石が取れた.

Max Score: 1500 points

Problem Statement

One day, E869120 the Coder went to "AtCoder Natural Park".
The park represented as H*W grid. The top-left corner cell is (1,1) and the bottom-right corner cell is (H,W).
In addition, N gems drop in the park. In each cell, there are no more than 1 gem drops.

He want to collect all gems in the park.
He have "special smell-sense", so if there are 1 or more gems in relative-position that he decide beforehand, he think that "there is a gem near me".
The example of "relative position" is as follows:

In the left picture, if E869120 exists in the cell (4, 4), gems in cell (6, 5) and (2, 4) is "near". Suppose his relative position is (0, 0). In this case, the relative position of gem in example is (2, 1) and (-2, 0) in order. To see the picture, both (2, 1) and (-2, 0) is near.
But (2, 3) isn't near because its relative position is (-2, -1) and it is not near as long as see the picture.

However, he can move to one of 4-adjacent cells which share a side in 1 second. If there is a gem on the cell that E869120 exists, he pick it automatically in 0 second.
Initially, he is on the cell (1, 1). Please write the program that collect all gems in the park as fast as possible.

Input and Output

This problem is "interactive task" (the task which has to take communications with input and output of program), so the format of input/output is special.

First, you are given the size of "AtCoder Natural Park": H and W. You are also given the number of gems: N. The input format is as follows:

H W N

Second, you must set the relative-direction which includes "near", to make E869120's "special smell-sense".

P
x_1 y_1
x_2 y_2
...
x_P y_P

In the 1-st line, you should output the number of relative-position which include "near": P. 0 \leq P \leq 500 should be satisfied.
From the 2-nd line to P+1-th line, you should output relative-position: (x_i, y_i). x_i,y_i should be greater than -10^9 and less than 10^9. If there is gem on the cell that E869120 exists, he pick it instantly. So, (x_i, y_i) should not be (0, 0).

Next, it repeats the following two processes (☆) and (★) alternately (the first process is (☆)).

(☆) You are given an information as follows:

S

The information is classified as follows:

  • far: There is no gems in his cells. In addition, there is no gem in relative-position that you set.
  • close: There is no gems in his cells. In addition, there is 1 or more gems in relative-position that you set.
  • get-far: There is a gem in his cells, so E869120 picks it. In addition, there is no gem in relative-position that you set after he picks.
  • get-close: There is a gem in his cells, so E869120 picks it. In addition, there is 1 or more gems in relative-position that you set after he picks.
  • get-clear: There is a gem in his cells, so E869120 picks it. In addition, all gems collected. You have to terminate your program if you program is given get-clear.

(★) E869120 moves. You should print as follows:

c
  • If c is L, it means he moves to the left cell.
  • If c is R, it means he moves to the right cell.
  • If c is U, it means he moves to the up cell.
  • If c is D, it means he moves to the down cell.
  • He should not move to outside of the park.

Note

If your output format is invalid or he moves outside of the park, the verdict is undefined. (it means it does not necessarily get WA)
And also, you must flush Standard Output. E869120 should not move more than 10 \ 000 times.

Constraints

  • H = 100
  • W = 100
  • N = 200
  • Test case is generated randomly.

Scoring

There are 15 testcases in this problem. For each cases, the max score is 100 points.
Let L be the number of moves. The point is calculated as follows:

  • If L \leq 2 \ 100, 100 points.
  • If 2 \ 101 \leq L \leq 2 \ 260, floor(100 - \frac{L - 2100}{8}) points.
  • If 2 \ 261 \leq L \leq 2 \ 540, floor(80 - \frac{L - 2260}{14}) points.
  • If 2 \ 541 \leq L \leq 3 \ 000, floor(60 - \frac{L - 2540}{25}) points.
  • If 3 \ 001 \leq L \leq 5 \ 000, floor(41.6 - \frac{L - 3000}{125}) points.
  • If 5 \ 001 \leq L \leq 6 \ 000, 24 points.
  • If 6 \ 001 \leq L \leq 7 \ 000, 21 points.
  • If 7 \ 001 \leq L \leq 8 \ 000, 18 points.
  • If 8 \ 001 \leq L \leq 9 \ 000, 15 points.
  • If 9 \ 001 \leq L \leq 10 \ 000, 3 points.

The graph that represents the correlation between the number of moves and score is as follows:

Sample Input and Output

The state of the park and the relative direction that includes "near" is as follows:

The interaction between computer and judge is as follows:

Input Output Message
4 4 4 Input the value of H, W, N.
8
-1 -1
-1 0
-1 1
0 -1
0 1
1 -1
1 0
1 1
Input relative directions.
get-far Currently he is (1, 1). Get a gem.
R Move to (1, 2).
close A gem in (2, 3) is near.
D Move to (2, 2).
close Gems in (2, 3) and (3, 2) is near.
D Move to (3, 2).
get-close Get a gem in (3, 2). A gem in (2, 3) is still near.
R Move to (3, 3).
close Gems in (2, 3) and (2, 4) is near.
U Move to (2, 3).
get-close Get a gem in (2, 3). A gem in (2, 4) is still near.
U Move to (2, 4).
get-clear Get a gem in (2, 4). All gems collected.