A - Bitwise Exclusive Or

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

0 以上 255 以下の整数 A,B が与えられます。 A \text{ xor }C=B となる 0 以上の整数 C を求めてください。

なお、そのような C はただ 1 つ存在し、0 以上 255 以下であることが証明されます。

\text{ xor } とは

整数 a, b のビットごとの排他的論理和 a \text{ xor } b は、以下のように定義されます。

  • a \text{ xor } b を二進表記した際の 2^k (k \geq 0) の位の数は、a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \text{ xor } 5 = 6 となります (二進表記すると: 011 \text{ xor } 101 = 110)。

制約

  • 0\leq A,B \leq 255
  • 入力に含まれる値は全て整数である

入力

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

A B

出力

答えを出力せよ。


入力例 1

3 6

出力例 1

5

3 は 二進表記で 115 は二進表記で 101 なので、これらの \text{xor} は二進表記で 110 であり、十進表記で 6 です。

このように、3 \text{ xor } 5 = 6 となるので、答えは 5 です。


入力例 2

10 12

出力例 2

6

図

Score : 100 points

Problem Statement

You are given integers A and B between 0 and 255 (inclusive). Find a non-negative integer C such that A \text{ xor }C=B.

It can be proved that there uniquely exists such C, and it will be between 0 and 255 (inclusive).

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of integers A and B, A\ \mathrm{XOR}\ B, is defined as follows:

  • When A\ \mathrm{XOR}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
For example, we have 3\ \mathrm{XOR}\ 5 = 6 (in base two: 011\ \mathrm{XOR}\ 101 = 110).

Constraints

  • 0\leq A,B \leq 255
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer.


Sample Input 1

3 6

Sample Output 1

5

When written in binary, 3 will be 11, and 5 will be 101. Thus, their \text{xor} will be 110 in binary, or 6 in decimal.

In short, 3 \text{ xor } 5 = 6, so the answer is 5.


Sample Input 2

10 12

Sample Output 2

6

Figure

B - Booby Prize

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1,\ldots,N の番号のついた N 人の選手がゲームを行いました。選手 i のスコアは A_i であり、スコアが小さい方が上位になります。

ブービー賞に該当する選手、すなわち、下位から 2 番目の選手の番号を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i は相異なる
  • 入力に含まれる値は全て整数である

入力

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

N
A_1 \ldots A_N 

出力

答えを出力せよ。


入力例 1

6
1 123 12345 12 1234 123456

出力例 1

3

6 人中 5 位になるのは、選手 3 です。


入力例 2

5
3 1 4 15 9

出力例 2

5

Score : 200 points

Problem Statement

N players, who are numbered 1, \ldots, N, have played a game. Player i has scored A_i, and a player with a smaller score ranks higher.

The player who ranks the second lowest will receive a booby prize. Who is this player? Answer with an integer representing the player.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N 

Output

Print the answer.


Sample Input 1

6
1 123 12345 12 1234 123456

Sample Output 1

3

It is Player 3 who ranks fifth among the six players.


Sample Input 2

5
3 1 4 15 9

Sample Output 2

5
C - Reorder Cards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

HW 列の格子状に HW 枚のカードが並べられています。
i=1,\ldots,N について、上から A_i 行目、左から B_i 列目にあるカードには数 i が書かれており、それ以外の HW-N 枚のカードには何も書かれていません。

これらのカードに対し、以下の 2 種類の操作を可能な限り繰り返します。

  • 数の書かれたカードを含まない行が存在するとき、その行のカードを全て取り除き、残りのカードを上へ詰める
  • 数の書かれたカードを含まない列が存在するとき、その列のカードを全て取り除き、残りのカードを左へ詰める

操作が終了したとき、数が書かれたカードがそれぞれどこにあるか求めてください。なお、答えは操作の仕方に依らず一意に定まることが証明されます。

制約

  • 1 \leq H,W \leq 10^9
  • 1 \leq N \leq \min(10^5,HW)
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq W
  • (A_i,B_i) は相異なる
  • 入力に含まれる値は全て整数である

入力

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

H W N
A_1 B_1
\vdots
A_N B_N

出力

