A - Lucky Direction

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

8 種類の方角(北、東、西、南、北東、北西、南東、南西)のいずれかを表す文字列 D が与えられます。方角とそれを表す文字列は以下のように対応しています。

  • 北:N
  • 東:E
  • 西:W
  • 南:S
  • 北東:NE
  • 北西:NW
  • 南東:SE
  • 南西:SW

D が表す方角の反対の方角を表す文字列を出力してください。

制約

  • D は N, E, W, S, NE, NW, SE, SW のいずれか

入力

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

D

出力

答えを出力せよ。


入力例 1

N

出力例 1

S

北の反対の方角である南を表す S を出力してください。


入力例 2

SE

出力例 2

NW

南東の反対の方角である北西を表す NW を出力してください。

Score : 100 points

Problem Statement

You are given a string D representing one of the eight directions (north, east, west, south, northeast, northwest, southeast, southwest). The correspondence between the directions and their representing strings is as follows.

  • North: N
  • East: E
  • West: W
  • South: S
  • Northeast: NE
  • Northwest: NW
  • Southeast: SE
  • Southwest: SW

Print the string representing the direction opposite to the direction denoted by D.

Constraints

  • D is one of N, E, W, S, NE, NW, SE, SW.

Input

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

D

Output

Print the answer.


Sample Input 1

N

Sample Output 1

S

Print S, which represents south, the direction opposite to north.


Sample Input 2

SE

Sample Output 2

NW

Print NW, which represents northwest, the direction opposite to southeast.

B - Cut

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N 枚からなるカードの山があり、上から i 枚目のカードには整数 A_i が書かれています。

山の下から K 枚のカードを取り出し、順序を保ったまま山の一番上に乗せました。

この操作後の山の上から順に、カードに書かれた整数を出力してください。

制約

  • 1 \leq K < N \leq 100
  • 1 \leq A_i \leq 100
  • 入力は全て整数

入力

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

N K
A_1 A_2 \ldots A_N

出力

操作後の山の上から i 枚目のカードに書かれた整数を B_i とする。B_1,B_2,\ldots,B_N をこの順に空白区切りで出力せよ。


入力例 1

5 3
1 2 3 4 5

出力例 1

3 4 5 1 2

最初、カードに書かれた整数は山の上から順に 1,2,3,4,5 です。

山の下から 3 枚のカードを取り出し、そのまま山の一番上に乗せたあと、カードに書かれた整数は山の上から順に 3,4,5,1,2 となります。


入力例 2

6 2
1 2 1 2 1 2

出力例 2

1 2 1 2 1 2

カードに書かれている整数は相異なるとは限りません。

Score : 100 points

Problem Statement

There is a stack of N cards, and the i-th card from the top has an integer A_i written on it.

You take K cards from the bottom of the stack and place them on top of the stack, maintaining their order.

Print the integers written on the cards from top to bottom after the operation.

Constraints

  • 1 \leq K < N \leq 100
  • 1 \leq A_i \leq 100
  • All input values are integers.

Input

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

N K
A_1 A_2 \ldots A_N

Output

Let B_i be the integer written on the i-th card from the top of the stack after the operation. Print B_1,B_2,\ldots,B_N in this order, separated by spaces.


Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

3 4 5 1 2

Initially, the integers written on the cards are 1,2,3,4,5 from top to bottom.

After taking three cards from the bottom of the stack and placing them on top, the integers written on the cards become 3,4,5,1,2 from top to bottom.


Sample Input 2

6 2
1 2 1 2 1 2

Sample Output 2

1 2 1 2 1 2

The integers written on the cards are not necessarily distinct.

C - Coloring Matrix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

各要素が 0 あるいは 1 である N 行 N 列の行列 A, B が与えられます。
A の i 行目 j 列目の要素を A_{i,j}、B の i 行目 j 列目の要素を B_{i,j} で表します。
A を適切に回転することで、 A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできるか判定してください。
ただし、A を回転するとは、以下の操作を好きな回数(0 回でもよい)繰り返すことをいいます。

  • 1 \leq i, j \leq N を満たすすべての整数の組 (i, j) について同時に A_{i,j} を A_{N + 1 - j,i} で置き換える

制約

  • 1 \leq N \leq 100
  • A, B の各要素は 0 か 1 のいずれか
  • 入力はすべて整数

入力

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

