A - On and Off

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

配点 : 100

問題文

高橋君は、毎日 S0 分に部屋の電気をつけ、毎日 T0 分に消します。
電気をつけている間に日付が変わることもあります。

X30 分に部屋の電気がついているかどうか判定してください。

制約

  • 0 \leq S, T, X \leq 23
  • S \neq T
  • 入力は全て整数である。

入力

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

S T X

出力

X30 分に部屋の電気がついているならば Yes と、そうでなければ No と出力せよ。


入力例 1

7 20 12

出力例 1

Yes

部屋の電気がついているのは 70 分から 200 分までの間です。1230 分には電気がついているので、Yes と出力します。


入力例 2

20 7 12

出力例 2

No

部屋の電気がついているのは 00 分から 70 分までの間と、200 分から(次の日の)00 分までの間です。 1230 分には電気がついていないので、No と出力します。


入力例 3

23 0 23

出力例 3

Yes

Score : 100 points

Problem Statement

Takahashi turns on the light of his room at S o'clock (on the 24-hour clock) every day and turns it off at T o'clock every day.
The date may change while the light is on.

Determine whether the light is on at 30 minutes past X o'clock.

Constraints

  • 0 \leq S, T, X \leq 23
  • S \neq T
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

S T X

Output

If the light is on at 30 minutes past X o'clock, print Yes; otherwise, print No.


Sample Input 1

7 20 12

Sample Output 1

Yes

The light is on between 7 o'clock and 20 o'clock. At 30 minutes past 12 o'clock, it is on, so we print Yes.


Sample Input 2

20 7 12

Sample Output 2

No

The light is on between 0 o'clock and 7 o'clock, and between 20 o'clock and 0 o'clock (on the next day). At 30 minutes past 12 o'clock, it is off, so we print No.


Sample Input 3

23 0 23

Sample Output 3

Yes
B - Takahashi's Secret

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

配点 : 200

問題文

高橋君には N 人の友達がいます。N 人の友達はそれぞれ、友達 1 、友達 2\ldots 、友達 N というあだ名で呼ばれています。

ある日、高橋君はある恥ずかしい秘密を、友達の一人である友達 X に知られてしまいました。
i = 1, 2, \ldots, N について、友達 i が高橋君の秘密を知ったとき、友達 A_i がまだ高橋君の秘密を知らなければ、友達 i は高橋君の秘密を友達 A_i にも教えてしまいます。

高橋君の秘密は最終的に何人の友達に知られることになるでしょうか?

制約

  • 2 \leq N \leq 10^5
  • 1 \leq X \leq N
  • 1 \leq A_i \leq N
  • A_i \neq i
  • 入力はすべて整数

入力

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

N X
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

4 2
3 1 1 2

出力例 1

3

高橋君の秘密は以下の流れで友達 1 、友達 2 、友達 33 人に知れ渡ります。

  • ある日、高橋君は秘密を友達 2 に知られてしまいました。
  • 秘密を知った友達 2 は、その秘密を友達 1 に教えます。
  • 秘密を知った友達 1 は、その秘密を友達 3 に教えます。

高橋君の秘密は最終的に 3 人の友達に知られることになるため、3 を出力します。


入力例 2

20 12
7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10

出力例 2

7

Score : 200 points

Problem Statement

Takahashi has N friends. They have nicknames: Friend 1, Friend 2, \ldots, Friend N.

One day, Takahashi accidentally let one of his friends, Friend X, learn his shameful secret.
For each i = 1, 2, \ldots, N, when Friend i learns the secret, he/she will share it with Friend A_i, if Friend A_i has not already learned it.

How many of Takahashi's friends will learn the secret in the end?

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq X \leq N
  • 1 \leq A_i \leq N
  • A_i \neq i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

4 2
3 1 1 2

Sample Output 1

3

Takahashi's secret will be learned by Friend 1, Friend 2, and Friend 3, as follows.

  • One day, Takahashi let Friend 2 learn the secret.
  • Friend 2 shares it with Friend 1.
  • Friend 1 shares it with Friend 3.

In the end, three of his friends learn the secret, so we print 3.


Sample Input 2

20 12
7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10

Sample Output 2

7
C - Final Day

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

配点 : 300

問題文

