A - Rightmost

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英小文字からなる文字列 S が与えられます。
Sa が現れるならば最後に現れるのが何文字目かを出力し、現れないならば -1 を出力してください。

制約

  • S は英小文字からなる長さ 1 以上 100 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

abcdaxayz

出力例 1

7

Sa3 回現れますが、最後に現れるのは 7 文字目なので、7 を出力します。


入力例 2

bcbbbz

出力例 2

-1

Sa は現れないので、-1 を出力します。


入力例 3

aaaaa

出力例 3

5

Score : 100 points

Problem Statement

You are given a string S consisting of lowercase English letters.
If a appears in S, print the last index at which it appears; otherwise, print -1. (The index starts at 1.)

Constraints

  • S is a string of length between 1 and 100 (inclusive) consisting of lowercase English letters.

Input

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

S

Output

Print the answer.


Sample Input 1

abcdaxayz

Sample Output 1

7

a appears three times in S. The last occurrence is at index 7, so you should print 7.


Sample Input 2

bcbbbz

Sample Output 2

-1

a does not appear in S, so you should print -1.


Sample Input 3

aaaaa

Sample Output 3

5
B - Adjacency List

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1, \dots, N と番号付けられた N 個の都市と、都市間を結ぶ M 本の道路があります。
i \, (1 \leq i \leq M) 番目の道路は都市 A_i と都市 B_i を結んでいます。

以下の指示に従い、N 行にわたって出力してください。

  • 都市 i \, (1 \leq i \leq N) と道路で直接結ばれた都市が d_i 個あるとし、それらを昇順に都市 a_{i, 1}, \dots, a_{i, d_i} とおく。
  • i \, (1 \leq i \leq N) 行目には、d_i + 1 個の整数 d_i, a_{i, 1}, \dots, a_{i, d_i} を、この順番で空白区切りで出力せよ。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (i \neq j) ならば (A_i, B_i) \neq (A_j, B_j)
  • 入力される値は全て整数

入力

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

N M
A_1 B_1
\vdots
A_M B_M

出力

問題文の指示に従い、N 行にわたって出力せよ。


入力例 1

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

出力例 1

3 2 3 6
2 1 5
2 1 6
0
2 2 6
3 1 3 5

都市 1 と道路で直接結ばれているのは都市 2, 3, 6 です。よって、d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6 であるので、1 行目には 3, 2, 3, 6 をこの順番で空白区切りで出力します。

a_{i, 1}, \dots, a_{i, d_i} は昇順に並んでいなければならないことに注意してください。例えば、1 行目に 3, 3, 2, 6 をこの順番で出力した場合、不正解となります。


入力例 2

5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

出力例 2

4 2 3 4 5
4 1 3 4 5
4 1 2 4 5
4 1 2 3 5
4 1 2 3 4

Score : 200 points

Problem Statement

There are N cities numbered 1, \dots, N, and M roads connecting cities.
The i-th road (1 \leq i \leq M) connects city A_i and city B_i.

Print N lines as follows.

  • Let d_i be the number of cities directly connected to city i \, (1 \leq i \leq N), and those cities be city a_{i, 1}, \dots, city a_{i, d_i}, in ascending order.
  • The i-th line (1 \leq i \leq N) should contain d_i + 1 integers d_i, a_{i, 1}, \dots, a_{i, d_i} in this order, separated by spaces.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i \lt B_i \leq N \, (1 \leq i \leq M)
  • (A_i, B_i) \neq (A_j, B_j) if (i \neq j).
  • All values in the input are integers.

Input

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

N M
A_1 B_1
\vdots
A_M B_M

Output

Print N lines as specified in the Problem Statement.


Sample Input 1

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

Sample Output 1

3 2 3 6
2 1 5
2 1 6
0
2 2 6
3 1 3 5

The cities directly connected to city 1 are city 2, city 3, and city 6. Thus, we have d_1 = 3, a_{1, 1} = 2, a_{1, 2} = 3, a_{1, 3} = 6, so you should print 3, 2, 3, 6 in the first line in this order, separated by spaces.

Note that a_{i, 1}, \dots, a_{i, d_i} must be in ascending order. For instance, it is unacceptable to print 3, 3, 2, 6 in the first line in this order.


Sample Input 2

5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