出力

A を適切に回転することで、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているようにできる場合 Yes を、そうでない場合 No を出力せよ。


入力例 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

出力例 1

Yes

はじめ、A は

0 1 1
1 0 0
0 1 0

です。
1 回操作を行うと、A は

0 1 0
1 0 1 
0 0 1

となります。
もう 1 度操作を行うと、A は

0 1 0
0 0 1
1 1 0

となります。
このとき、A_{i,j} = 1 であるすべての整数の組 (i, j) について B_{i,j} = 1 が成り立っているので、Yes を出力します。


入力例 2

2
0 0
0 0
1 1
1 1

出力例 2

Yes

入力例 3

5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0

出力例 3

No

Score : 200 points

Problem Statement

You are given N-by-N matrices A and B where each element is 0 or 1.
Let A_{i,j} and B_{i,j} denote the element at the i-th row and j-th column of A and B, respectively.
Determine whether it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1.
Here, to rotate A is to perform the following operation zero or more times:

  • for every pair of integers (i, j) such that 1 \leq i, j \leq N, simultaneously replace A_{i,j} with A_{N + 1 - j,i}.

Constraints

  • 1 \leq N \leq 100
  • Each element of A and B is 0 or 1.
  • All values in the input are integers.

Input

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

N
A_{1,1} A_{1,2} \ldots A_{1,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
B_{1,1} B_{1,2} \ldots B_{1,N}
\vdots
B_{N,1} B_{N,2} \ldots B_{N,N}

Output

If it is possible to rotate A so that B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, print Yes; otherwise, print No.


Sample Input 1

3
0 1 1
1 0 0
0 1 0
1 1 0
0 0 1
1 1 1

Sample Output 1

Yes

Initially, A is :

0 1 1
1 0 0
0 1 0

After performing the operation once, A is :

0 1 0
1 0 1 
0 0 1

After performing the operation once again, A is :

0 1 0
0 0 1
1 1 0

Here, B_{i,j} = 1 for every pair of integers (i, j) such that A_{i,j} = 1, so you should print Yes.


Sample Input 2

2
0 0
0 0
1 1
1 1

Sample Output 2

Yes

Sample Input 3

5
0 0 1 1 0
1 0 0 1 0
0 0 1 0 1
0 1 0 1 0
0 1 0 0 1
1 1 0 0 1
0 1 1 1 0
0 0 1 1 1
1 0 1 0 1
1 1 0 1 0

Sample Output 3

No
D - Magic Square

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

3 以上の奇数 N が与えられます。

N 行 N 列のマス目があり、はじめどのマスも空白です。 今から、以下の手順に従ってこのマス目の各マスに整数を書き込みます。 なお、上から i+1 行目、左から j+1 列目 (0\leq i<N, 0\leq j<N) のマスを (i,j) と表記することとします。

  1. マス (0,\frac{N-1}{2}) に 1 を書き込む。
  2. 次の操作を N^2-1 回繰り返す。
    • 前回整数を書き込んだマスを (r,c)、書き込んだ整数を k としたとき、マス ((r-1) \bmod N, (c+1) \bmod N) が空白ならばそのマスに、そうでなければマス ((r+1) \bmod N,c) に k+1 を書き込む。 ここで、x \bmod N は x を N で割ったあまりを表す。

この手順においてそれぞれのマスに書き込まれる整数を求めてください。 なお、どのマスもちょうど 1 回だけ整数を書き込まれることが証明できます。

制約

  • 3\leq N \leq 99
  • N は奇数

入力

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

N

出力

マス (i,j) に書き込まれる整数を a_{i,j} として、以下の形式で出力せよ。

a_{0,0} a_{0,1} \dots a_{0,N-1}
\vdots
a_{N-1,0} a_{N-1,1} \dots a_{N-1,N-1}

入力例 1

3

出力例 1

8 1 6
3 5 7
4 9 2

以下のように各マスに整数が書き込まれていきます。

  1. マス (0,\frac{3-1}{2})=(0,1) に 1 を書き込む。
  2. マス ((0-1) \bmod 3, (1+1) \bmod 3)=(2,2) は空白なので、そこに 2 を書き込む。
  3. マス ((2-1) \bmod 3, (2+1) \bmod 3)=(1,0) は空白なので、そこに 3 を書き込む。
  4. マス ((1-1) \bmod 3, (0+1) \bmod 3)=(0,1) は空白ではないので、マス ((1+1) \bmod 3,0)=(2,0) に 4 を書き込む。
  5. \vdots

入力例 2

5

出力例 2

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

Score : 200 points

Problem Statement

You are given an odd number N that is at least 3.

There is a grid with N rows and N columns, where all cells are initially empty. Now, you will write integers in each cell of this grid according to the following procedure. Let (i,j) denote the cell at the (i+1)-th row from the top and (j+1)-th column from the left (0\leq i<N, 0\leq j<N).

  1. Write 1 in cell (0,\frac{N-1}{2}).
  2. Repeat the following operation N^2-1 times:
    • Let (r,c) be the cell where an integer was written last time, and k be the integer written. If cell ((r-1) \bmod N, (c+1) \bmod N) is empty, write k+1 in that cell; otherwise, write k+1 in cell ((r+1) \bmod N,c). Here, x \bmod N denotes the remainder when x is divided by N.

Find the integer that will be written in each cell in this procedure. It can be proved that each cell will have an integer written in it exactly once.

Constraints

  • 3\leq N \leq 99
  • N is an odd number.

Input

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

N

Output

Let a_{i,j} be the integer written in cell (i,j), and print it in the following format:

a_{0,0} a_{0,1} \dots a_{0,N-1}
\vdots
a_{N-1,0} a_{N-1,1} \dots a_{N-1,N-1}

Sample Input 1

3

Sample Output 1

8 1 6
3 5 7
4 9 2

Integers are written in each cell as follows:

  1. Write 1 in cell (0,\frac{3-1}{2})=(0,1).
  2. Cell ((0-1) \bmod 3, (1+1) \bmod 3)=(2,2) is empty, so write 2 there.
  3. Cell ((2-1) \bmod 3, (2+1) \bmod 3)=(1,0) is empty, so write 3 there.
  4. Cell ((1-1) \bmod 3, (0+1) \bmod 3)=(0,1) is not empty, so write 4 in cell ((1+1) \bmod 3,0)=(2,0).
  5. \vdots

Sample Input 2

5

Sample Output 2

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
E - Sigma Problem

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

正整数 x,y に対して f(x,y) を「(x+y) を 10^8 で割ったあまり」として定義します。

長さ N の正整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を求めてください。

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j)


