A - Digit Machine

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

1 桁の数字が表示される画面と、ボタンからなる機械があります。

画面に数字 k が表示されているとき、ボタンを 1 回押すと画面の数字が a_k に変わります。

0 が表示されている状態からボタンを 3 回押すと、画面には何が表示されますか?

制約

  • 0\leq a_i \leq 9
  • 入力は全て整数である

入力

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

a_0 a_1 \dots a_9

出力

答えを出力せよ。


入力例 1

9 0 1 2 3 4 5 6 7 8

出力例 1

7

画面に表示される数字は、0 \rightarrow 9 \rightarrow 8 \rightarrow 7 と変化します。


入力例 2

4 8 8 8 0 8 8 8 8 8

出力例 2

4

画面に表示される数字は、0 \rightarrow 4 \rightarrow 0 \rightarrow 4 と変化します。


入力例 3

0 0 0 0 0 0 0 0 0 0

出力例 3

0

Score : 100 points

Problem Statement

There is a device with a screen that shows a single-digit number, and a button.

When the screen is showing a number k, pressing the button once changes the number on the screen to a_k.

The device currently shows 0. After pressing the button 3 times, what will be shown on the screen?

Constraints

  • 0\leq a_i \leq 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a_0 a_1 \dots a_9

Output

Print the answer.


Sample Input 1

9 0 1 2 3 4 5 6 7 8

Sample Output 1

7

The number on the screen transitions as 0 \rightarrow 9 \rightarrow 8 \rightarrow 7.


Sample Input 2

4 8 8 8 0 8 8 8 8 8

Sample Output 2

4

The number on the screen transitions as 0 \rightarrow 4 \rightarrow 0 \rightarrow 4.


Sample Input 3

0 0 0 0 0 0 0 0 0 0

Sample Output 3

0
B - Pasta

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君の家には N 本の麺からなるパスタがあり、i 本目の麺の長さは A_i です。
高橋君はこれから M 日間の食事計画を立てており、 i 日目にはパスタの麺のうち長さがちょうど B_i であるようなものを 1 本選び、食べようと考えています。 もし、1 日目から M 日目の間に 1 日でもそのような麺が無い日があれば、食事計画は失敗となります。 また、同じ麺を複数の日に食べることはできません。

高橋君が食事計画を最後まで実行することは可能ですか?

制約

  • 1 \leq M \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力はすべて整数である。

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

高橋君が食事計画を最後まで実行できる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3 2
1 1 3
3 1

出力例 1

Yes

1 日目に 3 本目の麺を、2 日目に 1 本目の麺を食べれば良いので、高橋君の食事計画は実行可能です。


入力例 2

1 1
1000000000
1

出力例 2

No

長さがちょうど 1 の麺が存在する必要があります。


入力例 3

5 2
1 2 3 4 5
5 5

出力例 3

No

長さが 5 の麺は 1 本しか存在しないため、2 日目に食事をとる事が出来ません。

Score : 200 points

Problem Statement

There is pasta consisting of N noodles at Takahashi's home. The length of the i-th noodle is A_i.
Takahashi has a meal plan for the next M days. On the i-th day, he is going to choose a pasta noodle of length exactly B_i and eat it. If no such noodle is available on any day, his plan fails. Additionally, he cannot eat the same noodle on multiple days.

Can Takahashi accomplish his meal plan?

Constraints

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

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

If Takahashi can accomplish his meal plan, print Yes; otherwise, print No.


Sample Input 1

3 2
1 1 3
3 1

Sample Output 1

Yes

He can eat the 3-rd noodle on the 1-st day and the 1-st noodle on the 2-nd day, so his meal plan is feasible.


Sample Input 2

1 1
1000000000
1

Sample Output 2

No

A noodle of length exactly 1 is needed.


Sample Input 3

5 2
1 2 3 4 5
5 5

Sample Output 3

No

Since there are only 1 noodle of length 5, he cannot have a meal on the 2-nd day.

C - Connect 6

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 300

問題文