Sample Output 2

4 2 3 4 5
4 1 3 4 5
4 1 2 4 5
4 1 2 3 5
4 1 2 3 4
C - Previous Permutation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

(1, \dots, N) の順列 P = (P_1, \dots, P_N) が与えられます。ただし、(P_1, \dots, P_N) \neq (1, \dots, N) です。

(1 \dots, N) の順列を全て辞書順で小さい順に並べたとき、PK 番目であるとします。辞書順で小さい方から K-1 番目の順列を求めてください。

順列とは?

(1, \dots, N)順列とは、(1, \dots, N) を並べ替えて得られる数列のことをいいます。

辞書順とは?

長さ N の数列 A = (A_1, \dots, A_N), B = (B_1, \dots, B_N) に対し、AB より辞書順で真に小さいとは、ある整数 1 \leq i \leq N が存在して、下記の 2 つがともに成り立つことをいいます。

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
  • A_i < B_i

制約

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • 入力される値は全て整数

入力

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

N
P_1 \ldots P_N

出力

求める順列を Q = (Q_1, \dots, Q_N) として、Q_1, \dots, Q_N をこの順に空白区切りで一行に出力せよ。


入力例 1

3
3 1 2

出力例 1

2 3 1

(1, 2, 3) の順列を辞書順で小さい順に並べると次のようになります。

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

よって P = (3, 1, 2) は小さい方から 5 番目であり、求める順列、すなわち小さい方から 5 - 1 = 4 番目の順列は (2, 3, 1) です。


入力例 2

10
9 8 6 5 10 3 1 2 4 7

出力例 2

9 8 6 5 10 2 7 4 3 1

Score : 300 points

Problem Statement

You are given a permutation P = (P_1, \dots, P_N) of (1, \dots, N), where (P_1, \dots, P_N) \neq (1, \dots, N).

Assume that P is the K-th lexicographically smallest among all permutations of (1 \dots, N). Find the (K-1)-th lexicographically smallest permutation.

What are permutations?

A permutation of (1, \dots, N) is an arrangement of (1, \dots, N) into a sequence.

What is lexicographical order?

For sequences of length N, A = (A_1, \dots, A_N) and B = (B_1, \dots, B_N), A is said to be strictly lexicographically smaller than B if and only if there is an integer 1 \leq i \leq N that satisfies both of the following.

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1}).
  • A_i < B_i.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • All values in the input are integers.

Input

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

N
P_1 \ldots P_N

Output

Let Q = (Q_1, \dots, Q_N) be the sought permutation. Print Q_1, \dots, Q_N in a single line in this order, separated by spaces.


Sample Input 1

3
3 1 2

Sample Output 1

2 3 1

Here are the permutations of (1, 2, 3) in ascending lexicographical order.

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

Therefore, P = (3, 1, 2) is the fifth smallest, so the sought permutation, which is the fourth smallest (5 - 1 = 4), is (2, 3, 1).


Sample Input 2

10
9 8 6 5 10 3 1 2 4 7

Sample Output 2

9 8 6 5 10 2 7 4 3 1
D - Divide by 2 or 3

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
あなたは以下の操作のうち 1 つを選んで行うことを 0 回以上何度でも繰り返せます。

  • 1 \leq i \leq N かつ a_i2 の倍数であるような整数 i を選び、a_i\frac{a_i}{2} に置き換える
  • 1 \leq i \leq N かつ a_i3 の倍数であるような整数 i を選び、a_i\frac{a_i}{3} に置き換える

あなたの目標は Aa_1=a_2=\ldots=a_N を満たす状態にすることです。
目標を達成するために必要な操作の回数の最小値を求めてください。ただし、どのように操作を行っても目標を達成できない場合、代わりに -1 と出力してください。

制約

  • 2 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N
a_1 a_2 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
1 4 3

出力例 1

3

次のように操作をすると 3 回で目標を達成でき、これが最小の回数です。

  • a_i2 の倍数であるような整数 i として 2 を選び、a_2\frac{a_2}{2} に置き換える。A(1,2,3) となる。
  • a_i2 の倍数であるような整数 i として 2 を選び、a_2\frac{a_2}{2} に置き換える。A(1,1,3) となる。
  • a_i3 の倍数であるような整数 i として 3 を選び、a_3\frac{a_3}{3} に置き換える。A(1,1,1) となる。