N 人の生徒が 4 日間にわたる試験を受けています。

それぞれの日に行われる試験は 300 点満点です。すなわち、4 日間を通した試験の満点は 1200 点です。

現在 3 日目までの試験が終わり、これから 4 日目の試験が行われようとしています。i \, (1 \leq i \leq N) 番目の生徒は j \, (1 \leq j \leq 3) 日目の試験で P_{i, j} 点獲得しました。

それぞれの生徒について、4 日目の試験後に上位 K 位以内に入っていることがあり得るかどうか判定してください。
ただし、4 日目の試験後の生徒の順位は、その生徒よりも 4 日間の合計点が高い生徒の人数に 1 を加えた値として定めます。

制約

  • 1 \leq K \leq N \leq 10^5
  • 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
  • 入力は全て整数である。

入力

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

N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}

出力

N 行出力せよ。i \, (1 \leq i \leq N) 行目には、i 番目の生徒が 4 日目の試験後に上位 K 位以内に入っていることがあり得るならば Yes と、そうでないならば No と出力せよ。


入力例 1

3 1
178 205 132
112 220 96
36 64 20

出力例 1

Yes
Yes
No

4 日目に全員が 100 点を取ると、1 番目の生徒が 1 位になります。 4 日目に 2 番目の生徒が 100 点を取り、それ以外の生徒が 0 点を取ると、2 番目の生徒が 1 位になります。 3 番目の生徒が 1 位になることはあり得ません。


入力例 2

2 1
300 300 300
200 200 200

出力例 2

Yes
Yes

入力例 3

4 2
127 235 78
192 134 298
28 56 42
96 120 250

出力例 3

Yes
Yes
No
Yes

Score : 300 points

Problem Statement

N students are taking a 4-day exam.

There is a 300-point test on each day, for a total of 1200 points.

The first three days of the exam are already over, and the fourth day is now about to begin. The i-th student (1 \leq i \leq N) got P_{i, j} points on the j-th day (1 \leq j \leq 3).

For each student, determine whether it is possible that he/she is ranked in the top K after the fourth day.
Here, the rank of a student after the fourth day is defined as the number of students whose total scores over the four days are higher than that of the student, plus 1.

Constraints

  • 1 \leq K \leq N \leq 10^5
  • 0 \leq P_{i, j} \leq 300 \, (1 \leq i \leq N, 1 \leq j \leq 3)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_{1,1} P_{1,2} P_{1,3}
\vdots
P_{N,1} P_{N,2} P_{N,3}

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain Yes if it is possible that the i-th student is ranked in the top K after the fourth day, and No otherwise.


Sample Input 1

3 1
178 205 132
112 220 96
36 64 20

Sample Output 1

Yes
Yes
No

If every student scores 100 on the fourth day, the 1-st student will rank 1-st.
If the 2-nd student scores 100 and the other students score 0 on the fourth day, the 2-nd student will rank 1-st.
The 3-rd student will never rank 1-st.


Sample Input 2

2 1
300 300 300
200 200 200

Sample Output 2

Yes
Yes

Sample Input 3

4 2
127 235 78
192 134 298
28 56 42
96 120 250

Sample Output 3

Yes
Yes
No
Yes
D - Linear Probing

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 400

問題文

N = 2^{20} 項からなる数列 A = (A_0, A_1, \dots, A_{N - 1}) があります。はじめ、全ての要素は -1 です。

Q 個のクエリを順番に処理してください。i \, (1 \leq i \leq Q) 個目のクエリは t_i = 1 または t_i = 2 を満たす整数 t_i および整数 x_i で表され、内容は以下の通りです。

  • t_i = 1 のとき、以下の処理を順番に行う。
    1. 整数 hh = x_i で定める。
    2. A_{h \bmod N} \neq -1 である間、h の値を 1 増やすことを繰り返す。この問題の制約下でこの操作が有限回で終了することは証明できる。
    3. A_{h \bmod N} の値を x_i で書き換える。
  • t_i = 2 のとき、その時点での A_{x_i \bmod N} の値を出力する。

なお、整数 a, b に対し、ab で割った余りを a \bmod b と表します。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
  • 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
  • t_i = 2 であるような i \, (1 \leq i \leq Q)1 つ以上存在する。
  • 入力は全て整数である。

