A - Horizon

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

配点 : 100

問題文

地上 x メートルの高さから見える水平線は \sqrt{x(12800000+x)} メートル先にあるとするとき、 地上 H メートルの高さから見える水平線が何メートル先にあるか求めてください。

制約

  • 1 \leq H \leq 10^5
  • H は整数である

入力

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

H

出力

答えを出力せよ。
なお、想定解との絶対誤差または相対誤差が 10^{-6} 以下であれば、正解として扱われる。


入力例 1

333

出力例 1

65287.907678222

\sqrt{333(12800000+333)} = 65287.9076782\ldots です。65287.91 などの出力でも正解となります。


入力例 2

634

出力例 2

90086.635834623

\sqrt{634(12800000+634)} = 90086.6358346\ldots です。

Score : 100 points

Problem Statement

Assuming that the horizon seen from a place x meters above the ground is \sqrt{x(12800000+x)} meters away, find how many meters away the horizon seen from a place H meters above the ground is.

Constraints

  • 1 \leq H \leq 10^5
  • H is an integer.

Input

Input is given from Standard Input in the following format:

H

Output

Print the answer.
Your answer will be considered correct when the absolute or relative error from the judge's answer is at most 10^{-6}.


Sample Input 1

333

Sample Output 1

65287.907678222

We have \sqrt{333(12800000+333)} = 65287.9076782\ldots. Outputs such as 65287.91 would also be accepted.


Sample Input 2

634

Sample Output 2

90086.635834623

We have \sqrt{634(12800000+634)} = 90086.6358346\ldots.

B - Lacked Number

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

配点 : 100

問題文

数字のみからなる、長さがちょうど 9 の文字列 S が与えられます。
S には 0 から 9 までのうち、ちょうど 1 つの数字を除いた 9 種類の数字が一度ずつ登場します。

S に登場しない唯一の数字を出力してください。

制約

  • S は数字のみからなる長さ 9 の文字列である。
  • S の文字はすべて相異なる。

入力

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

S

出力

S に登場しない唯一の数字を出力せよ。


入力例 1

023456789

出力例 1

1

文字列 023456789 には 1 のみが登場していません。 よって、1 を出力します。


入力例 2

459230781

出力例 2

6

文字列 459230781 には 6 のみが登場していません。 よって、6 を出力します。

文字列に数字が現れる順番は昇順とは限らないので注意してください。

Score : 100 points

Problem Statement

You are given a string S of length exactly 9 consisting of digits. One but all digits from 0 to 9 appear exactly once in S.

Print the only digit missing in S.

Constraints

  • S is a string of length 9 consisting of digits.
  • All characters in S are distinct.

Input

Input is given from Standard Input in the following format:

S

Output

Print the only digit missing in S.


Sample Input 1

023456789

Sample Output 1

1

The string 023456789 only lacks 1. Thus, 1 should be printed.


Sample Input 2

459230781

Sample Output 2

6

The string 459230781 only lacks 6. Thus, 6 should be printed.

Note that the digits in the string may not appear in increasing order.

C - Round-Robin Tournament

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

配点 : 200

問題文

1 から N までの番号が付いた N 人のプレイヤーが総当たり戦をしました。この総当たり戦で行われた試合全てについて、二人の一方が勝ち、もう一方が負けました。

総当たり戦の結果は N 個の長さ N の文字列 S_1,S_2,\ldots,S_N によって以下の形式で与えられます。

  • i\neq j のとき、S_ij 文字目は o, x のいずれかであり、o のときプレイヤー i がプレイヤー j に勝ったことを、x のときプレイヤー i がプレイヤー j に負けたことを意味する。

  • i=j のとき、S_ij 文字目は - である。

総当たり戦で勝った試合数が多いほうが順位が上であり、勝った試合数が同じ場合は、プレイヤーの番号が小さいほうが順位が上となります。 N 人のプレイヤーの番号を順位が高い順に答えてください。

制約

  • 2\leq N\leq 100
  • N は整数
  • S_io, x, - からなる長さ N の文字列
  • S_1,\ldots,S_N は問題文中の形式を満たす

入力

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

N 
S_1
S_2
\vdots
S_N

出力

N 人のプレイヤーの番号を、順位が高い順に空白区切りで出力せよ。


入力例 1

3
-xx
o-x
oo-

出力例 1

3 2 1

プレイヤー 10 勝、プレイヤー 21 勝、プレイヤー 32 勝なので、プレイヤーの番号は順位が高い順に 3,2,1 です。


入力例 2