N 行出力せよ。

操作終了後に数 i が書かれたカードが上から C_i 行目、左から D_i 列目に存在するとき、i 行目には C_i,D_i をこの順に空白区切りで出力せよ。


入力例 1

4 5 2
3 2
2 5

出力例 1

2 1
1 2

何も書かれていないカードを * で表すことにします。最初、カードの配置は以下の通りです。

*****
****2
*1***
*****

操作終了後、カードの配置は以下の通りになります。

*2
1*

1 が書かれたカードは上から 2 行目、左から 1 列目にあり、2 が書かれたカードは上から 1 行目、左から 2 列目にあります。


入力例 2

1000000000 1000000000 10
1 1
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000

出力例 2

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

Score : 300 points

Problem Statement

We have HW cards arranged in a matrix with H rows and W columns.
For each i=1, \ldots, N, the card at the A_i-th row from the top and B_i-th column from the left has a number i written on it. The other HW-N cards have nothing written on them.

We will repeat the following two kinds of operations on these cards as long as we can.

  • If there is a row without a numbered card, remove all the cards in that row and fill the gap by shifting the remaining cards upward.
  • If there is a column without a numbered card, remove all the cards in that column and fill the gap by shifting the remaining cards to the left.

Find the position of each numbered card after the end of the process above. It can be proved that these positions are uniquely determined without depending on the order in which the operations are done.

Constraints

  • 1 \leq H,W \leq 10^9
  • 1 \leq N \leq \min(10^5,HW)
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq W
  • All pairs (A_i,B_i) are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
A_1 B_1
\vdots
A_N B_N

Output

Print N lines.

If, after the end of the process, the card with the number i is at the C_i-th row from the top and D_i-th column from the left, the i-th line should contain C_i and D_i in this order with a space between them.


Sample Input 1

4 5 2
3 2
2 5

Sample Output 1

2 1
1 2

Let * represent a card with nothing written on it. The initial arrangement of cards is:

*****
****2
*1***
*****

After the end of the process, they will be arranged as follows:

*2
1*

Here, the card with 1 is at the 2-nd row from the top and 1-st column from the left, and the card with 2 is at the 1-st row from the top and 2-nd column from the left.


Sample Input 2

1000000000 1000000000 10
1 1
10 10
100 100
1000 1000
10000 10000
100000 100000
1000000 1000000
10000000 10000000
100000000 100000000
1000000000 1000000000

Sample Output 2

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
D - Takahashi Tour

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

AtCoder 国には 1 から N の番号がついた N 個の都市と、1 から N-1 の番号がついた N-1 個の道路があります。
道路 i を通ると都市 A_i と都市 B_i の間を相互に移動することができます。全ての都市は道路を使って互いに行き来できることが保証されます。

高橋くんは都市 1 を出発し、次のルールで旅をします。

  • いまいる都市と道路で直接つながっている都市のうち、まだ訪れたことがない都市が存在するとき、そのような都市のうち番号が最も小さい都市へ移動する
  • そのような都市が存在しないとき
    • いまいる都市が都市 1 なら旅を終了する
    • そうでないなら、いまいる都市を初めて訪れたときに直前にいた都市へ移動する

高橋くんが旅の過程で訪れる都市を順に答えてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i \leq N
  • 全ての都市は道路を使って互いに行き来できる

入力

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

出力

高橋くんが訪れる都市を、旅の開始時及び終了時の都市 1 も含めて順に空白区切りで出力せよ。


入力例 1

4
1 2
4 2
3 1

出力例 1

1 2 4 2 1 3 1

高橋くんの旅は次のようになります。

  • 最初都市 1 にいます。
  • 都市 1 から道路で直接つながっている都市のうち未訪問なのは都市 2,3 です。番号が小さい都市 2 へ移動します。
  • 都市 2 から道路で直接つながっている都市のうち未訪問なのは都市 4 です。都市 4 へ移動します。
  • 都市 4 から道路で直接つながっている都市のうち未訪問の都市はありません。直前にいた都市 2 へ移動します。
  • 都市 2 から道路で直接つながっている都市のうち未訪問の都市はありません。初めて都市 2 に来る直前にいた都市 1 へ移動します。
  • 都市 1 から道路で直接つながっている都市のうち未訪問なのは都市 3 です。都市 3 へ移動します。
  • 都市 3 から道路で直接つながっている都市のうち未訪問の都市はありません。直前にいた都市 1 へ移動します。
  • 都市 1 から道路で直接つながっている都市のうち未訪問の都市はありません。旅を終了します。