入力

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

Q
t_1 x_1
\vdots
t_{Q} x_{Q}

出力

t_i = 2 であるようなクエリに対し、それぞれ答えを 1 行に出力せよ。そのようなクエリが 1 つ以上存在することは保証される。


入力例 1

4
1 1048577
1 1
2 2097153
2 3

出力例 1

1048577
-1

x_1 \bmod N = 1 であるので、1 番目のクエリによって A_1 = 1048577 となります。

2 番目のクエリにおいて、はじめ h = x_2 ですが、A_{h \bmod N} = A_{1} \neq -1 であるので h の値を 1 増やします。すると A_{h \bmod N} = A_{2} = -1 となるので、このクエリによって A_2 = 1 となります。

3 番目のクエリにおいて、A_{x_3 \bmod N} = A_{1} = 1048577 を出力します。

4 番目のクエリにおいて、A_{x_4 \bmod N} = A_{3} = -1 を出力します。

この問題において N = 2^{20} = 1048576 は定数であり、入力では与えられないことに注意してください。

Score : 400 points

Problem Statement

There is a sequence A = (A_0, A_1, \dots, A_{N - 1}) with N = 2^{20} terms. Initially, every term is -1.

Process Q queries in order. The i-th query (1 \leq i \leq Q) is described by an integer t_i such that t_i = 1 or t_i = 2, and another integer x_i, as follows.

  • If t_i = 1, do the following in order.
    1. Define an integer h as h = x_i.
    2. While A_{h \bmod N} \neq -1, keep adding 1 to h. We can prove that this process ends after finite iterations under the Constraints of this problem.
    3. Replace the value of A_{h \bmod N} with x_i.
  • If t_i = 2, print the value of A_{x_i \bmod N} at that time.

Here, for integers a and b, a \bmod b denotes the remainder when a is divided by b.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • t_i \in \{ 1, 2 \} \, (1 \leq i \leq Q)
  • 0 \leq x_i \leq 10^{18} \, (1 \leq i \leq Q)
  • There is at least one i (1 \leq i \leq Q) such that t_i = 2.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
t_1 x_1
\vdots
t_{Q} x_{Q}

Output

For each query with t_i = 2, print the response in one line. It is guaranteed that there is at least one such query.


Sample Input 1

4
1 1048577
1 1
2 2097153
2 3

Sample Output 1

1048577
-1

We have x_1 \bmod N = 1, so the first query sets A_1 = 1048577.

In the second query, initially we have h = x_2, for which A_{h \bmod N} = A_{1} \neq -1, so we add 1 to h. Now we have A_{h \bmod N} = A_{2} = -1, so this query sets A_2 = 1.

In the third query, we print A_{x_3 \bmod N} = A_{1} = 1048577.

In the fourth query, we print A_{x_4 \bmod N} = A_{3} = -1.

Note that, in this problem, N = 2^{20} = 1048576 is a constant and not given in input.

E - Integer Sequence Fair

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

配点 : 500

問題文

整数列を一堂に集めてその優劣を定める、整数列品評会が行われます。 品評会では、1 以上 K 以下の整数からなる長さ N の整数列すべてが審査対象となり、 審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつけます。

「審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつける方法」が何通りあるかを 998244353 で割ったあまりを出力してください。

ただし、2 つの方法が異なるとは「審査対象となるある数列 A = (A_1, A_2, \ldots, A_N) が存在して、 A に対してつける点数が 2 つの方法で異なる」ことを言います。

制約

  • 1 \leq N, K, M \leq 10^{18}
  • N, K, M は整数

入力

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

N K M

出力

「審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつける方法」が何通りあるかを 998244353 で割ったあまりを出力せよ。


入力例 1

2 2 2

出力例 1

16

審査対象となる数列は、(1, 1), (1, 2), (2, 1), (2, 2)4 つです。「審査対象の数列それぞれに対して 1 以上 2 以下の整数の点数をつける方法」は、以下の 16 通りあります。

  • (1, 1)1 点、(1, 2)1 点、(2, 1)1 点、(2, 2)1 点をつける方法
  • (1, 1)1 点、(1, 2)1 点、(2, 1)1 点、(2, 2)2 点をつける方法
  • (1, 1)1 点、(1, 2)1 点、(2, 1)2 点、(2, 2)1 点をつける方法
  • (1, 1)1 点、(1, 2)1 点、(2, 1)2 点、(2, 2)2 点をつける方法
  • \cdots
  • (1, 1)2 点、(1, 2)2 点、(2, 1)2 点、(2, 2)2 点をつける方法