制約

  • 2\leq N\leq 3\times 10^5
  • 1\leq A_i < 10^8
  • 入力される数値は全て整数

入力

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

N 
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
3 50000001 50000002

出力例 1

100000012
  • f(A_1,A_2)=50000004
  • f(A_1,A_3)=50000005
  • f(A_2,A_3)=3

なので、答えは f(A_1,A_2)+f(A_1,A_3)+f(A_2,A_3) = 100000012 です。

総和を 10^8 で割ったあまりを求めるわけではないことに注意してください。


入力例 2

5
1 3 99999999 99999994 1000000

出力例 2

303999988

Score: 300 points

Problem Statement

For positive integers x and y, define f(x, y) as the remainder of (x + y) divided by 10^8.

You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N. Find the value of the following expression:

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j).


Constraints

  • 2 \leq N \leq 3\times 10^5
  • 1 \leq A_i < 10^8
  • All input values are integers.

Input

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

N 
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3
3 50000001 50000002

Sample Output 1

100000012
  • f(A_1,A_2)=50000004
  • f(A_1,A_3)=50000005
  • f(A_2,A_3)=3

Thus, the answer is f(A_1,A_2) + f(A_1,A_3) + f(A_2,A_3) = 100000012.

Note that you are not asked to compute the remainder of the sum divided by 10^8.


Sample Input 2

5
1 3 99999999 99999994 1000000

Sample Output 2

303999988
F - Doukasen

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

N 本の導火線を一直線に接着したものがあります。左から i 本目の導火線は長さが A_i cm で、 1 秒あたり B_i cm の一定の速さで燃えます。

この導火線の左端と右端から同時に火をつけるとき、 2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 1000
  • 入力は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

2 つの火がぶつかる場所が着火前の導火線の左端から何 cm の地点か(単位を除いて)出力せよ。

想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。


入力例 1

3
1 1
2 1
3 1

出力例 1

3.000000000000000

着火前の導火線の左端から 3 cm の地点で 2 つの火がぶつかります。