入力例 2

3
2 7 6

出力例 2

-1

どのように操作を行っても目標を達成できません。


入力例 3

6
1 1 1 1 1 1

出力例 3

0

Score : 400 points

Problem Statement

You are given a sequence of positive integers: A=(a_1,a_2,\ldots,a_N).
You can choose and perform one of the following operations any number of times, possibly zero.

  • Choose an integer i such that 1 \leq i \leq N and a_i is a multiple of 2, and replace a_i with \frac{a_i}{2}.
  • Choose an integer i such that 1 \leq i \leq N and a_i is a multiple of 3, and replace a_i with \frac{a_i}{3}.

Your objective is to make A satisfy a_1=a_2=\ldots=a_N.
Find the minimum total number of times you need to perform an operation to achieve the objective. If there is no way to achieve the objective, print -1 instead.

Constraints

  • 2 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9
  • All values in the input are integers.

Input

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

N
a_1 a_2 \ldots a_N

Output

Print the answer.


Sample Input 1

3
1 4 3

Sample Output 1

3

Here is a way to achieve the objective in three operations, which is the minimum needed.

  • Choose an integer i=2 such that a_i is a multiple of 2, and replace a_2 with \frac{a_2}{2}. A becomes (1,2,3).
  • Choose an integer i=2 such that a_i is a multiple of 2, and replace a_2 with \frac{a_2}{2}. A becomes (1,1,3).
  • Choose an integer i=3 such that a_i is a multiple of 3, and replace a_3 with \frac{a_3}{3}. A becomes (1,1,1).

Sample Input 2

3
2 7 6

Sample Output 2

-1

There is no way to achieve the objective.


Sample Input 3

6
1 1 1 1 1 1

Sample Output 3

0
E - Round Trip

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

H 行、横 W 列のマス目があり、上から i \, (1 \leq i \leq H) 行目、左から j \, (1 \leq j \leq W) 列目のマスを (i, j) と表します。

各マスは「始点」「道」「障害物」のいずれかです。
マス (i, j) の状態は文字 C_{i, j} で表され、C_{i, j} = S なら始点、C_{i, j} = . なら道、C_{i, j} = # なら障害物です。始点のマスはただ一つ存在します。

始点のマスを出発し、上下または左右に隣接するマスに移動することを繰り返して、障害物のマスを通らずに始点のマスへ戻ってくるような長さ 4 以上の経路であって、最初と最後を除き同じマスを通らないようなものが存在するか判定してください。
より厳密には、以下の条件を満たす整数 n およびマスの列 (x_0, y_0), (x_1, y_1), \dots, (x_n, y_n) が存在するか判定してください。

  • n \geq 4
  • C_{x_0, y_0} = C_{x_n, y_n} = S
  • 1 \leq i \leq n - 1 ならば C_{x_i, y_i} = .
  • 1 \leq i \lt j \leq n - 1 ならば (x_i, y_i) \neq (x_j, y_j)
  • 0 \leq i \leq n - 1 ならばマス (x_i, y_i) とマス (x_{i+1}, y_{i+1}) は上下または左右に隣接する

制約

  • 4 \leq H \times W \leq 10^6
  • H, W2 以上の整数
  • C_{i, j}S.# のいずれか
  • C_{i, j} = S となる (i, j) がただ一つ存在する

入力

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

H W
C_{1, 1} \ldots C_{1, W}
\vdots
C_{H, 1} \ldots C_{H, W}

出力

問題文の条件を満たす経路が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

4 4
....
#.#.
.S..
.##.

出力例 1

Yes

(3, 2) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (1, 4) \rightarrow (2, 4) \rightarrow (3, 4) \rightarrow (3, 3) \rightarrow (3, 2) という経路が条件を満たします。


入力例 2

2 2
S.
.#

出力例 2

No

入力例 3

5 7
.#...#.
..#.#..
...S...
..#.#..
.#...#.

出力例 3

No

Score : 500 points

Problem Statement

We have a grid with H rows from top to bottom and W columns from left to right. Let (i, j) denote the i-th row from the top (1 \leq i \leq H) and j-th column from the left (1 \leq j \leq W).