よって、16 を出力します。


入力例 2

3 14 15926535

出力例 2

109718301

998244353 で割ったあまりを出力することに注意してください。

Score : 500 points

Problem Statement

Integer Sequence Exhibition is taking place, where integer sequences are gathered in one place and evaluated. Here, every integer sequence of length N consisting of integers between 1 and K (inclusive) is evaluated and given an integer score between 1 and M (inclusive).

Print the number, modulo 998244353, of ways to give each of the evaluated sequences a score between 1 and M.

Here, two ways are said to be different when there is an evaluated sequence A = (A_1, A_2, \ldots, A_N) that is given different scores by the two ways.

Constraints

  • 1 \leq N, K, M \leq 10^{18}
  • N, K, and M are integers.

Input

Input is given from Standard Input in the following format:

N K M

Output

Print the number, modulo 998244353, of ways to give each of the evaluated sequences a score between 1 and M.


Sample Input 1

2 2 2

Sample Output 1

16

Four sequences are evaluated: (1, 1), (1, 2), (2, 1), and (2, 2). There are 16 ways to give each of these sequences a score between 1 and 2, as follows.

  • Give 1 to (1, 1), 1 to (1, 2), 1 to (2, 1), and 1 to (2, 2)
  • Give 1 to (1, 1), 1 to (1, 2), 1 to (2, 1), and 2 to (2, 2)
  • Give 1 to (1, 1), 1 to (1, 2), 2 to (2, 1), and 1 to (2, 2)
  • Give 1 to (1, 1), 1 to (1, 2), 2 to (2, 1), and 2 to (2, 2)
  • \cdots
  • Give 2 to (1, 1), 2 to (1, 2), 2 to (2, 1), and 2 to (2, 2)

Thus, we print 16.


Sample Input 2

3 14 15926535

Sample Output 2

109718301

Be sure to print the count modulo 998244353.

F - Stamp Game

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

配点 : 500

問題文

H 行、横 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。
1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) それぞれについて、マス (i, j) には正整数 A_{i, j} が書かれています。また、すべてのマスは白色に塗られています。

高橋君は、縦 h_1 行、横 w_1 列の長方形の黒いスタンプを持っており、青木君は、縦 h_2 行、横 w_2 列の長方形の白いスタンプを持っています。
2 人はこれらのスタンプとマス目を使って対戦ゲームをします。

まず高橋君が、持っている黒いスタンプを 1 回使って、マス目の縦 h_1 行、横 w_1 列の長方形領域を黒色に塗りつぶします。
すなわち、1 \leq i \leq H - h_1 + 1 かつ 1 \leq j \leq W - w_1 + 1 を満たす整数の組 (i, j) を一つ選び、 i \leq r \leq i + h_1 - 1 かつ j \leq c \leq j + w_1 - 1 を満たすすべてのマス (r, c) を黒色に塗りつぶします。

次に青木君が、持っている白いスタンプを 1 回使って、マス目の縦 h_2 行、横 w_2 列の長方形領域を白色に塗りつぶします。
すなわち、1 \leq i \leq H - h_2 + 1 かつ 1 \leq j \leq W - w_2 + 1 を満たす整数の組 (i, j) を一つ選び、 i \leq r \leq i + h_2 - 1 かつ j \leq c \leq j + w_2 - 1 を満たすすべてのマス (r, c) を白色に塗りつぶします。
このとき、青木君が白色に塗るマスがすでに高橋君によって黒色に塗られていた場合は、そのマスの色は白で上書きされます。

上記の通りに高橋君と青木君がスタンプを 1 回ずつ使った後の、黒色に塗られたマスに書かれた整数の合計を、ゲームの「スコア」とします。 高橋君はスコアが出来るだけ大きくなるように、青木君はスコアが出来るだけ小さくなるように、それぞれ最適な戦略をとります。 ゲームのスコアがいくらになるかを求めて下さい。