入力例 2

3
1 3
2 2
3 1

出力例 2

3.833333333333333

入力例 3

5
3 9
1 2
4 6
1 5
5 3

出力例 3

8.916666666666668

Score : 300 points

Problem Statement

We have N fuses connected in series. The i-th fuse from the left has a length of A_i centimeters and burns at a constant speed of B_i centimeters per second.

Consider igniting this object from left and right ends simultaneously. Find the distance between the position where the two flames will meet and the left end of the object.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 1000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the distance between the position where the two flames will meet and the left end of the object, in centimeters (print just the number).

Your output will be considered correct when its absolute or relative error from our answer is at most 10^{-5}.


Sample Input 1

3
1 1
2 1
3 1

Sample Output 1

3.000000000000000

The two flames will meet at 3 centimeters from the left end of the object.


Sample Input 2

3
1 3
2 2
3 1

Sample Output 2

3.833333333333333

Sample Input 3

5
3 9
1 2
4 6
1 5
5 3

Sample Output 3

8.916666666666668
G - Island Tour

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

AtCoder 諸島は N 個の島からなり、これらの島々は N 本の橋によって結ばれています。 島には 1 から N までの番号が付けられていて、i\ (1\leq i\leq N-1) 本目の橋は島 i と島 i+1 を、N 本目の橋は島 N と島 1 を双方向に結んでいます。 橋を渡る以外に島の間を行き来する方法は存在しません。

AtCoder 諸島では、島 X_1 から始めて島 X_2,X_3,\dots,X_M を順に訪れるツアーが定期的に催行されています。 移動の過程で訪れる島とは別の島を経由することもあり、ツアー中に橋を通る回数の合計がツアーの長さと定義されます。

厳密には、ツアーとは以下の条件を全て満たす l+1 個の島の列 a_0,a_1,\dots,a_l のことであり、その長さ は l として定義されます。

  • 全ての j\ (0\leq j\leq l-1) について、島 a_j と島 a_{j+1} は橋で直接結ばれている
  • ある 0 = y_1 < y_2 < \dots < y_M = l が存在して、全ての k\ (1\leq k\leq M) について a_{y_k} = X_k

財政難に苦しむ AtCoder 諸島では、維持費削減のため橋を 1 本封鎖することになりました。 封鎖する橋をうまく選んだとき、ツアーの長さの最小値がいくつになるか求めてください。

制約

  • 3\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 1\leq X_k\leq N
  • X_k\neq X_{k+1}\ (1\leq k\leq M-1)
  • 入力は全て整数

入力

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

N M
X_1 X_2 \dots X_M

出力

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


入力例 1

3 3
1 3 2

出力例 1