Each square is one of the following: the initial point, a road, and an obstacle.
A square (i, j) is represented by a character C_{i, j}. The square is the initial point if C_{i, j} = S, a road if C_{i, j} = ., and an obstacle if C_{i, j} = #. There is exactly one initial point.

Determine whether there is a path of length 4 or greater that starts at the initial point, repeats moving vertically or horizontally to an adjacent square, and returns to the initial point without going through an obstacle or visiting the same square multiple times except at the beginning and the end.
More formally, determine whether there are an integer n and a sequence of squares (x_0, y_0), (x_1, y_1), \dots, (x_n, y_n) that satisfy the following conditions.

  • n \geq 4.
  • C_{x_0, y_0} = C_{x_n, y_n} = S.
  • If 1 \leq i \leq n - 1, then C_{x_i, y_i} = ..
  • If 1 \leq i \lt j \leq n - 1, then (x_i, y_i) \neq (x_j, y_j).
  • If 0 \leq i \leq n - 1, then square (x_i, y_i) and square (x_{i+1}, y_{i+1}) are vertically or horizontally adjacent to each other.

Constraints

  • 4 \leq H \times W \leq 10^6
  • H and W are integers greater than or equal to 2.
  • C_{i, j} is S, ., or #.
  • There is exactly one (i, j) such that C_{i, j} = S.

Input

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

H W
C_{1, 1} \ldots C_{1, W}
\vdots
C_{H, 1} \ldots C_{H, W}

Output

If there is a path that satisfies the conditions in the Problem Statement, print Yes; otherwise, print No.


Sample Input 1

4 4
....
#.#.
.S..
.##.

Sample Output 1

Yes

The path (3, 2) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (1, 4) \rightarrow (2, 4) \rightarrow (3, 4) \rightarrow (3, 3) \rightarrow (3, 2) satisfies the conditions.


Sample Input 2

2 2
S.
.#

Sample Output 2

No

Sample Input 3

5 7
.#...#.
..#.#..
...S...
..#.#..
.#...#.

Sample Output 3

No
F - Double Chance

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

カード 1, カード 2, \ldots, カード NN 枚のカードがあり、 カード i (1\leq i\leq N) には整数 A_i が書かれています。

K=1,2,\ldots,N について、次の問題を解いてください。

カード 1, カード 2, \ldots, カード KK 枚のカードが入っている袋があります。
次の操作を 2 回繰り返し、記録された数を順に x,y とします。

袋から無作為にカードを 1 枚取り出し、カードに書かれている数を記録する。その後、カードを 袋の中に戻す

\max(x,y) の値の期待値を \text{mod} 998244353 で出力してください(注記参照)。
ただし、\max(x,y)xy のうち小さくない方の値を表します。

注記

求める期待値は必ず有限値かつ有理数となることが証明できます。また、この問題の制約下では、その値を互いに素な 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
  • 1 \leq A_i \leq 2\times 10^5
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

N 行出力せよ。 i 行目 (1\leq i\leq N) には、K=i の時の問題に対する答えを出力せよ。


入力例 1

3
5 7 5

出力例 1

5
499122183
443664163

例えば、K=2 の時の答えは次のようにして求まります。

袋の中にはカード 1 とカード 2 が入っており、それぞれには A_1=5A_2=7 が書かれています。

  • 1 回目に取り出されたカードがカード 12 回目に取り出されたカードもカード 1 のとき、x=y=5 であり、\max(x,y)=5 となります。
  • 1 回目に取り出されたカードがカード 12 回目に取り出されたカードはカード 2 のとき、x=5, y=7 であり、\max(x,y)=7 となります。
  • 1 回目に取り出されたカードがカード 22 回目に取り出されたカードはカード 1 のとき、x=7, y=5 であり、\max(x,y)=7 となります。
  • 1 回目に取り出されたカードがカード 22 回目に取り出されたカードもカード 2 のとき、x=y=7 であり、\max(x,y)=7 となります。

これらが等確率で起こるため、期待値は \frac{5+7+7+7}{4}=\frac{13}{2} となります。 499122183\times 2\equiv 13 \pmod{998244353} であるため、499122183 を出力します。


入力例 2

7
22 75 26 45 72 81 47

出力例 2

22
249561150
110916092
873463862
279508479
360477194
529680742

Score : 500 points

Problem Statement