7
-oxoxox
x-xxxox
oo-xoox
xoo-ooo
ooxx-ox
xxxxx-x
oooxoo-

出力例 2

4 7 3 1 5 2 6

プレイヤー 4 とプレイヤー 7 はどちらも 5 勝ですが、プレイヤー番号が小さいプレイヤー 4 のほうが順位が上になります。

Score : 200 points

Problem Statement

There are N players numbered 1 to N, who have played a round-robin tournament. For every match in this tournament, one player won and the other lost.

The results of the matches are given as N strings S_1,S_2,\ldots,S_N of length N each, in the following format:

  • If i\neq j, the j-th character of S_i is o or x. o means that player i won against player j, and x means that player i lost to player j.

  • If i=j, the j-th character of S_i is -.

The player with more wins ranks higher. If two players have the same number of wins, the player with the smaller player number ranks higher. Report the player numbers of the N players in descending order of rank.

Constraints

  • 2\leq N\leq 100
  • N is an integer.
  • S_i is a string of length N consisting of o, x, and -.
  • S_1,\ldots,S_N conform to the format described in the problem statement.

Input

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

N 
S_1
S_2
\vdots
S_N

Output

Print the player numbers of the N players in descending order of rank.


Sample Input 1

3
-xx
o-x
oo-

Sample Output 1

3 2 1

Player 1 has 0 wins, player 2 has 1 win, and player 3 has 2 wins. Thus, the player numbers in descending order of rank are 3,2,1.


Sample Input 2

7
-oxoxox
x-xxxox
oo-xoox
xoo-ooo
ooxx-ox
xxxxx-x
oooxoo-

Sample Output 2

4 7 3 1 5 2 6

Both players 4 and 7 have 5 wins, but player 4 ranks higher because their player number is smaller.

D - Hammer

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

配点 : 200

問題文

数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。

座標 Y には壁があり、最初、高橋君は壁を超えて移動することができません。
座標 Z にあるハンマーを拾った後でなら、壁を破壊して通過できるようになります。

高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。

制約

  • -1000 \leq X,Y,Z \leq 1000
  • X,Y,Z は相異なり、いずれも 0 でない
  • 入力に含まれる値は全て整数である

入力

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

X Y Z

出力

高橋君がゴールに到達することが可能であれば、移動距離の最小値を出力せよ。不可能であれば、かわりに -1 と出力せよ。


入力例 1

10 -10 1

出力例 1

10

高橋君はまっすぐゴールに向かうことができます。


入力例 2

20 10 -10

出力例 2

40

ゴールは壁の向こう側にあります。まずハンマーを拾い、壁を壊すことでゴールに到達することができます。


入力例 3

100 1 1000

出力例 3

-1

Score : 200 points

Problem Statement

Takahashi is at the origin of a number line. He wants to reach a goal at coordinate X.

There is a wall at coordinate Y, which Takahashi cannot go beyond at first.
However, after picking up a hammer at coordinate Z, he can destroy that wall and pass through.

Determine whether Takahashi can reach the goal. If he can, find the minimum total distance he needs to travel to do so.

Constraints

  • -1000 \leq X,Y,Z \leq 1000
  • X, Y, and Z are distinct, and none of them is 0.
  • All values in the input are integers.

Input

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

X Y Z

Output

If Takahashi can reach the goal, print the minimum total distance he needs to travel to do so. If he cannot, print -1 instead.


Sample Input 1

10 -10 1

Sample Output 1

10

Takahashi can go straight to the goal.


Sample Input 2

20 10 -10

Sample Output 2

40

The goal is beyond the wall. He can get there by first picking up the hammer and then destroying the wall.


Sample Input 3

100 1 1000

Sample Output 3

-1
E - Counting Squares

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

配点 : 300

問題文

二次元平面があります。1 以上 9 以下の整数 r,c について、S_{r}c 番目の文字が # であるとき座標 (r,c) にポーンが置いてあり、S_{r}c 番目の文字が . であるとき座標 (r,c) に何も置かれていません。

この平面上の正方形であって、4 頂点全てにポーンが置いてあるものの個数を求めてください。

制約

  • S_1,\ldots,S_9 はそれぞれ #. からなる長さ 9 の文字列

入力

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

S_1
S_2
\vdots
S_9

出力

答えを出力せよ。


入力例 1

##.......
##.......
.........
.......#.
.....#...
........#
......#..
.........
.........

出力例 1

2

座標 (1,1),(1,2),(2,2),(2,1) を頂点とする正方形は、4 頂点全てにポーンが置かれています。