2
  • 1 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2)=(1,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 2 のツアーが催行できます。これより短いツアーは存在しません。
  • 2 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,3,1,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。
  • 3 本目の橋を封鎖した場合:通る島の列として (a_0,a_1,a_2,a_3)=(1,2,3,2) を取ることで島 1,3,2 を順に訪れることができ、長さ 3 のツアーが催行できます。これより短いツアーは存在しません。

よって、封鎖する橋をうまく選んだときのツアーの長さの最小値は 2 です。

以下の図は左から順に橋 1,2,3 を封鎖した場合を表し、数字の書かれた丸が島、丸同士を結ぶ線が橋、青い矢印が最短のツアーの経路を表します。


入力例 2

4 5
2 4 2 4 2

出力例 2

8

X_1,X_2,\dots,X_M の中に同じ島が複数回現れることもあります。


入力例 3

163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369

出力例 3

390009

Score: 425 points

Problem Statement

The AtCoder Archipelago consists of N islands connected by N bridges. The islands are numbered from 1 to N, and the i-th bridge (1\leq i\leq N-1) connects islands i and i+1 bidirectionally, while the N-th bridge connects islands N and 1 bidirectionally. There is no way to travel between islands other than crossing the bridges.

On the islands, a tour that starts from island X_1 and visits islands X_2, X_3, \dots, X_M in order is regularly conducted. The tour may pass through islands other than those being visited, and the total number of times bridges are crossed during the tour is defined as the length of the tour.

More precisely, a tour is a sequence of l+1 islands a_0, a_1, \dots, a_l that satisfies all the following conditions, and its length is defined as l:

  • For all j\ (0\leq j\leq l-1), islands a_j and a_{j+1} are directly connected by a bridge.
  • There are some 0 = y_1 < y_2 < \dots < y_M = l such that for all k\ (1\leq k\leq M), a_{y_k} = X_k.

Due to financial difficulties, the islands will close one bridge to reduce maintenance costs. Determine the minimum possible length of the tour when the bridge to be closed is chosen optimally.

Constraints

  • 3\leq N \leq 2\times 10^5
  • 2\leq M \leq 2\times 10^5
  • 1\leq X_k\leq N
  • X_k\neq X_{k+1}\ (1\leq k\leq M-1)
  • All input values are integers.

Input

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

N M
X_1 X_2 \dots X_M

Output

Print the answer as an integer.


Sample Input 1

3 3
1 3 2

Sample Output 1

2
  • If the first bridge is closed: By taking the sequence of islands (a_0, a_1, a_2) = (1, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 2 can be conducted. There is no shorter tour.
  • If the second bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 3, 1, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.
  • If the third bridge is closed: By taking the sequence of islands (a_0, a_1, a_2, a_3) = (1, 2, 3, 2), it is possible to visit islands 1, 3, 2 in order, and a tour of length 3 can be conducted. There is no shorter tour.

Therefore, the minimum possible length of the tour when the bridge to be closed is chosen optimally is 2.

The following figure shows, from left to right, the cases when bridges 1, 2, 3 are closed, respectively. The circles with numbers represent islands, the lines connecting the circles represent bridges, and the blue arrows represent the shortest tour routes.


Sample Input 2

4 5
2 4 2 4 2

Sample Output 2

8

The same island may appear multiple times in X_1, X_2, \dots, X_M.


Sample Input 3

163054 10
62874 19143 77750 111403 29327 56303 6659 18896 64175 26369

Sample Output 3

390009
H - LRUD Moving

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

正整数 N,A,B が与えられます。A,B の値はどちらも 1 以上 N 以下であることが保証されます。

N\times N のグリッドがあります。上から i 行目、左から j 列目のマスをマス (i,j) と表記します。はじめ、コマがマス (1,1) に置かれています。

このコマを上下左右に隣接するマスに動かす移動を N^2-2 回繰り返すことで、マス (A,B) 以外の全てのマスを経由しつつマス (N,N) までコマを動かしたいです。ただし、同じマスを 2 回以上経由してはいけません(マス (1,1),(N,N) も途中で経由してはいけません)。

そのような移動が可能か判定し、可能である場合は移動列を一つ出力してください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1\le T \le 5000
  • 2\le N\le 10^3
  • 1\le A,B\le N
  • (A,B)\neq (1,1),(N,N)
  • 全てのテストケースにおける N^2 の総和は 10^6 以下
  • 入力される値は全て整数

入力

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

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

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

N A B

出力

各テストケースに対する答えを順に改行区切りで出力せよ。

各テストケースについて、条件を満たす移動が不可能である場合 No を出力せよ。

可能である場合、以下の形式で出力せよ。

Yes
S_1S_2\ldots S_{N^2-2}

ただし、S_k は k 回目の移動前のコマの座標をマス (i,j) として、k 回目の移動で

  • マス (i,j) からマス (i,j-1) に移動する場合は S_k= L
  • マス (i,j) からマス (i,j+1) に移動する場合は S_k= R
  • マス (i,j) からマス (i-1,j) に移動する場合は S_k= U
  • マス (i,j) からマス (i+1,j) に移動する場合は S_k= D

と定義する。

条件を満たす移動が複数存在する場合、どれを出力しても正答となる。


入力例 1

3
2 1 2
3 2 2
4 3 2

出力例 1

Yes
DR
No
Yes
RRRDLLLDDRRURD

1 番目のテストケースについて考えます。

はじめ、コマがマス (1,1) にある状態から以下のように 2 回移動させます。

  • コマを下に移動させる。コマはマス (2,1) に移動する。
  • コマを右に移動させる。コマはマス (2,2) に移動する。

この移動は条件を満たしています。

Score : 450 points

Problem Statement

You are given positive integers N,A,B. It is guaranteed that both A and B are between 1 and N, inclusive.

There is an N\times N grid. The cell at the i-th row from the top and j-th column from the left is denoted as cell (i,j). Initially, a piece is placed at cell (1,1).

By repeating N^2-2 moves, each of which moves the piece to an adjacent cell (up, down, left, or right), you want to move the piece to cell (N,N) while visiting every cell except cell (A,B). You must not visit the same cell more than once (you must not visit cells (1,1) and (N,N) in the middle, either).

Determine whether such a sequence of moves is possible, and if so, output one such sequence.

You are given T test cases; solve each of them.

Constraints

  • 1\le T \le 5000
  • 2\le N\le 10^3
  • 1\le A,B\le N
  • (A,B)\neq (1,1),(N,N)
  • The sum of N^2 over all test cases is at most 10^6.
  • All input values are integers.

Input

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

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

Each test case is given in the following format:

N A B

Output

Output the answers for the test cases in order, separated by newlines.

For each test case, if it is impossible to perform a sequence of moves satisfying the conditions, output No.

If it is possible, output in the following format:

Yes
S_1S_2\ldots S_{N^2-2}

Here, S_k is defined as follows, where the coordinates of the piece before the k-th move is cell (i,j).

  • S_k= L if the piece moves from cell (i,j) to cell (i,j-1)
  • S_k= R if the piece moves from cell (i,j) to cell (i,j+1)
  • S_k= U if the piece moves from cell (i,j) to cell (i-1,j)
  • S_k= D if the piece moves from cell (i,j) to cell (i+1,j)

If multiple sequences of moves satisfy the conditions, any of them will be accepted.


Sample Input 1

3
2 1 2
3 2 2
4 3 2

Sample Output 1

Yes
DR
No
Yes
RRRDLLLDDRRURD

Consider the first test case.

Starting with the piece at cell (1,1), move it twice as follows:

  • Move the piece downward. The piece moves to cell (2,1).
  • Move the piece rightward. The piece moves to cell (2,2).

This sequence of moves satisfies the conditions.

I - typewriter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

N 段からなるタイプライターがあります。このうち、上から i 段目のキーでは文字列 S_i に含まれる文字が打てます。

このキーボードを使って、以下のルールで文字列をひとつ入力することを考えます。

  • まず、整数 1 \le k \le N を選択する。
  • その後、空文字列から始めて、上から k 段目にあるキーだけを使ってちょうど L 文字の文字列を入力する。

このルールに従って入力可能な L 文字の文字列は何通りありますか? 答えは非常に大きくなる場合があるので 998244353 で割った余りを出力してください。

制約

  • N,L は整数
  • 1 \le N \le 18
  • 1 \le L \le 10^9
  • S_i は abcdefghijklmnopqrstuvwxyz の(連続とは限らない)空でない部分列

入力

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

N L
S_1
S_2
\dots
S_N

出力

答えを出力せよ。


入力例 1

2 2
ab
ac

出力例 1

7

入力可能な文字列は aa, ab, ac, ba, bb, ca, cc の 7 つです。


入力例 2

4 3
abcdefg
hijklmnop
qrstuv
wxyz

出力例 2

1352

入力例 3

5 1000000000
abc
acde
cefg
abcfh
dghi

出力例 3

346462871

答えを 998244353 で割った余りを出力してください。

Score : 500 points

Problem Statement

We have a typewriter with N rows. The keys in the i-th row from the top can type the characters in a string S_i.

Let us use this keyboard to enter a string, as follows.

  • First, choose an integer 1 \le k \le N.
  • Then, start with an empty string and only use the keys in the k-th row from the top to enter a string of length exactly L.

How many strings of length L can be entered in this way? Since the answer can be enormous, print it modulo 998244353.

Constraints

  • N and L are integers.
  • 1 \le N \le 18
  • 1 \le L \le 10^9
  • S_i is a (not necessarily contiguous) non-empty subsequence of abcdefghijklmnopqrstuvwxyz.

Input

Input is given from Standard Input in the following format:

N L
S_1
S_2
\dots
S_N

Output

Print the answer.


Sample Input 1

2 2
ab
ac

Sample Output 1

7

We can enter seven strings: aa, ab, ac, ba, bb, ca, cc.


Sample Input 2

4 3
abcdefg
hijklmnop
qrstuv
wxyz

Sample Output 2

1352

Sample Input 3

5 1000000000
abc
acde
cefg
abcfh
dghi

Sample Output 3

346462871

Be sure to print the answer modulo 998244353.