There are N cards called card 1, card 2, \ldots, card N. On card i (1\leq i\leq N), an integer A_i is written.

For K=1, 2, \ldots, N, solve the following problem.

We have a bag that contains K cards: card 1, card 2, \ldots, card K.
Let us perform the following operation twice, and let x and y be the numbers recorded, in the recorded order.

Draw a card from the bag uniformly at random, and record the number written on that card. Then, return the card to the bag.

Print the expected value of \max(x,y), modulo 998244353 (see Notes).
Here, \max(x,y) denotes the value of the greater of x and y (or x if they are equal).

Notes

It can be proved that the sought expected value is always finite and rational. Additionally, under the Constraints of this problem, when that value is represented as \frac{P}{Q} with two coprime integers P and Q, it can be proved that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Print this R.

Constraints

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

Input

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

N
A_1 A_2 \ldots A_N

Output

Print N lines. The i-th line (1\leq i\leq N) should contain the answer to the problem for K=i.


Sample Input 1

3
5 7 5

Sample Output 1

5
499122183
443664163

For instance, the answer for K=2 is found as follows.

The bag contains card 1 and card 2, with A_1=5 and A_2=7 written on them, respectively.

  • If you draw card 1 in the first draw and card 1 again in the second draw, we have x=y=5, so \max(x,y)=5.
  • If you draw card 1 in the first draw and card 2 in the second draw, we have x=5 and y=7, so \max(x,y)=7.
  • If you draw card 2 in the first draw and card 1 in the second draw, we have x=7 and y=5, so \max(x,y)=7.
  • If you draw card 2 in the first draw and card 2 again in the second draw, we have x=y=7, so \max(x,y)=7.

These events happen with the same probability, so the sought expected value is \frac{5+7+7+7}{4}=\frac{13}{2}. We have 499122183\times 2\equiv 13 \pmod{998244353}, so 499122183 should be printed.


Sample Input 2

7
22 75 26 45 72 81 47

Sample Output 2

22
249561150
110916092
873463862
279508479
360477194
529680742
G - Count Sequences

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

以下の条件を満たす項数 N の整数列 A=(a_1,a_2,\ldots,a_N) の個数を 998244353 で割った余りを求めてください。

  • 0 \leq a_1 \leq a_2 \leq \ldots \leq a_N \leq M
  • i=1,2,\ldots,N-1 それぞれについて、「a_i3 で割った余り」と「a_{i+1}3 で割った余り」が異なる

制約

  • 2 \leq N \leq 10^7
  • 1 \leq M \leq 10^7
  • 入力はすべて整数

入力

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

N M

出力

答えを出力せよ。


入力例 1

3 4

出力例 1

8

以下の 8 個が条件を満たします。

  • (0,1,2)
  • (0,1,3)
  • (0,2,3)
  • (0,2,4)
  • (1,2,3)
  • (1,2,4)
  • (1,3,4)
  • (2,3,4)

入力例 2

276 10000000

出力例 2

909213205

個数を 998244353 で割った余りを求めてください。

Score : 600 points

Problem Statement

Find the number of sequences of integers with N terms, A=(a_1,a_2,\ldots,a_N), that satisfy the following conditions, modulo 998244353.

  • 0 \leq a_1 \leq a_2 \leq \ldots \leq a_N \leq M.
  • For each i=1,2,\ldots,N-1, the remainder when a_i is divided by 3 is different from the remainder when a_{i+1} is divided by 3.

Constraints

  • 2 \leq N \leq 10^7
  • 1 \leq M \leq 10^7
  • All values in the input are integers.

Input

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

N M

Output

Print the answer.


Sample Input 1

3 4

Sample Output 1

8

Here are the eight sequences that satisfy the conditions.

  • (0,1,2)
  • (0,1,3)
  • (0,2,3)
  • (0,2,4)
  • (1,2,3)
  • (1,2,4)
  • (1,3,4)
  • (2,3,4)

Sample Input 2

276 10000000

Sample Output 2

909213205

Be sure to find the count modulo 998244353.

Ex - Construct a Matrix

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