入力例 2

5
1 2
1 3
1 4
1 5

出力例 2

1 2 1 3 1 4 1 5 1

Score : 400 points

Problem Statement

The Republic of AtCoder has N cities numbered 1 through N and N-1 roads numbered 1 through N-1. Road i connects City A_i and City B_i bidirectionally. It is guaranteed that one can travel between every pair of cities using roads.

Takahashi will depart City 1 and have a journey by repeating the following.

  • If there are unvisited cities that are directly connected to the city Takahashi is in now, he goes to the city with the smallest number among those cities.
  • Otherwise,
    • if he is in City 1, he ends the journey;
    • otherwise, he goes to the city he was in just before visiting the current city for the first time.

Find the sequence of cities visited by Takahashi in the order he visits them.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i \leq N
  • It is possible to travel between every pair of cities using roads.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

Output

Print the sequence of cities visited by Takahashi in the order he visits them, including City 1 at the beginning and end of the journey, with spaces in between.


Sample Input 1

4
1 2
4 2
3 1

Sample Output 1

1 2 4 2 1 3 1

His journey will be as follows.

  • Start in City 1.
  • The unvisited cities directly connected to City 1 are City 2 and 3. Go to the city with the smaller number, that is, City 2.
  • The unvisited city directly connected to City 2 is City 4. Go there.
  • There is no unvisited city directly connected to City 4. Go back to City 2.
  • There is no unvisited city directly connected to City 2. Go to City 1, where he was in just before visiting City 2 for the first time.
  • The unvisited city directly connected to City 1 is City 3. Go there.
  • There is no unvisited city directly connected to City 3. Go back to City 1.
  • There is no unvisited city directly connected to City 1. End the journey.

Sample Input 2

5
1 2
1 3
1 4
1 5

Sample Output 2

1 2 1 3 1 4 1 5 1
E - Stronger Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

HW 列の格子状の区画に区切られた街があります。上から i 行目、左から j 列目の区画は、S_{i,j}. のとき道、# のとき塀です。

高橋君は自分の家から魚屋に買い物に行くことにしました。高橋君の家は街の左上隅の区画にあり、魚屋は街の右下隅の区画にあります。

高橋君は、自分がいる区画から上下左右に隣接する道の区画に移動することができます。街の外に出ることはできません。
塀の区画に移動することはできませんが、高橋君は非常に腕力が強いため、パンチを 1 回繰り出すことで任意の 2\times 2 の区画内の塀を壊して道にすることができます。

高橋君が魚屋にたどり着くためには、最低何回パンチを繰り出せばよいか求めてください。

制約

  • 2 \leq H,W \leq 500
  • H,W は整数
  • S_{i,j}. または #
  • S_{1,1}S_{H,W}.

入力

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

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

出力

答えを出力せよ。


入力例 1

5 5
..#..
#.#.#
##.##
#.#.#
..#..

出力例 1

1

例えば、以下の * で表す 2\times 2 の区画にある塀を破壊すると魚屋にたどり着くことができます。

..#..
#.**#
##**#
#.#.#
..#..

破壊対象の 2 \times 2 の区画の全てが塀である必要はありません。


入力例 2

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

出力例 2

0

遠回りが必要ですが、塀を破壊することなく魚屋にたどり着くことができます。


入力例 3

8 8
.#######
########
########
########
########
########
########
#######.

出力例 3

5

Score : 500 points

Problem Statement

There is a town divided into a grid of cells with H rows and W columns. The cell at the i-th row from the top and j-th column from the left is a passable space if S_{i,j} is . and a block if S_{i,j} is #.

Takahashi will go from his house to a fish market. His house is in the cell at the top-left corner, and the fish market is in the cell at the bottom-right corner.