座標 (4,8),(5,6),(7,7),(6,9) を頂点とする正方形も、4 頂点全てにポーンが置かれています。

よって答えは 2 です。


入力例 2

.#.......
#.#......
.#.......
.........
....#.#.#
.........
....#.#.#
........#
.........

出力例 2

3

Score : 300 points

Problem Statement

There is a two-dimensional plane. For integers r and c between 1 and 9, there is a pawn at the coordinates (r,c) if the c-th character of S_{r} is #, and nothing if the c-th character of S_{r} is ..

Find the number of squares in this plane with pawns placed at all four vertices.

Constraints

  • Each of S_1,\ldots,S_9 is a string of length 9 consisting of # and ..

Input

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

S_1
S_2
\vdots
S_9

Output

Print the answer.


Sample Input 1

##.......
##.......
.........
.......#.
.....#...
........#
......#..
.........
.........

Sample Output 1

2

The square with vertices (1,1), (1,2), (2,2), and (2,1) have pawns placed at all four vertices.

The square with vertices (4,8), (5,6), (7,7), and (6,9) also have pawns placed at all four vertices.

Thus, the answer is 2.


Sample Input 2

.#.......
#.#......
.#.......
.........
....#.#.#
.........
....#.#.#
........#
.........

Sample Output 2

3
F - Paint to make a rectangle

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

配点 : 300

問題文

HW 列のマス目が与えられます。
以下、上から i 行目 (1\leq i\leq H) かつ左から j 列目 (1\leq j\leq W) のマスをマス (i,j) で表します。
マス目の状態は H 個の長さ W の文字列 S_1,S_2, \ldots, S_H によって以下のように表されます。

  • S_ij 文字目が # のとき、マス (i,j) は黒く塗られている。
  • S_ij 文字目が . のとき、マス (i,j) は白く塗られている。
  • S_ij 文字目が ? のとき、マス (i,j) は塗られていない。

高橋君はまだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにしたいです。
より具体的には、ある 4 つの整数の組 (a,b,c,d) (1\leq a\leq b\leq H, 1\leq c\leq d\leq W) が存在して、次が成り立つようにしたいです。

マス (i,j) (1\leq i\leq H, 1\leq j\leq W) は、 a\leq i\leq b かつ c\leq j\leq d をみたすとき、黒く塗られている。
そうでないとき、白く塗られている。

そのようなことが可能か判定してください。

制約

  • 1\leq H,W\leq 1000
  • H, W は整数
  • S_i#, ., ? のみからなる長さ W の文字列
  • 黒く塗られたマスが 1 つ以上存在する。

入力

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

H W
S_1
S_2
\vdots
S_H

出力

まだ塗られていないマスをそれぞれ白または黒で塗ることで、黒マス全体が長方形をなすようにできるならば Yes を、そうでないならば No を出力せよ。


入力例 1

3 5
.#?#.
.?#?.
?...?

出力例 1

Yes

マス目は以下の状態になっています。? のマスがまだ塗られていないマスです。

マス (1,3), (2,2), (2,4) を黒く塗り、マス (3,1), (3,5) を白く塗ることで、 以下のように黒マス全体が長方形をなすようにできます。

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


入力例 2

3 3
?##
#.#
##?

出力例 2

No

黒マス全体が長方形をなすためには、少なくともマス (2,2) を黒く塗る必要がありますがすでに白く塗られています。
よって、黒マス全体が長方形をなすようにマス目を塗ることはできないため、No を出力します。


入力例 3

1 1
#

出力例 3

Yes

Score : 300 points

Problem Statement

You are given a grid of H rows and W columns.
Let (i,j) denote the cell at row i (1 \leq i \leq H) from the top and column j (1 \leq j \leq W) from the left.
The state of the grid is represented by H strings S_1, S_2, \ldots, S_H, each of length W, as follows:

  • If the j-th character of S_i is #, cell (i,j) is painted black.
  • If the j-th character of S_i is ., cell (i,j) is painted white.
  • If the j-th character of S_i is ?, cell (i,j) is not yet painted.

Takahashi wants to paint each not-yet-painted cell white or black so that all the black cells form a rectangle.
More precisely, he wants there to exist a quadruple of integers (a,b,c,d) (1 \leq a \leq b \leq H, 1 \leq c \leq d \leq W) such that:

For each cell (i,j) (1 \leq i \leq H, 1 \leq j \leq W), if a \leq i \leq b and c \leq j \leq d, the cell is black;
otherwise, the cell is white.