NN 列のマス目があり、それぞれのマスは白または黒で塗られています。
マス目の状態は N 個の文字列 S_i で表され、 S_ij 文字目が # であることはマス目の上から i 行目、左から j 列目のマスが黒く塗られていることを、 . であることは白く塗られていることをさします。

高橋君はこのマス目のうち高々 2 つの白く塗られているマスを選び、黒く塗ることができます。
マス目の中に、黒く塗られたマスが縦、横、ななめのいずれかの向きに 6 つ以上連続するようにできるか判定してください。
ただし、黒く塗られたマスがななめに 6 つ以上連続するとは、NN 列のマス目に完全に含まれる 66 列のマス目であって、その少なくとも一方の対角線上のマスがすべて黒く塗られているようなものが存在する事をさします。

制約

  • 6 \leq N \leq 1000
  • \lvert S_i\rvert =N
  • S_i#. のみからなる。

入力

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

N
S_1
S_2
\vdots
S_N

出力

高々 2 つのマス目を黒く塗ることで条件をみたすようにできるなら Yes を、そうでないならば No を出力せよ。


入力例 1

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

出力例 1

Yes

上から 3 行目の左から 3, 6 番目のマスを塗ることで横方向に 6 つの黒く塗られたマスを連続させることができます。


入力例 2

6
######
######
######
######
######
######

出力例 2

Yes

高橋君はマス目を新たに黒く塗ることはできませんが、すでにこのマス目は条件をみたしています。


入力例 3

10
..........
#..##.....
..........
..........
....#.....
....#.....
.#...#..#.
..........
..........
..........

出力例 3

No

Score : 300 points

Problem Statement

There is a grid with N horizontal rows and N vertical columns, where each square is painted white or black.
The state of the grid is represented by N strings, S_i. If the j-th character of S_i is #, the square at the i-th row from the top and the j-th column from the left is painted black. If the character is ., then the square is painted white.

Takahashi can choose at most two of these squares that are painted white, and paint them black.
Determine if it is possible to make the grid contain 6 or more consecutive squares painted black aligned either vertically, horizontally, or diagonally.
Here, the grid is said to be containing 6 or more consecutive squares painted black aligned diagonally if the grid with N rows and N columns completely contains a subgrid with 6 rows and 6 columns such that all the squares on at least one of its diagonals are painted black.

Constraints

  • 6 \leq N \leq 1000
  • \lvert S_i\rvert =N
  • S_i consists of # and ..

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

If it is possible to fulfill the condition by painting at most two squares, then print Yes; otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

By painting the 3-rd and the 6-th squares from the left in the 3-rd row from the top, there will be 6 squares painted black aligned horizontally.


Sample Input 2

6
######
######
######
######
######
######

Sample Output 2

Yes

While Takahashi cannot choose a square to paint black, the grid already satisfies the condition.


Sample Input 3

10
..........
#..##.....
..........
..........
....#.....
....#.....
.#...#..#.
..........
..........
..........

Sample Output 3

No
D - Sequence Query

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

空の数列 A があります。
クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 xAx を追加する。

  • 2 x kAx 以下の要素のうち、大きい方から k 番目の値を出力する。(k\bf{5} 以下)
    ただし、Ax 以下の要素が k 個以上存在しないときは -1 と出力する。

  • 3 x kAx 以上の要素のうち、小さい方から k 番目の値を出力する。(k\bf{5} 以下)
    ただし、Ax 以上の要素が k 個以上存在しないときは -1 と出力する。

制約

  • 1\leq Q \leq 2\times 10^5
  • 1\leq x\leq 10^{18}
  • 1\leq k\leq 5
  • 入力は全て整数である

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

i 番目のクエリ \text{query}_i では、まずクエリの種類 c_i (1,2,3 のいずれか) が与えられる。
c_i=1 の場合は x が追加で与えられ、c_i=2,3 の場合は x,k が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x
2 x k
3 x k

出力