Takahashi can move one cell up, down, left, or right to a passable cell. He cannot leave the town. He cannot enter a block, either. However, his physical strength allows him to destroy all blocks in a square region with 2\times 2 cells of his choice with one punch, making these cells passable.

Find the minimum number of punches needed for Takahashi to reach the fish market.

Constraints

  • 2 \leq H,W \leq 500
  • H and W are integers.
  • S_{i,j} is . or #.
  • S_{1,1} and S_{H,W} are ..

Input

Input is given from Standard Input in the following format:

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

Output

Print the answer.


Sample Input 1

5 5
..#..
#.#.#
##.##
#.#.#
..#..

Sample Output 1

1

He can reach the fish market by, for example, destroying the blocks in the square region with 2\times 2 cells marked * below.

..#..
#.**#
##**#
#.#.#
..#..

It is not required that all of the 2\times 2 cells in the region to punch are blocks.


Sample Input 2

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

Sample Output 2

0

He can reach the fish market without destroying blocks, though he has to go a long way around.


Sample Input 3

8 8
.#######
########
########
########
########
########
########
#######.

Sample Output 3

5
F - Common Prefixes

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 500

問題文

2 つの文字列 X,Y に対して、その類似度 f(X,Y) を、XY を先頭から見て一致している文字数とします。
例えば abcaxbc の類似度は 1aaaaaaa の類似度は 3 です。

長さ N の文字列 S が与えられます。Si 文字目以降からなる文字列を S_i とします。k=1,\ldots,N のそれぞれについて、f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N) を求めてください。

制約

  • 1 \leq N \leq 10^6
  • S は英小文字のみからなる長さ N の文字列

入力

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

N
S

出力

N 行出力せよ。

i 行目には k=i の問題の答えを出力せよ。


入力例 1

3
abb

出力例 1

3
3
2

S_1,S_2,S_3 はそれぞれ abb, bb, b です。

  • k=1 のとき f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3
  • k=2 のとき f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3
  • k=3 のとき f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2

入力例 2

11
mississippi

出力例 2

11
16
14
12
13
11
9
7
4
3
4

Score : 500 points

Problem Statement

Let the similarity f(X,Y) between two strings X and Y be the length of their longest common prefix.
For example, the similarity between abc and axbc is 1, and the similarity between aaa and aaaa is 3.

You are given a string S of length N. Let S_i be the suffix of S beginning with the i-th character of S. For each k=1,\ldots,N, find f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N).

Constraints

  • 1 \leq N \leq 10^6
  • S is a string of length N consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print N lines.

The i-th line should contain the answer for k=i.


Sample Input 1

3
abb

Sample Output 1

3
3
2

S_1 is abb, S_2 is bb, and S_3 is b.

  • For k=1: f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3.
  • For k=2: f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3.
  • For k=3: f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2.

Sample Input 2

11
mississippi

Sample Output 2

11
16
14
12
13
11
9
7
4
3
4
G - Connectivity 2

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の単純無向グラフ G が与えられます。頂点には 1,2,\dots,N、辺には 1,2,\dots,M の番号がついていて、辺 i は頂点 a_i と頂点 b_i を結んでいます。
G から 0 本以上の辺を取り除き、新しいグラフ H を作ることを考えます。H としてありうるグラフは全部で 2^M 個ありますが、そのうち頂点 1 と頂点 k が連結であるものの個数を 2 \leq k \leq N を満たす全ての整数 k に対して求めてください。
答えは非常に大きくなる可能性があるので、 998244353 で割ったあまりを出力してください。

制約

  • 2 \leq N \leq 17
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq a_i \lt b_i \leq N
  • 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 行出力せよ。i 行目には k = i + 1 のときの答えを出力せよ。


入力例 1

3 2
1 2
2 3

出力例 1

2
1

H としてあり得るグラフ、および頂点 1 と連結な頂点は次の通りです。

  • 辺が無いとき、頂点 1 はどの点とも連結でない。
  • 頂点 1 と頂点 2 を結ぶ辺だけがあるとき、頂点 1 は頂点 2 と連結である。
  • 頂点 2 と頂点 3 を結ぶ辺だけがあるとき、頂点 1 はどの点とも連結でない。
  • 両方の辺があるとき、頂点 1 は頂点 2 および頂点 3 と連結である。