Determine whether this is possible.

Constraints

  • 1 \leq H, W \leq 1000
  • H and W are integers.
  • Each S_i is a string of length W consisting of #, ., ?.
  • There is at least one cell that is already painted black.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

If it is possible to paint all the not-yet-painted cells so that the black cells form a rectangle, print Yes; otherwise, print No.


Sample Input 1

3 5
.#?#.
.?#?.
?...?

Sample Output 1

Yes

The grid is in the following state. ? indicates a cell that are not yet painted.

By painting cells (1,3), (2,2), and (2,4) black and cells (3,1) and (3,5) white, the black cells can form a rectangle as follows:

Therefore, print Yes.


Sample Input 2

3 3
?##
#.#
##?

Sample Output 2

No

To form a rectangle with all black cells, you would need to paint cell (2,2) black, but it is already painted white.
Therefore, it is impossible to make all black cells form a rectangle, so print No.


Sample Input 3

1 1
#

Sample Output 3

Yes
G - Dance

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

配点 : 400

問題文

1, 2, \ldots, 2N と番号づけられた 2N 人の人が舞踏会に参加します。 彼らは N 個の 2 人組にわかれてダンスを踊ります。

2 人組を構成する人のうち、番号の小さい方の人が人 i 、番号の大きい方の人が人 j のとき、 その 2 人組の「相性」は A_{i, j} です。
N 個の 2 人組の相性がそれぞれ B_1, B_2, \ldots, B_N であるとき、 「舞踏会全体の楽しさ」はそれらのビットごとの排他的論理和である B_1 \oplus B_2 \oplus \cdots \oplus B_N です。

2N 人の参加者が N 個の 2 人組に分かれる方法」を自由に選べるとき、「舞踏会全体の楽しさ」としてあり得る最大値を出力してください。

制約

  • 1 \leq N \leq 8
  • 0 \leq A_{i, j} < 2^{30}
  • 入力はすべて整数

入力

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

N
A_{1, 2} A_{1, 3} A_{1, 4} \cdots A_{1, 2N}
A_{2, 3} A_{2, 4} \cdots A_{2, 2N}
A_{3, 4} \cdots A_{3, 2N}
\vdots
A_{2N-1, 2N}

出力

舞踏会全体の楽しさとしてあり得る最大値を出力せよ。


入力例 1

2
4 0 1
5 3
2

出力例 1

6

i と人 j からなる 2 人組を \lbrace i, j\rbrace で表します。 4 人が 2 個の 2 人組にわかれる方法は下記の 3 通りです。

  • \lbrace 1, 2\rbrace, \lbrace 3, 4\rbrace という 2 組にわかれる。 このとき、舞踏会全体の楽しさは A_{1, 2} \oplus A_{3, 4} = 4 \oplus 2 = 6 です。
  • \lbrace 1, 3\rbrace, \lbrace 2, 4\rbrace という 2 組にわかれる。 このとき、舞踏会全体の楽しさは A_{1, 3} \oplus A_{2, 4} = 0 \oplus 3 = 3 です。
  • \lbrace 1, 4\rbrace, \lbrace 2, 3\rbrace という 2 組にわかれる。 このとき、舞踏会全体の楽しさは A_{1, 4} \oplus A_{2, 3} = 1 \oplus 5 = 4 です。

よって、舞踏会全体の楽しさとしてあり得る最大値は 6 です。


入力例 2

1
5

出力例 2

5

1 と人 2 からなる 2 人組のみが作られ、このときの舞踏会全体の楽しさは 5 です。


入力例 3

5
900606388 317329110 665451442 1045743214 260775845 726039763 57365372 741277060 944347467
369646735 642395945 599952146 86221147 523579390 591944369 911198494 695097136
138172503 571268336 111747377 595746631 934427285 840101927 757856472
655483844 580613112 445614713 607825444 252585196 725229185
827291247 105489451 58628521 1032791417 152042357
919691140 703307785 100772330 370415195
666350287 691977663 987658020
1039679956 218233643
70938785

出力例 3

1073289207

Score : 400 points

Problem Statement

2N people numbered 1, 2, \ldots, 2N attend a ball. They will group into N pairs and have a dance.

If Person i and Person j pair up, where i is smaller than j, the affinity of that pair is A_{i, j}.
If the N pairs have the affinity of B_1, B_2, \ldots, B_N, the total fun of the ball is the bitwise XOR of them: B_1 \oplus B_2 \oplus \cdots \oplus B_N.