c_i=2,3 を満たすクエリの個数を q として q 行出力せよ。
j(1\leq j\leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

11
1 20
1 10
1 30
1 20
3 15 1
3 15 2
3 15 3
3 15 4
2 100 5
1 1
2 100 5

出力例 1

20
20
30
-1
-1
1

\text{query}_{1,2,3,4} が終了した段階で、A=(20,10,30,20) となっています。

\text{query}_{5,6,7} について、A15 以上の要素は (20,30,20) です。
このうち小さい方から 1 番目の値は 202 番目の値は 203 番目の値は 30 です。

Score : 400 points

Problem Statement

We have an empty sequence A.
Given Q queries, process them in order.
Each query is of one of the following three types.

  • 1 x : Insert x to A.

  • 2 x k : Among the elements of A that are less than or equal to x, print the k-th largest value. (k is no more than \bf{5})
    If there are less than k elements of A that are less than or equal to x, then print -1.

  • 3 x k : Among the elements of A that are greater than or equal to x, print the k-th smallest value. (k is no more than \bf{5})
    If there are less than k elements of A that are greater than or equal to x, then print -1.

Constraints

  • 1\leq Q \leq 2\times 10^5
  • 1\leq x\leq 10^{18}
  • 1\leq k\leq 5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

In the i-th query \text{query}_i, the type of query c_i (which is either 1, 2, or 3) is given first.
If c_i=1, then x is additionally given; if c_i=2, 3, then x and k are additionally given.

In other words, each query is given in one of the following three formats:

1 x
2 x k
3 x k

Output

Print q lines, where q is the number of queries such that c_i=2,3.
The j-th line (1\leq j\leq q) should contain the answer for the j-th such query.


Sample Input 1

11
1 20
1 10
1 30
1 20
3 15 1
3 15 2
3 15 3
3 15 4
2 100 5
1 1
2 100 5

Sample Output 1

20
20
30
-1
-1
1

After \text{query}_{1,2,3,4} have been processed, we have A=(20,10,30,20).

For \text{query}_{5,6,7}, the elements of A greater than or equal to 15 are (20,30,20).
The 1-st smallest value of them is 20; the 2-nd is 20; the 3-rd is 30.

E - Putting Candies

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の数列 A=(A_0,A_1,\ldots,A_{N-1}) が与えられます。
最初の時点では空の皿があり、高橋君は次の操作を K 回繰り返します。

  • 皿の中のアメの個数を X とする。皿に A_{(X\bmod N)} 個のアメを追加する。 ただし、X\bmod NXN で割った余りを表す。

K 回の操作の後で、皿の中には何個のアメがあるか求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq K \leq 10^{12}
  • 1 \leq A_i\leq 10^6
  • 入力はすべて整数である。

入力

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

N K
A_0 A_1 \ldots A_{N-1}

出力

答えを出力せよ。


入力例 1

5 3
2 1 6 3 1

出力例 1

11

皿の中のアメの個数は次のように変化します。

  • 1 回目の操作において、X=0 であるので、アメは A_{(0\mod 5)}=A_0=2 個追加されます。
  • 2 回目の操作において、X=2 であるので、アメは A_{(2\mod 5)}=A_2=6 個追加されます。
  • 3 回目の操作において、X=8 であるので、アメは A_{(8\mod 5)}=A_3=3 個追加されます。

よって、3 回の操作の後で、皿には 11 個のアメがあります。出力する値は N で割った余りではない事に注意してください。


入力例 2

10 1000000000000
260522 914575 436426 979445 648772 690081 933447 190629 703497 47202

出力例 2

826617499998784056

答えは 32 bit 整数型に収まらない場合があります。

Score : 500 points

Problem Statement

You are given a sequence A=(A_0,A_1,\ldots,A_{N-1}) of length N.
There is an initially empty dish. Takahashi is going to repeat the following operation K times.

  • Let X be the number of candies on the dish. He puts A_{(X\bmod N)} more candies on the dish. Here, X\bmod N denotes the remainder when X is divided by N.

Find how many candies are on the dish after the K operations.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq K \leq 10^{12}
  • 1 \leq A_i\leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 \ldots A_{N-1}

Output

Print the answer.


Sample Input 1

5 3
2 1 6 3 1

Sample Output 1

11

The number of candies on the dish transitions as follows.

  • In the 1-st operation, we have X=0, so A_{(0\mod 5)}=A_0=2 more candies will be put on the dish.
  • In the 2-nd operation, we have X=2, so A_{(2\mod 5)}=A_2=6 more candies will be put on the dish.
  • In the 3-rd operation, we have X=8, so A_{(8\mod 5)}=A_3=3 more candies will be put on the dish.

Thus, after the 3 operations, there will be 11 candies on the dish. Note that you must not print the remainder divided by N.


Sample Input 2

10 1000000000000
260522 914575 436426 979445 648772 690081 933447 190629 703497 47202

Sample Output 2

826617499998784056

The answer may not fit into a 32-bit integer type.

F - Skate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

HW 列のグリッド型のスケート場があります。上から i 行目、左から j 行目のマスを (i,j) で表します。

スケート場には N 個の障害物があり、i 個目の障害物は (X_i,Y_i) に置かれています。

高橋君は 1 回の移動において、上下左右いずれかの方向を選んで、障害物に当たるまで進み続けます。
障害物に当たるとその 1 つ手前のマスで停止します。 なお、スケート場の周りは崖になっており、障害物に当たらないような移動は禁止とします。

高橋君ははじめ (s_x,s_y) にいて、何回か移動することで (g_x,g_y) で停止したいと考えています。

(g_x,g_y) へ辿り着くには最小で何回の移動が必要ですか?辿り着けないときはその旨を伝えてください。

制約

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • i\neq j ならば、(X_i,Y_i)\neq (X_j,Y_j)
  • 入力は全て整数である

入力

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

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

(g_x,g_y) へ辿り着くには最小で何回の移動が必要か出力せよ。
ただし、辿り着けないならば -1 と出力せよ。


入力例 1

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

出力例 1

4

図は、(s_x,s_y)S で、(g_x,g_y)G で表しています。
(3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6) と移動すると、4 回の移動で (g_x,g_y) に辿り着くことができます。


入力例 2

4 6 2
3 2
3 5
4 5
2 5

出力例 2

-1

(g_x,g_y) で停止する必要があります。
通過しただけでは (g_x,g_y) へ辿り着いたとみなされないことに注意してください。


入力例 3

1 10 1
1 5
1 1
1 7

出力例 3

-1


左を選んで進むと、高橋君は (g_x,g_y) を通過したのちに崖に落ちてしまいます。
スケート場の周りは崖になっており、障害物に当たらないような移動は禁止されていることに注意してください。

Score : 500 points

Problem Statement

There is a skating rink represented by 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.

The skating rink has N obstacles. The i-th obstacle is placed at (X_i,Y_i).

In a single move, Takahashi chooses one of the four directions, up, down, left, or right, and keeps moving until he hits an obstacle.
When he hits an obstacle, he stops at the square right before the obstacle. Since the skating rink is surrounded by cliffs, it is prohibited to start a move in which he will never hit an obstacle.

Takahashi is initially at (s_x,s_y). He wants to make some number of moves to stop at (g_x,g_y).

Find the minimum number of moves required to end up at (g_x, g_y). If it is not possible, report the fact.

Constraints

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • If i\neq j, then (X_i,Y_i)\neq (X_j,Y_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the minimum number of moves required to end up at (g_x,g_y).
If it is impossible to end up there, print -1.


Sample Input 1

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

Sample Output 1

4

In the figure, (s_x,s_y) is denoted by S and (g_x,g_y) is denoted by G.
By moving as (3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6), he can end up at (g_x,g_y) with 4 moves.


Sample Input 2

4 6 2
3 2
3 5
4 5
2 5

Sample Output 2

-1

He must stop at (g_x,g_y).
Note that just passing through (g_x,g_y) is not considered to be ending up at the goal.


Sample Input 3

1 10 1
1 5
1 1
1 7

Sample Output 3

-1


If he chooses to move to the left, Takahashi will fall down the cliff after passing through (g_x,g_y).
Note that it is prohibited to start a move in which he will never hit an obstacle, as the skating rink is surrounded by cliffs.

G - Round Robin

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N までの番号がついた N 人が総当たり戦をしています。
すなわち、全ての組 (i,j) (1\leq i \lt j \leq N) について、人 i と人 j1 回試合をするので、試合は合計で \frac{N(N-1)}{2} 試合行われます。
なお、試合は必ず一方が勝者、もう一方が敗者となり、引き分けとなることはありません。

既に M 試合が終了しており、i 試合目では人 W_i が人 L_i に勝ちました。

総当たり戦が終了したとき、単独優勝をする可能性のある人を列挙してください。
ただし単独優勝とは、その人の勝利数が、他のどの人の勝利数よりも多いことを言います。

制約

  • 2\leq N \leq 50
  • 0\leq M \leq \frac{N(N-1)}{2}
  • 1\leq W_i,L_i\leq N
  • W_i \neq L_i
  • i\neq j ならば、(W_i,L_i) \neq (W_j,L_j)
  • (W_i,L_i) \neq (L_j,W_j)
  • 入力は全て整数である

入力

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

N M
W_1 L_1
W_2 L_2
\vdots
W_M L_M

出力

単独優勝をする可能性のある人の番号の集合を A=(A_1,A_2,\dots,A_K) (A_1\lt A_2 \lt \dots \lt A_K) として、A を昇順に空白区切りで出力せよ。
すなわち、以下の形式で出力せよ。

A_1 A_2 \dots A_K

入力例 1

4 2
2 1
2 3

出力例 1

2 4

2,4 は単独優勝する可能性があり、人 1,3 は単独優勝する可能性がありません。
なお、4 2 などの出力は不正解となることに注意してください。


入力例 2

3 3
1 2
2 3
3 1

出力例 2


単独優勝する可能性のある人がいないこともあります。


入力例 3

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

出力例 3

1 3 6 7

Score : 600 points

Problem Statement

N players numbered 1 through N will participate in a round-robin tournament.
Specifically, for every pair (i,j) (1\leq i \lt j \leq N), Player i and Player j play a match against each other once, for a total of \frac{N(N-1)}{2} matches.
In every match, one of the players will be a winner and the other will be a loser; there is no draw.

M matches have already ended. In the i-th match, Player W_i won Player L_i.

List all the players who may become the unique winner after the round-robin tournament is completed.
A player is said to be the unique winner if the number of the player's wins is strictly greater than that of any other player.

Constraints

  • 2\leq N \leq 50
  • 0\leq M \leq \frac{N(N-1)}{2}
  • 1\leq W_i,L_i\leq N
  • W_i \neq L_i
  • If i\neq j, then (W_i,L_i) \neq (W_j,L_j).
  • (W_i,L_i) \neq (L_j,W_j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
W_1 L_1
W_2 L_2
\vdots
W_M L_M

Output

Let A=(A_1,A_2,\dots,A_K) (A_1\lt A_2 \lt \dots \lt A_K) be the set of indices of players that may become the unique winner. Print A in the increasing order, with spaces in between.
In other words, print in the following format.

A_1 A_2 \dots A_K

Sample Input 1

4 2
2 1
2 3

Sample Output 1

2 4

Players 2 and 4 may become the unique winner, while Players 1 and 3 cannot.
Note that output like 4 2 is considered to be incorrect.


Sample Input 2

3 3
1 2
2 3
3 1

Sample Output 2


It is possible that no player can become the unique winner.


Sample Input 3

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

Sample Output 3

1 3 6 7
Ex - Card Deck Score

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 個の整数のうちいずれか 1 つが書かれたカードが何枚かあり、 具体的には、A_i が書かれたカードが B_i 枚あります。
次に、この B_1+B_2\cdots +B_N 枚の中から M 枚のカードを選ぶ方法について、 その選んだカードに書かれた M 個の整数の積をその選び方のスコアとして定めます。
同じ整数が書かれたカードは区別できないとしたとき、M 枚の選び方としてあり得る すべての選び方についてスコアを足し合わせた値を 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 16
  • 1 \leq M \leq 10^{18}
  • 1 \leq A_i < 998244353
  • 1 \leq B_i \leq 10^{17}
  • i\neq j ならば A_i \neq A_j
  • M\leq B_1+B_2+\cdots B_N
  • 入力は全て整数である。

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

3 3
3 1
5 2
6 3

出力例 1

819

3 枚を選ぶ方法としては次の 6 通りが考えられます。

  • 3 と書かれたカードを 1 枚、5 と書かれたカードを 2 枚選ぶ。
  • 3 と書かれたカードを 1 枚、5 と書かれたカードを 1 枚、6 と書かれたカードを 1 枚選ぶ。
  • 3 と書かれたカードを 1 枚、6 と書かれたカードを 2 枚選ぶ。
  • 5 と書かれたカードを 2 枚、6 と書かれたカードを 1 枚選ぶ。
  • 5 と書かれたカードを 1 枚、6 と書かれたカードを 2 枚選ぶ。
  • 6 と書かれたカードを 3 枚選ぶ。

スコアは順に 75, 90, 108, 150, 180, 216 であり、これらの総和は 819 となります。


入力例 2

3 2
1 1
5 2
25 1

出力例 2

180

125 の書かれたカードを 1 枚ずつ選ぶ」選び方と 「 5 の書かれたカードを 2 枚選ぶ」選び方は、いずれもスコアは 25 ですが、 カードの選び方としては異なるものとして数えられます。


入力例 3

10 232657150901347497
139547946 28316250877914575
682142538 78223540024979445
110643588 74859962623690081
173455495 60713016476190629
271056265 85335723211047202
801329567 48049062628894325
864844366 54979173822804784
338794337 69587449430302156
737638908 15812229161735902
462149872 49993004923078537

出力例 3

39761306

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

Score : 600 points

Problem Statement

There are some cards. Each card has one of N integers written on it. Specifically, there are B_i cards with A_i written on them.
Next, for a combination of M cards chosen out of these (B_1+B_2\cdots +B_N) cards, we define the score of the combination by the product of the integers written on the M cards.
Supposed that cards with the same integer written on them are indistinguishable, find the sum, modulo 998244353, of the scores over all possible combinations of M cards.

Constraints

  • 1 \leq N \leq 16
  • 1 \leq M \leq 10^{18}
  • 1 \leq A_i < 998244353
  • 1 \leq B_i \leq 10^{17}
  • If i\neq j, then A_i \neq A_j.
  • M\leq B_1+B_2+\cdots B_N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer.


Sample Input 1

3 3
3 1
5 2
6 3

Sample Output 1

819

There are 6 possible combinations of 3 cards.

  • A combination of 1 card with 3 written on it, and 2 cards with 5 written on them.
  • A combination of 1 card with 3 written on it, 1 card with 5 written on it, and 1 card with 6 written on it.
  • A combination of 1 card with 3 written on it, and 2 cards with 6 written on them.
  • A combination of 2 cards with 5 written on them, and 1 card with 6 written on it.
  • A combination of 1 card with 5 written on it, and 2 cards with 6 written on them.
  • A combination of 3 cards with 6 written on them.

The scores are 75, 90, 108, 150, 180, and 216, respectively, for a sum of 819.


Sample Input 2

3 2
1 1
5 2
25 1

Sample Output 2

180

"A combination of a card with 1 and another card with 25" and "a combination of two cards with 5 written on them" have the same score of 25, but they are considered to be different combinations.


Sample Input 3

10 232657150901347497
139547946 28316250877914575
682142538 78223540024979445
110643588 74859962623690081
173455495 60713016476190629
271056265 85335723211047202
801329567 48049062628894325
864844366 54979173822804784
338794337 69587449430302156
737638908 15812229161735902
462149872 49993004923078537

Sample Output 3

39761306

Be sure to print the answer modulo 998244353.