入力例 2

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

出力例 2

43
31
37
41

入力例 3

2 0

出力例 3

0

Score : 600 points

Problem Statement

Given is a simple undirected graph G with N vertices and M edges. The vertices are numbered 1,2,\dots,N, the edges are numbered 1,2,\dots,M, and Edge i connects Vertex a_i and Vertex b_i.
Consider removing zero or more edges from G to get a new graph H. There are 2^M graphs that we can get as H. Among them, find the number of such graphs that Vertex 1 and Vertex k are directly or indirectly connected, for each integer k such that 2 \leq k \leq N.
Since the counts may be enormous, print them modulo 998244353.

Constraints

  • 2 \leq N \leq 17
  • 0 \leq M \leq \frac{N(N-1)}{2}
  • 1 \leq a_i \lt b_i \leq N
  • (a_i, b_i) \neq (a_j, b_j) if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
\vdots
a_M b_M

Output

Print N-1 lines. The i-th line should contain the answer for k = i + 1.


Sample Input 1

3 2
1 2
2 3

Sample Output 1

2
1

We can get the following graphs as H.

  • The graph with no edges. Vertex 1 is disconnected from any other vertex.
  • The graph with only the edge connecting Vertex 1 and 2. Vertex 2 is reachable from Vertex 1.
  • The graph with only the edge connecting Vertex 2 and 3. Vertex 1 is disconnected from any other vertex.
  • The graph with both edges. Vertex 2 and 3 are reachable from Vertex 1.

Sample Input 2

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

Sample Output 2

43
31
37
41

Sample Input 3

2 0

Sample Output 3

0
H - Stroll

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は家の周りをあてもなく歩き回ることにしました。
散歩の間、高橋君は地点 1, 地点 2, \dots, 地点 NN か所の地点を歩き回ります。ここで、地点 1 は自宅です。
地点間に道が存在するような地点の組は M 組あります。 i 番目の組を (a_i, b_i) とした時、地点 a_i と地点 b_i を双方向に結ぶ長さ d (1 \leq d \leq T) キロメートルの道は p_{i, d} 本あります。

高橋君は自宅を出発して T キロメートル歩いて自宅に戻る散歩コースの本数が知りたくなりました。ここで、長さ T キロメートルの散歩コースは次のように定義されます。

  • 地点と道を交互に並べた列 v_0 = 1, e_0, v_1, \dots,e_{k-1}, v_k = 1 であって、e_i (0 \leq i \leq k-1)v_iv_{i+1} を結んでいて、 e_i の長さの和が T キロメートルである。

あなたは高橋君のかわりに条件を満たす散歩コースの本数を 998244353 で割ったあまりを求めてください。ただし、2 つの散歩コースは列として異なるときに異なるとみなされます。

制約

  • 2 \leq N \leq 10
  • 1 \leq M \leq \min \left(10, \frac{N(N-1)}{2} \right)
  • 1 \leq T \leq 4 \times 10^4
  • 1 \leq a_i \lt b_i \leq N
  • i \neq j ならば (a_i, b_i) \neq (a_j, b_j)
  • 0 \leq p_{i,j} \lt 998244353

入力

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

N M T
a_1 b_1
p_{1,1} p_{1,2} \ldots p_{1,T}
\vdots
a_M b_M
p_{M,1} p_{M,2} \ldots p_{M,T}

出力

条件を満たす散歩コースの本数を 998244353 で割ったあまりを出力せよ。


入力例 1

3 2 2
1 2
1 0
1 3
2 0

出力例 1

5

高橋君の家の周りには、

  • 地点 1 と地点 2 を結ぶ長さ 1 キロメートルの道が 1
  • 地点 1 と地点 3 を結ぶ長さ 1 キロメートルの道が 2

あります。条件を満たすコースは

  • 地点 1 \to 地点 2 \to 地点 1 の順に巡るコースが 1 \times 1 = 1 通り
  • 地点 1 \to 地点 3 \to 地点 1 の順に巡るコースが 2 \times 2 = 4 通り