以下の条件を満たす NN 列の行列 X が存在するかどうかを判定し、存在する場合は 1 つ示してください。( X の上から i 行目、左から j 列目の要素を x_{i,j} とします)

  • すべての i,j(1 \leq i,j \leq N) に対し、x_{i,j} \in \{ 0,1,2 \}
  • i=1,2,\ldots,Q それぞれに対し次の条件が成立する。
    • P = \prod_{a_i \leq j \leq b_i} \prod_{c_i \leq k \leq d_i} x_{j,k} とする。この時、P3 で割った余りは e_i に等しい。

制約

  • 1 \leq N,Q \leq 2000
  • 1 \leq a_i \leq b_i \leq N
  • 1 \leq c_i \leq d_i \leq N
  • e_i \in \{0,1,2 \}
  • 入力はすべて整数

入力

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

N Q
a_1 b_1 c_1 d_1 e_1
\vdots
a_Q b_Q c_Q d_Q e_Q

出力

条件を満たす X が存在しないならば No と出力せよ。

条件を満たす X が存在するならば 1 行目に Yes と出力し、2 行目以降に以下の形式で X の一例を出力せよ。

x_{1,1} x_{1,2} \ldots x_{1,N}
x_{2,1} x_{2,2} \ldots x_{2,N}
\vdots
x_{N,1} x_{N,2} \ldots x_{N,N}

条件を満たす X が複数存在する場合、どれを出力しても良い。


入力例 1

2 3
1 1 1 2 0
1 2 2 2 1
2 2 1 2 2

出力例 1

Yes
0 2
1 2

例えば i=2 に対し、P = \prod_{a_2 \leq j \leq b_2} \prod_{c_2 \leq k \leq d_2} x_{j,k}= \prod_{1 \leq j \leq 2} \prod_{2 \leq k \leq 2} x_{j,k}=x_{1,2} \times x_{2,2} です。
この出力例において x_{1,2}=2, x_{2,2}=2 なので P=2 \times 2 = 4 であり、これを 3 で割った余りは e_2=1 に等しいです。
i=1,3 に対しても同様に条件を満たすことを確認できます。


入力例 2

4 4
1 4 1 4 0
1 4 1 4 1
1 4 1 4 2
1 4 1 4 0

出力例 2

No

Score : 600 points

Problem Statement

Determine whether there is an N-by-N matrix X that satisfies the following conditions, and present one such matrix if it exists. (Let x_{i,j} denote the element of X at the i-th row from the top and j-th column from the left.)

  • x_{i,j} \in \{ 0,1,2 \} for every i and j (1 \leq i,j \leq N).
  • The following holds for each i=1,2,\ldots,Q.
    • Let P = \prod_{a_i \leq j \leq b_i} \prod_{c_i \leq k \leq d_i} x_{j,k}. Then, P is congruent to e_i modulo 3.

Constraints

  • 1 \leq N,Q \leq 2000
  • 1 \leq a_i \leq b_i \leq N
  • 1 \leq c_i \leq d_i \leq N
  • e_i \in \{0,1,2 \}
  • All values in the input are integers.

Input

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

N Q
a_1 b_1 c_1 d_1 e_1
\vdots
a_Q b_Q c_Q d_Q e_Q

Output

If no matrix X satisfies the conditions, print No.

If there is a matrix X that satisfies them, print Yes in the first line and one such instance of X in the following format in the second and subsequent lines.

x_{1,1} x_{1,2} \ldots x_{1,N}
x_{2,1} x_{2,2} \ldots x_{2,N}
\vdots
x_{N,1} x_{N,2} \ldots x_{N,N}

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


Sample Input 1

2 3
1 1 1 2 0
1 2 2 2 1
2 2 1 2 2

Sample Output 1

Yes
0 2
1 2

For example, for i=2, we have P = \prod_{a_2 \leq j \leq b_2} \prod_{c_2 \leq k \leq d_2} x_{j,k}= \prod_{1 \leq j \leq 2} \prod_{2 \leq k \leq 2} x_{j,k}=x_{1,2} \times x_{2,2}.
In this sample output, we have x_{1,2}=2 and x_{2,2}=2, so P=2 \times 2 = 4, which is congruent to e_2=1 modulo 3.
It can be similarly verified that the condition is also satisfied for i=1 and i=3.


Sample Input 2

4 4
1 4 1 4 0
1 4 1 4 1
1 4 1 4 2
1 4 1 4 0

Sample Output 2

No