制約

  • 2 \leq H, W \leq 1000
  • 1 \leq h_1, h_2 \leq H
  • 1 \leq w_1, w_2 \leq W
  • 1 \leq A_{i, j} \leq 10^9
  • 入力はすべて整数

入力

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

H W h_1 w_1 h_2 w_2
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}

出力

高橋君はスコアが出来るだけ大きくなるように、青木君はスコアが出来るだけ小さくなるように、それぞれ最適な戦略をとるときの、ゲームのスコアを出力せよ。


入力例 1

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

出力例 1

19

ゲームは以下の通りに進行します。

  • はじめ、すべてのマスは白色で塗られています。
  • まず高橋君が、持っている縦 2 行横 3 列の黒いスタンプを使って、マス (2, 2), (2, 3), (2 ,4), (3, 2), (3, 3), (3, 4)6 つのマスを黒色で塗りつぶします。
  • 次に青木君が、持っている縦 3 行横 1 列の白いスタンプを使って、マス (1, 4), (2, 4), (3, 4) を白色で塗りつぶします。
  • 最終的に黒色で塗られているマスは、マス (2, 2), (2, 3), (3, 2), (3, 3)4 つであるため、ゲームのスコアは 9 + 2 + 3 + 5 = 19 です。

入力例 2

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

出力例 2

0

青木君がスタンプを使った後、すべてのマスは白色であり、ゲームのスコアは 0 となります。


入力例 3

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

出力例 3

180