の計 5 通りです。


入力例 2

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

出力例 2

130

高橋君の家の周りには、

  • 地点 1 と地点 2 を結ぶ長さ 1 キロメートルの道が 3
  • 地点 1 と地点 3 を結ぶ長さ 2 キロメートルの道が 1
  • 地点 2 と地点 3 を結ぶ長さ 1 キロメートルの道が 2

あります。条件を満たすコースは、経由する地点を列挙すると

  • 地点 1 \to 地点 2 \to 地点 1 \to 地点 2 \to 地点 1
  • 地点 1 \to 地点 2 \to 地点 3 \to 地点 1
  • 地点 1 \to 地点 2 \to 地点 3 \to 地点 2 \to 地点 1
  • 地点 1 \to 地点 3 \to 地点 1
  • 地点 1 \to 地点 3 \to 地点 2 \to 地点 1

5 パターンがあり、本数は上から順に 81 通り、 6 通り、 36 通り、 1 通り、 6 通りとなります。


入力例 3

2 1 5
1 2
31415 92653 58979 32384 62643

出力例 3

844557977

Score : 600 points

Problem Statement

Takahashi has decided to wander around his house.
During his walk, he will roam between N points called Point 1, Point 2, \dots, Point N, where Point 1 is his house.
There are M pairs of points connected by roads; let (a_i, b_i) be the i-th of these pairs. There are p_{i, d} roads with a length of d (1 \leq d \leq T) kilometers connecting Point a_i and Point b_i.

Takahashi wants to know the number of T kilometers courses that begin and end at his house. Here, a T kilometers course is defined as follows.

  • An alternating sequence with points and roads v_0 = 1, e_0, v_1, \dots,e_{k-1}, v_k = 1 such that e_i (0 \leq i \leq k-1) connects v_i and v_{i+1}, and the total length of e_i is T kilometers.

Help Takahashi by finding the number of such courses modulo 998244353. Two courses are considered different when they are different as sequences.

Constraints

  • 2 \leq N \leq 10
  • 1 \leq M \leq \min \left(10, \frac{N(N-1)}{2} \right)
  • 1 \leq T \leq 4 \times 10^4
  • 1 \leq a_i \lt b_i \leq N
  • (a_i, b_i) \neq (a_j, b_j) if i \neq j.
  • 0 \leq p_{i,j} \lt 998244353

Input

Input is given from Standard Input in the following format:

N M T
a_1 b_1
p_{1,1} p_{1,2} \ldots p_{1,T}
\vdots
a_M b_M
p_{M,1} p_{M,2} \ldots p_{M,T}

Output

Print the number of desirable courses, modulo 998244353.


Sample Input 1

3 2 2
1 2
1 0
1 3
2 0

Sample Output 1

5

Around his house, there are:

  • one 1-kilometer road connecting Point 1 and Point 2, and
  • two 1-kilometer roads connecting Point 1 and Point 3.

We have the following five desirable courses:

  • 1 \times 1 = 1 course that goes Point 1 \to Point 2 \to Point 1, and
  • 2 \times 2 = 4 courses that goes Point 1 \to Point 3 \to Point 1.

Sample Input 2

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

Sample Output 2

130

Around his house, there are:

  • three 1-kilometer roads connecting Point 1 and Point 2,
  • one 2-kilometer road connecting Point 1 and Point 3, and
  • two 1-kilometer roads connecting Point 2 and Point 3.

The desirable courses can be classified into the following categories, according to the points visited:

  • Point 1 \to Point 2 \to Point 1 \to Point 2 \to Point 1,
  • Point 1 \to Point 2 \to Point 3 \to Point 1,
  • Point 1 \to Point 2 \to Point 3 \to Point 2 \to Point 1,
  • Point 1 \to Point 3 \to Point 1, and
  • Point 1 \to Point 3 \to Point 2 \to Point 1.

We have 81, 6, 36, 1, and 6 course(s) of these categories, respectively.


Sample Input 3

2 1 5
1 2
31415 92653 58979 32384 62643

Sample Output 3

844557977