Print the maximum possible total fun of the ball when the 2N people can freely group into N pairs.

Constraints

  • 1 \leq N \leq 8
  • 0 \leq A_{i, j} < 2^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_{1, 2} A_{1, 3} A_{1, 4} \cdots A_{1, 2N}
A_{2, 3} A_{2, 4} \cdots A_{2, 2N}
A_{3, 4} \cdots A_{3, 2N}
\vdots
A_{2N-1, 2N}

Output

Print the maximum possible total fun of the ball.


Sample Input 1

2
4 0 1
5 3
2

Sample Output 1

6

Let \lbrace i, j\rbrace denote a pair of Person i and Person j. There are three ways for the four people to group into two pairs, as follows.

  • Group into \lbrace 1, 2\rbrace, \lbrace 3, 4\rbrace. The total fun of the ball here is A_{1, 2} \oplus A_{3, 4} = 4 \oplus 2 = 6.
  • Group into \lbrace 1, 3\rbrace, \lbrace 2, 4\rbrace. The total fun of the ball here is A_{1, 3} \oplus A_{2, 4} = 0 \oplus 3 = 3.
  • Group into \lbrace 1, 4\rbrace, \lbrace 2, 3\rbrace. The total fun of the ball here is A_{1, 4} \oplus A_{2, 3} = 1 \oplus 5 = 4.

Therefore, the maximum possible total fun of the ball is 6.


Sample Input 2

1
5

Sample Output 2

5

There will be just a pair of Person 1 and Person 2, where the total fun of the ball is 5.


Sample Input 3

5
900606388 317329110 665451442 1045743214 260775845 726039763 57365372 741277060 944347467
369646735 642395945 599952146 86221147 523579390 591944369 911198494 695097136
138172503 571268336 111747377 595746631 934427285 840101927 757856472
655483844 580613112 445614713 607825444 252585196 725229185
827291247 105489451 58628521 1032791417 152042357
919691140 703307785 100772330 370415195
666350287 691977663 987658020
1039679956 218233643
70938785

Sample Output 3

1073289207
H - K-th Largest Connected Components

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

配点 : 475

問題文

N 頂点 0 辺の無向グラフがあります。頂点には 1 から N の番号がつけられています。

Q 個のクエリが与えられるので、与えられた順に処理してください。各クエリは以下の 2 種類のいずれかです。

  • タイプ 1 : 1 u v の形式で与えられる。頂点 u と頂点 v の間に辺を追加する。
  • タイプ 2 : 2 v k の形式で与えられる。頂点 v と連結な頂点の中で、k 番目に頂点番号が大きいものを出力する。ただし、頂点 v と連結な頂点が k 個未満のときは -1 を出力する。

制約

  • 1\leq N,Q\leq 2\times 10^5
  • タイプ 1 のクエリにおいて、1\leq u\lt v\leq N
  • タイプ 2 のクエリにおいて、1\leq v\leq N, 1\leq k\leq 10
  • 入力は全て整数

入力

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

ただし、\mathrm{query}_ii 個目のクエリであり、以下のいずれかの形式で与えられる。

1 u v
2 v k

出力

タイプ 2 のクエリの個数を q として、q 行出力せよ。i 行目には i 個目のタイプ 2 に対するクエリの答えを出力せよ。


入力例 1

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

出力例 1

2
1
-1
4
2
-1
  • 1 個目のクエリについて、頂点 1 と頂点 2 の間に辺が追加されます。
  • 2 個目のクエリについて、頂点 1 と連結な頂点は 1,22 つです。この中で 1 番目に大きい 2 を出力します。
  • 3 個目のクエリについて、頂点 1 と連結な頂点は 1,22 つです。この中で 2 番目に大きい 1 を出力します。
  • 4 個目のクエリについて、頂点 1 と連結な頂点は 1,22 つです。3 個未満なので -1 を出力します。
  • 5 個目のクエリについて、頂点 1 と頂点 3 の間に辺が追加されます。
  • 6 個目のクエリについて、頂点 2 と頂点 3 の間に辺が追加されます。
  • 7 個目のクエリについて、頂点 3 と頂点 4 の間に辺が追加されます。
  • 8 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,44 つです。この中で 1 番目に大きい 4 を出力します。
  • 9 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,44 つです。この中で 3 番目に大きい 2 を出力します。
  • 10 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,44 つです。5 個未満なので -1 を出力します。

入力例 2