Score : 500 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
For each pair of integers (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, (i, j) contains a positive integer A_{i,j}. Additionally, every square is painted white.

Takahashi has a rectangular black stamp that can cover h_1 rows and w_1 columns, and Aoki has a rectangular white stamp that can cover h_2 rows and w_2 columns. Using these stamps and the grid, they will play a game against each other.

First, Takahashi presses his black stamp to paint a rectangular region with h_1 rows and w_1 columns black.
That is, he chooses a pair of integers (i, j) such that 1 \leq i \leq H - h_1 + 1 and 1 \leq j \leq W - w_1 + 1, and paints black every square (r, c) such that i \leq r \leq i + h_1 - 1 and j \leq c \leq j + w_1 - 1.

Second, Aoki presses his white stamp to paint a rectangular region with h_2 rows and w_2 columns white.
That is, he chooses a pair of integers (i, j) such that 1 \leq i \leq H - h_2 + 1 and 1 \leq j \leq W - w_2 + 1, and paints white every square (r, c) such that i \leq r \leq i + h_2 - 1 and j \leq c \leq j + w_2 - 1.
If some of these squares are already painted black by Takahashi, they will also become white.

The game's score is defined as the sum of the integers written on the squares painted black after Takahashi and Aoki press their stamps as described above. Takahashi follows the optimal strategy to make the score as large as possible, while Aoki follows the optimal strategy to make the score as small as possible. What will the game's score be?

Constraints

  • 2 \leq H, W \leq 1000
  • 1 \leq h_1, h_2 \leq H
  • 1 \leq w_1, w_2 \leq W
  • 1 \leq A_{i, j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W h_1 w_1 h_2 w_2
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}

Output

Print the game's score when Takahashi follows the optimal strategy to make the score as large as possible and Aoki follows the optimal strategy to make the score as small as possible.


Sample Input 1

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

Sample Output 1

19

The game will go as follows.

  • Initially, all squares are painted white.
  • First, Takahashi presses his 2 \times 3 black stamp to paint the following six squares black: (2, 2), (2, 3), (2 ,4), (3, 2), (3, 3), (3, 4).
  • Second, Aoki presses his 3 \times 1 white stamp to paint the following three squares white: (1, 4), (2, 4), (3, 4).
  • Eventually, the following four squares are painted black: (2, 2), (2, 3), (3, 2), (3, 3), for a score of 9 + 2 + 3 + 5 = 19.

Sample Input 2

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

Sample Output 2

0

After Aoki presses his stamp, all squares will be white, for a score of 0.


Sample Input 3

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

Sample Output 3

180
G - Digits on Grid

実行時間制限: 3 sec / メモリ制限: 1024 MB

配点 : 600

問題文

H 行、横 W 列のマス目があり、それぞれのマスには 1 から 9 のいずれかの数字が書かれています。 1 \leq i \leq H かつ 1 \leq j \leq W を満たす整数の組 (i, j) それぞれについて、上から i 行目、 左から j 列目のマスに書かれた数字は c_{i, j} です。

高橋君と青木君はこのマス目を使って 2 人で遊びます。 まず、高橋君がいずれか 1 つのマスを選び、そのマスにコマを置きます。その後、2 人は下記の手順 1. から 4. を N 回繰り返します。

  1. 高橋君が次の 2 つのうちどちらか一方を行う。
    • 現在コマが置かれているマスと同じ行にある別のマスに、コマを移動する。
    • 何もしない。
  2. 高橋君が、現在コマが置かれているマスに書かれた数字を黒板に書く。
  3. 青木君が次の 2 つのうちどちらか一方を行う。
    • 現在コマが置かれているマスと同じ列にある別のマスに、コマを移動する。
    • 何もしない。
  4. 青木君が、現在コマが置かれているマスに書かれた数字を黒板に書く。

その後、黒板には 2N 個の数字が書かれています。それらの数字を黒板に書かれたのが早い順番に並べたものを d_1, d_2, \ldots, d_{2N} とします。 2 人は 2N 個の数字をこの順番で繋げて 2N 桁の整数 X := d_1d_2\ldots d_{2N} を作ります。

整数 X としてあり得るものが何通りあるかを、998244353 で割った余りを出力してください。

制約

  • 2 \leq H, W \leq 10
  • 1 \leq N \leq 300
  • 1 \leq c_{i, j} \leq 9
  • 入力はすべて整数

入力

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

H W N
c_{1, 1}c_{1, 2}\cdotsc_{1, W}
c_{2, 1}c_{2, 2}\cdotsc_{2, W}
\vdots
c_{H, 1}c_{H, 2}\cdotsc_{H, W}

出力

整数 X としてあり得るものが何通りあるかを、998244353 で割った余りを出力せよ。


入力例 1

2 2 1
31
41

出力例 1

5

例えば、以下の進行が考えられます。

  • まず高橋君がマス (1, 2) にコマを置く。
  • 高橋君がコマをマス (1, 2) からマス (1, 1) に動かす。その後、マス (1, 1) に書かれた数字 3 を黒板に書く。
  • 青木君がコマをマス (1, 1) からマス (2, 1) に動かす。その後、マス (2, 1) に書かれた数字 4 を黒板に書く。

この場合、X = 34 となります。
他の例として、以下の進行も考えられます。

  • まず高橋君がマス (2, 2) にコマを置く。
  • 高橋君がコマをマス (2, 2) から動かさず、マス (2, 2) に書かれた数字 1 を黒板に書く。
  • 青木君がコマをマス (2, 2) からマス (1, 2) に動かす。その後、マス (1, 2) に書かれた数字 1 を黒板に書く。

この場合、X = 11 となります。
X としてあり得る整数は、上記の例で示した 34, 11 の他にも 33, 44, 43 があります。また、それら以外の整数が X となることはありえません。
X としてあり得る整数の個数は 5 個であるので 5 を出力します。


入力例 2

2 3 4
777
777

出力例 2

1

整数 X としてあり得るのは、77777777 のみです。


入力例 3

10 10 300
3181534389
4347471911
4997373645
5984584273
1917179465
3644463294
1234548423
6826453721
5892467783
1211598363

出力例 3

685516949

998244353 で割った余りを出力することに注意して下さい。

Score : 600 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, where each square contains a digit between 1 and 9. For each pair of integers (i, j) such that 1 \leq i \leq H and 1 \leq j \leq W, the digit written on the square at the i-th row from the top and j-th column from the left is c_{i, j}.

Using this grid, Takahashi and Aoki will play together. First, Takahashi chooses a square and puts a piece on it. Then, the two will repeat the following procedures, 1. through 4., N times.

  1. Takahashi does one of the following two actions.
    • Move the piece to another square that shares a row with the square the piece is on.
    • Do nothing.
  2. Takahashi writes on a blackboard the digit written on the square the piece is on.
  3. Aoki does one of the following two actions.
    • Move the piece to another square that shares a column with the square the piece is on.
    • Do nothing.
  4. Aoki writes on the blackboard the digit written on the square the piece is on.

After that, there will be 2N digits written on the blackboard. Let d_1, d_2, \ldots, d_{2N} be those digits, in the order they are written. The two boys will concatenate the 2N digits in this order to make a 2N-digit integer X := d_1d_2\ldots d_{2N}.

Find the number, modulo 998244353, of different integers that X can become.

Constraints

  • 2 \leq H, W \leq 10
  • 1 \leq N \leq 300
  • 1 \leq c_{i, j} \leq 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
c_{1, 1}c_{1, 2}\cdotsc_{1, W}
c_{2, 1}c_{2, 2}\cdotsc_{2, W}
\vdots
c_{H, 1}c_{H, 2}\cdotsc_{H, W}

Output

Print the number, modulo 998244353, of different integers that X can become.


Sample Input 1

2 2 1
31
41

Sample Output 1

5

Below is one possible scenario.

  • First, Takahashi puts the piece on (1, 2).
  • Takahashi moves the piece from (1, 2) to (1, 1), and then writes the digit 3 written on (1, 1).
  • Aoki moves the piece from (1, 1) to (2, 1), and then writes the digit 4 written on (2, 1).

In this case, we have X = 34.
Below is another possible scenario.

  • First, Takahashi puts the piece on (2, 2).
  • Takahashi keeps the piece on (2, 2), and then writes the digit 1 written on (2, 2).
  • Aoki moves the piece from (2, 2) to (1, 2), and then writes the digit 1 written on (1, 2).

In this case, we have X = 11. Other than these, X can also become 33, 44, or 43, but nothing else.
That is, there are five integers that X can become, so we print 5.


Sample Input 2

2 3 4
777
777

Sample Output 2

1

X can only become 77777777.


Sample Input 3

10 10 300
3181534389
4347471911
4997373645
5984584273
1917179465
3644463294
1234548423
6826453721
5892467783
1211598363

Sample Output 3

685516949

Be sure to find the count modulo 998244353.

H - Histogram

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

配点 : 600

問題文

長さ N の整数列 A = (A_1, \dots, A_N) および C = (C_1, \dots, C_N) が与えられます。

あなたは以下の操作を好きな回数(0 回でもよい)行うことができます。

  • 1 \leq i \leq N を満たす整数 i を選び、A_i の値を 1 増やす。このとき、C_i 円の費用を支払う。

好きな回数の操作を行ったあと、A の要素の種類数を K として、K \times X 円を支払わなければなりません。

支払う金額の合計は最小で何円ですか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq X \leq 10^6
  • 1 \leq A_i, C_i \leq 10^6 \, (1 \leq i \leq N)
  • 入力は全て整数である。

入力

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

N X
A_1 C_1
\vdots
A_N C_N

出力

答えを表す数値を出力せよ。


入力例 1

3 5
3 2
2 4
4 3

出力例 1

12

A_11 加算すると A の要素の種類数は 2 になり、支払う金額の合計は C_1 + 2 \times X = 12 円となります。支払う金額をこれより少なくすることはできません。


入力例 2

1 1
1 1

出力例 2

1

入力例 3

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

出力例 3

29

Score : 600 points

Problem Statement

Given are integer sequences of length N each: A = (A_1, \dots, A_N) and C = (C_1, \dots, C_N).

You can do the following operation any number of times, possibly zero.

  • Choose an integer i such that 1 \leq i \leq N and add 1 to the value of A_i, for a cost of C_i yen (Japanese currency).

After you are done with the operation, you have to pay K \times X yen, where K is the number of different values among the elements of A.

What is the minimum total amount of money you have to pay?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq X \leq 10^6
  • 1 \leq A_i, C_i \leq 10^6 \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
A_1 C_1
\vdots
A_N C_N

Output

Print a number representing the answer.


Sample Input 1

3 5
3 2
2 4
4 3

Sample Output 1

12

After adding 1 to A_1, there will be two different values among the elements of A, for a total cost of C_1 + 2 \times X = 12 yen. It is impossible to make the total cost less than this.


Sample Input 2

1 1
1 1

Sample Output 2

1

Sample Input 3

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

Sample Output 3

29