6 20
1 3 4
1 3 5
2 1 1
2 3 1
1 1 5
2 6 9
2 1 3
2 6 1
1 4 6
2 2 1
2 6 2
2 4 7
1 1 4
2 6 2
2 3 4
1 2 5
2 4 1
1 1 6
2 3 3
2 1 3

出力例 2

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

Score : 475 points

Problem Statement

There is an undirected graph with N vertices and 0 edges. The vertices are numbered 1 to N.

You are given Q queries to process in order. Each query is of one of the following two types:

  • Type 1: Given in the format 1 u v. Add an edge between vertices u and v.
  • Type 2: Given in the format 2 v k. Print the k-th largest vertex number among the vertices connected to vertex v. If there are fewer than k vertices connected to v, print -1.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • In a Type 1 query, 1 \leq u < v \leq N.
  • In a Type 2 query, 1 \leq v \leq N, 1 \leq k \leq 10.
  • All input values are integers.

Input

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Here, \mathrm{query}_i is the i-th query and is given in one of the following formats:

1 u v
2 v k

Output

Let q be the number of Type 2 queries. Print q lines. The i-th line should contain the answer to the i-th Type 2 query.


Sample Input 1

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

Sample Output 1

2
1
-1
4
2
-1
  • In the first query, an edge is added between vertices 1 and 2.
  • In the second query, two vertices are connected to vertex 1: 1 and 2. Among them, the 1-st largest vertex number is 2, which should be printed.
  • In the third query, two vertices are connected to vertex 1: 1 and 2. Among them, the 2-nd largest vertex number is 1, which should be printed.
  • In the fourth query, two vertices are connected to vertex 1: 1 and 2, which is fewer than 3, so print -1.
  • In the fifth query, an edge is added between vertices 1 and 3.
  • In the sixth query, an edge is added between vertices 2 and 3.
  • In the seventh query, an edge is added between vertices 3 and 4.
  • In the eighth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 1-st largest vertex number is 4, which should be printed.
  • In the ninth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 3-rd largest vertex number is 2, which should be printed.
  • In the tenth query, four vertices are connected to vertex 1: 1,2,3,4, which is fewer than 5, so print -1.

Sample Input 2

6 20
1 3 4
1 3 5
2 1 1
2 3 1
1 1 5
2 6 9
2 1 3
2 6 1
1 4 6
2 2 1
2 6 2
2 4 7
1 1 4
2 6 2
2 3 4
1 2 5
2 4 1
1 1 6
2 3 3
2 1 3

Sample Output 2

1
5
-1
3
6
2
5
-1
5
3
6
4
4
I - Variety of Digits

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

配点 : 500

問題文

M 個の数字 C_i が与えられます。

1 以上 N 以下の整数のうち、先頭に余分な 0 をつけずに 10 進法で表した時に C_1,\ldots,C_M を全て含むようなもの全ての和を、 998244353 で割った余りを求めてください。

制約

  • 1 \leq N < 10^{10^4}
  • 1 \leq M \leq 10
  • 0 \leq C_1 < \ldots < C_M \leq 9
  • 入力に含まれる値は全て整数である

入力

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

N
M
C_1 \ldots C_M

出力

答えを出力せよ。


入力例 1

104
2
0 1

出力例 1

520

1 以上 104 以下の整数のうち、10 進法で表した時に 0, 1 を共に含むようなものは、10,100,101,102,103,1046 個あります。
これらの和は 520 です。


入力例 2

999
4
1 2 3 4

出力例 2

0

1 以上 999 以下の整数で、1, 2, 3, 4 を全て含むようなものは存在しません。


入力例 3

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
5
0 2 4 6 8

出力例 3

397365274

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

Score : 500 points

Problem Statement

Given are M digits C_i.

Find the sum, modulo 998244353, of all integers between 1 and N (inclusive) that contain all of C_1, \ldots, C_M when written in base 10 without unnecessary leading zeros.

Constraints

  • 1 \leq N < 10^{10^4}
  • 1 \leq M \leq 10
  • 0 \leq C_1 < \ldots < C_M \leq 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
M
C_1 \ldots C_M

Output

Print the answer.


Sample Input 1

104
2
0 1

Sample Output 1

520

Between 1 and 104, there are six integers that contain both 0 and 1 when written in base 10: 10,100,101,102,103,104.
The sum of them is 520.


Sample Input 2

999
4
1 2 3 4

Sample Output 2

0

Between 1 and 999, no integer contains all of 1, 2, 3, 4.


Sample Input 3

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
5
0 2 4 6 8

Sample Output 3

397365274

Be sure to find the sum modulo 998244353.