A - Order Something Else

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君は、レストランで「AtCoder ドリンク」というドリンクを飲もうとしています。 AtCoder ドリンクは定価である P 円を払えば飲むことができます。

また、高橋君は割引券を持っており、それを使うと AtCoder ドリンクを定価より安い価格である Q 円で飲むことができますが、 その場合には AtCoder ドリンクの他に、N 品ある料理の中から 1 つを追加で注文しなければなりません。 i = 1, 2, \ldots, N について、i 番目の料理の価格は D_i 円です。

高橋君がドリンクを飲むため支払う合計金額の最小値を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \lt P \leq 10^5
  • 1 \leq D_i \leq 10^5
  • 入力はすべて整数

入力

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

N P Q
D_1 D_2 \ldots D_N

出力

答えを出力せよ。


入力例 1

3 100 50
60 20 40

出力例 1

70

割引券を使用して 2 番目の料理を注文することで、ドリンク代 50 円と料理代 20 円の合計 70 円の支払いで AtCoder ドリンクを飲むことができ、支払う合計金額が最小となります。


入力例 2

3 100 50
60000 20000 40000

出力例 2

100

割引券を使用せず定価の 100 円で AtCoder ドリンクを飲むことで、支払う合計金額が最小となります。

Score : 100 points

Problem Statement

Takahashi wants a beverage called AtCoder Drink in a restaurant. It can be ordered at a regular price of P yen.

He also has a discount coupon that allows him to order it at a lower price of Q yen. However, he must additionally order one of the restaurant's N dishes to use that coupon. For each i = 1, 2, \ldots, N, the price of the i-th dish is D_i yen.

Print the minimum total amount of money that he must pay to get the drink.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \lt P \leq 10^5
  • 1 \leq D_i \leq 10^5
  • All input values are integers.

Input

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

N P Q
D_1 D_2 \ldots D_N

Output

Print the answer.


Sample Input 1

3 100 50
60 20 40

Sample Output 1

70

If he uses the coupon and orders the second dish, he can get the drink by paying 50 yen for it and 20 yen for the dish, for a total of 70 yen, which is the minimum total payment needed.


Sample Input 2

3 100 50
60000 20000 40000

Sample Output 2

100

The total payment will be minimized by not using the coupon and paying the regular price of 100 yen.

B - Strictly Superior

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

AtCoder 商店には N 個の商品があります。 i\ (1\leq i\leq N) 番目の商品の価格は P _ i です。 i\ (1\leq i\leq N) 番目の商品は C _ i 個の機能をもち、i\ (1\leq i\leq N) 番目の商品の j\ (1\leq j\leq C _ i) 番目の機能は 1 以上 M 以下の整数 F _ {i,j} として表されます。

高橋くんは、AtCoder 商店の商品で一方が一方の上位互換であるものがないか気になりました。 i 番目の商品と j 番目の商品 (1\leq i,j\leq N) であって、次の条件をすべて満たすものがあるとき Yes と、ないとき No と出力してください。

  • P _ i\geq P _ j である。
  • j 番目の製品は i 番目の製品がもつ機能をすべてもつ。
  • P _ i\gt P _ j であるか、j 番目の製品は i 番目の製品にない機能を 1 つ以上もつ。

制約

  • 2\leq N\leq100
  • 1\leq M\leq100
  • 1\leq P _ i\leq10^5\ (1\leq i\leq N)
  • 1\leq C _ i\leq M\ (1\leq i\leq N)
  • 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N M
P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1}
P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2}
\vdots
P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}

出力

答えを 1 行で出力せよ。


入力例 1

5 6
10000 2 1 3
15000 3 1 2 4
30000 3 1 3 5
35000 2 1 5
100000 6 1 2 3 4 5 6

出力例 1

Yes

(i,j)=(4,3) とすると、条件を全て満たします。

他の組は条件を満たしません。例えば (i,j)=(4,5) とすると j 番目の商品は i 番目の商品の機能をすべてもっていますが、P _ i\lt P _ j なので上位互換ではありません。


入力例 2

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

出力例 2

No

まったく同じ価格と機能をもった商品がある場合もあります。


入力例 3

20 10
72036 3 3 4 9
7716 4 1 2 3 6
54093 5 1 6 7 8 10
25517 7 3 4 5 6 7 9 10
96930 8 2 3 4 6 7 8 9 10
47774 6 2 4 5 6 7 9
36959 5 1 3 4 5 8
46622 7 1 2 3 5 6 8 10
34315 9 1 3 4 5 6 7 8 9 10
54129 7 1 3 4 6 7 8 9
4274 5 2 4 7 9 10
16578 5 2 3 6 7 9
61809 4 1 2 4 5
1659 5 3 5 6 9 10
59183 5 1 2 3 4 9
22186 4 3 5 6 8
98282 4 1 4 7 10
72865 8 1 2 3 4 6 8 9 10
33796 6 1 3 5 7 9 10
74670 4 1 2 6 8

出力例 3

Yes

Score : 200 points

Problem Statement

AtCoder Shop has N products. The price of the i-th product (1\leq i\leq N) is P _ i. The i-th product (1\leq i\leq N) has C_i functions. The j-th function (1\leq j\leq C _ i) of the i-th product (1\leq i\leq N) is represented as an integer F _ {i,j} between 1 and M, inclusive.

Takahashi wonders whether there is a product that is strictly superior to another. If there are i and j (1\leq i,j\leq N) such that the i-th and j-th products satisfy all of the following conditions, print Yes; otherwise, print No.

  • P _ i\geq P _ j.
  • The j-th product has all functions of the i-th product.
  • P _ i\gt P _ j, or the j-th product has one or more functions that the i-th product lacks.

Constraints

  • 2\leq N\leq100
  • 1\leq M\leq100
  • 1\leq P _ i\leq10^5\ (1\leq i\leq N)
  • 1\leq C _ i\leq M\ (1\leq i\leq N)
  • 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N M
P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1}
P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2}
\vdots
P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}

Output

Print the answer in a single line.


Sample Input 1

5 6
10000 2 1 3
15000 3 1 2 4
30000 3 1 3 5
35000 2 1 5
100000 6 1 2 3 4 5 6

Sample Output 1

Yes

(i,j)=(4,3) satisfies all of the conditions.

No other pair satisfies them. For instance, for (i,j)=(4,5), the j-th product has all functions of the i-th one, but P _ i\lt P _ j, so it is not strictly superior.


Sample Input 2

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

Sample Output 2

No

Multiple products may have the same price and functions.


Sample Input 3

20 10
72036 3 3 4 9
7716 4 1 2 3 6
54093 5 1 6 7 8 10
25517 7 3 4 5 6 7 9 10
96930 8 2 3 4 6 7 8 9 10
47774 6 2 4 5 6 7 9
36959 5 1 3 4 5 8
46622 7 1 2 3 5 6 8 10
34315 9 1 3 4 5 6 7 8 9 10
54129 7 1 3 4 6 7 8 9
4274 5 2 4 7 9 10
16578 5 2 3 6 7 9
61809 4 1 2 4 5
1659 5 3 5 6 9 10
59183 5 1 2 3 4 9
22186 4 3 5 6 8
98282 4 1 4 7 10
72865 8 1 2 3 4 6 8 9 10
33796 6 1 3 5 7 9 10
74670 4 1 2 6 8

Sample Output 3

Yes
C - Reversible

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。

i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。

2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_iS_j と一致するか、S_iS_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。

N 本の棒の中に、何種類の異なる棒があるかを出力してください。

制約

  • N は整数
  • 2 \leq N \leq 2 \times 10^5
  • S_i は英小文字のみからなる文字列
  • |S_i| \geq 1
  • \sum_{i = 1}^N |S_i| \leq 2 \times 10^5

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

6
a
abc
de
cba
de
abc

出力例 1

3
  • S_2 = abcS_4 = cba を前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。
  • S_2 = abcS_6 = abc と一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。
  • S_3 = deS_5 = de と一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。

よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。

Score : 300 points

Problem Statement

There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.

For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.

Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.

Print the number of different sticks among the N sticks.

Constraints

  • N is an integer.
  • 2 \leq N \leq 2 \times 10^5
  • S_i is a string consisting of lowercase English letters.
  • |S_i| \geq 1
  • \sum_{i = 1}^N |S_i| \leq 2 \times 10^5

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

6
a
abc
de
cba
de
abc

Sample Output 1

3
  • S_2 = abc equals the reversal of S_4 = cba, so the second and fourth sticks are considered the same.
  • S_2 = abc equals S_6 = abc, so the second and sixth sticks are considered the same.
  • S_3 = de equals S_5 = de, so the third and fifth sticks are considered the same.

Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).

D - Peaceful Teams

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

N 人のスポーツ選手がいます。

N 人の選手たちには互いに相性の悪い選手のペアが M 組あり、相性の悪い組のうち i\ (1\leq i\leq M) 組目は A _ i 番目の選手と B _ i 番目の選手です。

あなたは、選手を T チームに分けます。 どの選手もちょうど一つのチームに属さなければならず、どのチームにも少なくとも一人の選手が属さなければなりません。 さらに、どの i=1,2,\ldots,M についても、 A _ i 番目の選手と B _ i 番目の選手が同じチームに属していてはいけません。

この条件を満たすチーム分けの方法は何通りあるか求めてください。 ただし、チーム分けの方法が異なるとは、ある二人が存在して、彼らが一方のチーム分けでは同じチームに所属し、もう一方では異なるチームに所属することをいいます。

制約

  • 1\leq T\leq N\leq10
  • 0\leq M\leq\dfrac{N(N-1)}2
  • 1\leq A _ i\lt B _ i\leq N\ (1\leq i\leq M)
  • (A _ i,B _ i)\neq (A _ j,B _ j)\ (1\leq i\lt j\leq M)
  • 入力はすべて整数

入力

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

N T M
A _ 1 B _ 1
A _ 2 B _ 2
\vdots
A _ M B _ M

出力

答えを 1 行で出力せよ。


入力例 1

5 2 2
1 3
3 4

出力例 1

4

次の 4 通りのチーム分けが条件を満たします。

他に条件を満たすチーム分けは存在しないので、4 を出力してください。


入力例 2

5 1 2
1 3
3 4

出力例 2

0

条件を満たすチーム分けがひとつも存在しないこともあります。


入力例 3

6 4 0

出力例 3

65

相性の悪いペアがひとつも存在しないこともあります。


入力例 4

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

出力例 4

8001

Score : 400 points

Problem Statement

There are N sports players.

Among them, there are M incompatible pairs. The i-th incompatible pair (1\leq i\leq M) is the A_i-th and B_i-th players.

You will divide the players into T teams. Every player must belong to exactly one team, and every team must have one or more players. Additionally, for each i=1,2,\ldots,M, the A_i-th and B_i-th players must not belong to the same team.

Find the number of ways to satisfy these conditions. Here, two divisions are considered different when there are two players who belong to the same team in one division and different teams in the other.

Constraints

  • 1\leq T\leq N\leq10
  • 0\leq M\leq\dfrac{N(N-1)}2
  • 1\leq A _ i\lt B _ i\leq N\ (1\leq i\leq M)
  • (A _ i,B _ i)\neq (A _ j,B _ j)\ (1\leq i\lt j\leq M)
  • All input values are integers.

Input

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

N T M
A _ 1 B _ 1
A _ 2 B _ 2
\vdots
A _ M B _ M

Output

Print the answer in a single line.


Sample Input 1

5 2 2
1 3
3 4

Sample Output 1

4

The following four divisions satisfy the conditions.

No other division satisfies them, so print 4.


Sample Input 2

5 1 2
1 3
3 4

Sample Output 2

0

There may be no division that satisfies the conditions.


Sample Input 3

6 4 0

Sample Output 3

65

There may be no incompatible pair.


Sample Input 4

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

Sample Output 4

8001
E - NAND repeatedly

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 450

問題文

01 からなる長さ N の文字列 S が与えられます。 S は長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) の情報を表しており、Si 文字目 (1\leq i\leq N)0 のとき A _ i=01 のとき A _ i=1です。

\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]

を求めてください。

より厳密には、次のように定められる f(i,j)\ (1\leq i\leq j\leq N) に対して \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) を求めてください。

\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]

ただし、否定論理積 \barwedge は次を満たす二項演算子です。

\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0\]

制約

  • 1\leq N\leq10^6
  • S01 からなる長さ N の文字列
  • 入力はすべて整数

入力

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

N
S

出力

答えを 1 行で出力せよ。


入力例 1

5
00110

出力例 1

9

1\leq i\leq j\leq N を満たすすべての組 (i,j) に対して、f(i,j) の値は以下のようになります。

  • f(1,1)=0=0
  • f(1,2)=0\barwedge0=1
  • f(1,3)=(0\barwedge0)\barwedge1=0
  • f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
  • f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
  • f(2,2)=0=0
  • f(2,3)=0\barwedge1=1
  • f(2,4)=(0\barwedge1)\barwedge1=0
  • f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
  • f(3,3)=1=1
  • f(3,4)=1\barwedge1=0
  • f(3,5)=(1\barwedge1)\barwedge0=1
  • f(4,4)=1=1
  • f(4,5)=1\barwedge0=1
  • f(5,5)=0=0

これらの総和は 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9 なので、9 を出力してください。

\barwedge は結合法則を満たさないことに注意してください。 例えば、(1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0) です。


入力例 2

30
101010000100101011010011000010

出力例 2

326

Score : 450 points

Problem Statement

You are given a string S of length N consisting of 0 and 1. It describes a length-N sequence A=(A _ 1,A _ 2,\ldots,A _ N). If the i-th character of S (1\leq i\leq N) is 0, then A _ i=0; if it is 1, then A _ i=1.

Find the following:

\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]

More formally, find \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) for f(i,j)\ (1\leq i\leq j\leq N) defined as follows:

\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]

Here, \barwedge, NAND, is a binary operator satisfying the following:

\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0.\]

Constraints

  • 1\leq N\leq10^6
  • S is a string of length N consisting of 0 and 1.
  • All input values are integers.

Input

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

N
S

Output

Print the answer in a single line.


Sample Input 1

5
00110

Sample Output 1

9

Here are the values of f(i,j) for the pairs (i,j) such that 1\leq i\leq j\leq N:

  • f(1,1)=0=0
  • f(1,2)=0\barwedge0=1
  • f(1,3)=(0\barwedge0)\barwedge1=0
  • f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
  • f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
  • f(2,2)=0=0
  • f(2,3)=0\barwedge1=1
  • f(2,4)=(0\barwedge1)\barwedge1=0
  • f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
  • f(3,3)=1=1
  • f(3,4)=1\barwedge1=0
  • f(3,5)=(1\barwedge1)\barwedge0=1
  • f(4,4)=1=1
  • f(4,5)=1\barwedge0=1
  • f(5,5)=0=0

Their sum is 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9, so print 9.

Note that \barwedge does not satisfy the associative property. For instance, (1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0).


Sample Input 2

30
101010000100101011010011000010

Sample Output 2

326
F - Make 10 Again

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個のサイコロがあります。 i = 1, 2, \ldots, N について、i 番目のサイコロを振ると 1 以上 A_i 以下の整数の出目がそれぞれ等確率でランダムにでます。

N 個のサイコロすべてを同時に振るとき、その結果が下記の条件を満たす確率を \text{mod } 998244353 で求めてください。

N 個のサイコロの中からいくつか( N 個全部でも良い)を選ぶ方法であって、選んだサイコロの出目の和がちょうど 10 であるようなものが存在する。

確率 \text{mod } 998244353 の定義

この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^6
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
1 7 2 9

出力例 1

942786334

例えば、1, 2, 3, 4 個目のサイコロの出目が順に 1, 3, 2, 7 だった場合、この結果は問題文中の条件を満たします。 実際、2, 4 番目のサイコロを選択すると、選んだサイコロの出目の和が 3 + 7 = 10 になります。 また、1, 3, 4 番目のサイコロを選択しても、選んだサイコロの出目の和が 1 + 2 + 7 = 10 になります。

一方、1, 2, 3, 4 個目のサイコロの出目が順に 1, 6, 1, 5 だった場合、 どのようにサイコロを選んでも選んだサイコロの出目の和が 10 にならないため、この結果は問題文中の条件を満たしません。

この入力例では、N 個のサイコロを振った結果が問題文中の条件を満たす確率は \frac{11}{18} です。 よって、その \text{mod } 998244353 における値である 942786334 を出力します。


入力例 2

7
1 10 100 1000 10000 100000 1000000

出力例 2

996117877

Score : 500 points

Problem Statement

We have N dice. For each i = 1, 2, \ldots, N, when the i-th die is thrown, it shows a random integer between 1 and A_i, inclusive, with equal probability.

Find the probability, modulo 998244353, that the following condition is satisfied when the N dice are thrown simultaneously.

There is a way to choose some (possibly all) of the N dice so that the sum of their results is 10.

How to find a probability modulo 998244353

It can be proved that the sought probability is always a rational number. Additionally, the constraints of this problem guarantee that if the sought probability is represented as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.

Here, there is a unique integer z such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^6
  • All input values 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

4
1 7 2 9

Sample Output 1

942786334

For instance, if the first, second, third, and fourth dice show 1, 3, 2, and 7, respectively, these results satisfy the condition. In fact, if the second and fourth dice are chosen, the sum of their results is 3 + 7 = 10. Alternatively, if the first, third, and fourth dice are chosen, the sum of their results is 1 + 2 + 7 = 10.

On the other hand, if the first, second, third, and fourth dice show 1, 6, 1, and 5, respectively, there is no way to choose some of them so that the sum of their results is 10, so the condition is not satisfied.

In this sample input, the probability of the results of the N dice satisfying the condition is \frac{11}{18}. Thus, print this value modulo 998244353, that is, 942786334.


Sample Input 2

7
1 10 100 1000 10000 100000 1000000

Sample Output 2

996117877
G - Takahashi And Pass-The-Ball Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 550

問題文

高橋くんが N 人います。

i 番目の高橋くんは、整数 A _ i とボール B _ i 個を持っています。

高橋くんたちは 1 以上 K 以下の整数 x を一様ランダムに選び、次の操作を x 回繰り返します。

  • すべての i について、i 番目の高橋くんは A _ i 番目の高橋くんに自分が持っているボールをすべて渡す。

操作は N 人によって同時に行われることに注意してください。

i=1,2,\ldots,N について、一連の操作が終了したとき i 番目の高橋くんが持っているボールの個数の期待値を {}\bmod{998244353} で求めてください。

期待値 {}\bmod{998244353} の定義 この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac yx で表したときに x998244353 で割り切れないことが保証されます。 このとき、y\equiv xz\pmod{998244353} を満たす 0\leq z\lt998244353 がただ一つ存在するので、z を出力してください。

制約

  • 1\leq N\leq 2\times10^5
  • 1\leq K\leq 10^{18}
  • K998244353 の倍数でない
  • 1\leq A _ i\leq N\ (1\leq i\leq N)
  • 0\leq B _ i\lt998244353\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N K
A _ 1 A _ 2 \cdots A _ N
B _ 1 B _ 2 \cdots B _ N

出力

i=1,2,\ldots,N について、操作が終了したとき i 番目の高橋くんが持っているボールの個数の期待値を空白区切りで 1 行に出力せよ。


入力例 1

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

出力例 1

3 0 499122179 499122178 5

操作を 2 回行うと、それぞれの高橋くんが持っているボールは以下のようになります。

x=1 が選ばれた場合、それぞれの高橋くんが持っているボールの数は 4,0,1,2,5 個です。
x=2 が選ばれた場合、それぞれの高橋くんが持っているボールの数は 2,0,4,1,5 個です。

よって、求める期待値はそれぞれ 3,0,\dfrac52,\dfrac32,5 です。 {}\bmod{998244353} での値を求めるとそれぞれ 3,0,499122179,499122178,5 となるので、これらを順に空白区切りで出力してください。


入力例 2

3 1000
1 1 1
1 10 100

出力例 2

111 0 0

1 回以上操作すると、すべてのボールを 1 番目の高橋くんが持つことになります。


入力例 3

16 1000007
16 12 6 12 1 8 14 14 5 7 6 5 9 6 10 9
719092922 77021920 539975779 254719514 967592487 476893866 368936979 465399362 342544824 540338192 42663741 165480608 616996494 16552706 590788849 221462860

出力例 3

817852305 0 0 0 711863206 253280203 896552049 935714838 409506220 592088114 0 413190742 0 363914270 0 14254803

入力例 4

24 100000000007
19 10 19 15 1 20 13 15 8 23 22 16 19 22 2 20 12 19 17 20 16 8 23 6
944071276 364842194 5376942 671161415 477159272 339665353 176192797 2729865 676292280 249875565 259803120 103398285 466932147 775082441 720192643 535473742 263795756 898670859 476980306 12045411 620291602 593937486 761132791 746546443

出力例 4

918566373 436241503 0 0 0 455245534 0 356196743 0 906000633 0 268983266 21918337 0 733763572 173816039 754920403 0 273067118 205350062 0 566217111 80141532 0

Score : 550 points

Problem Statement

There are N Takahashi.

The i-th Takahashi has an integer A_i and B_i balls.

An integer x between 1 and K, inclusive, will be chosen uniformly at random, and they will repeat the following operation x times.

  • For every i, the i-th Takahashi gives all his balls to the A_i-th Takahashi.

Beware that all N Takahashi simultaneously perform this operation.

For each i=1,2,\ldots,N, find the expected value, modulo 998244353, of the number of balls the i-th Takahashi has at the end of the operations.

How to find a expected value modulo 998244353 It can be proved that the sought probability is always a rational number. Additionally, the constraints of this problem guarantee that if the sought probability is represented as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353. Here, there is a unique 0\leq z\lt998244353 such that y\equiv xz\pmod{998244353}, so report this z.

Constraints

  • 1\leq N\leq 2\times10^5
  • 1\leq K\leq 10^{18}
  • K is not a multiple of 998244353.
  • 1\leq A _ i\leq N\ (1\leq i\leq N)
  • 0\leq B _ i\lt998244353\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N K
A _ 1 A _ 2 \cdots A _ N
B _ 1 B _ 2 \cdots B _ N

Output

Print the expected value of the number of balls the i-th Takahashi has at the end of the operations for i=1,2,\ldots,N, separated by spaces, in a single line.


Sample Input 1

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

Sample Output 1

3 0 499122179 499122178 5

During two operations, the five Takahashi have the following number of balls.

If x=1 is chosen, the five Takahashi have 4,0,1,2,5 balls.
If x=2 is chosen, the five Takahashi have 2,0,4,1,5 balls.

Thus, the sought expected values are 3,0,\dfrac52,\dfrac32,5. Print these values modulo 998244353, that is, 3,0,499122179,499122178,5, separated by spaces.


Sample Input 2

3 1000
1 1 1
1 10 100

Sample Output 2

111 0 0

After one or more operations, the first Takahashi gets all balls.


Sample Input 3

16 1000007
16 12 6 12 1 8 14 14 5 7 6 5 9 6 10 9
719092922 77021920 539975779 254719514 967592487 476893866 368936979 465399362 342544824 540338192 42663741 165480608 616996494 16552706 590788849 221462860

Sample Output 3

817852305 0 0 0 711863206 253280203 896552049 935714838 409506220 592088114 0 413190742 0 363914270 0 14254803

Sample Input 4

24 100000000007
19 10 19 15 1 20 13 15 8 23 22 16 19 22 2 20 12 19 17 20 16 8 23 6
944071276 364842194 5376942 671161415 477159272 339665353 176192797 2729865 676292280 249875565 259803120 103398285 466932147 775082441 720192643 535473742 263795756 898670859 476980306 12045411 620291602 593937486 761132791 746546443

Sample Output 4

918566373 436241503 0 0 0 455245534 0 356196743 0 906000633 0 268983266 21918337 0 733763572 173816039 754920403 0 273067118 205350062 0 566217111 80141532 0
Ex - Negative Cost

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 625

問題文

あなたの目の前に体力 H の魔物が出現しました。 現在のあなたの魔力は 0 です。

あなたは技 1 、技 2\ldots 、技 N という N 種類の技を好きな順番で何回でも使うことができます。

i = 1, 2, \ldots, N について、技 i はあなたの魔力が C_i 以上のときにのみ使用することができ、 使用するとあなたの魔力が C_i だけ減り魔物の体力が D_i だけ減ります。 ただし、C_i が負の場合、あなたの魔力が C_i だけ減るということは、あなたの魔力が -C_i だけ増加するということを意味します。

魔物の体力が 0 以下になるまでにあなたが技を使う回数としてあり得る最小値を出力してください。 なお、本問題の制約下において、有限回の技の使用によって魔物の体力を 0 以下にできることが保証されます(下記の制約を参照)。

制約

  • 1 \leq N \leq 300
  • 1 \leq H \leq 10^{18}
  • -300 \leq C_i \leq 300
  • ある 1 \leq i \leq N が存在して C_i \leq 0
  • 1 \leq D_i \leq 10^9
  • 入力はすべて整数

入力

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

N H
C_1 D_1
C_2 D_2
\vdots
C_N D_N

出力

答えを出力せよ。


入力例 1

3 48
3 20
-4 2
1 5

出力例 1

5

あなたの魔力が 0 、魔物の体力が 48 である初期状態から、下記の手順で技を使うことを考えます。

  • 2 を使用する。その結果、あなたは魔力は 4 に、魔物の体力は 46 になる。
  • 3 を使用する。その結果、あなたは魔力は 3 に、魔物の体力は 41 になる。
  • 1 を使用する。その結果、あなたは魔力は 0 に、魔物の体力は 21 になる。
  • 2 を使用する。その結果、あなたは魔力は 4 に、魔物の体力は 19 になる。
  • 1 を使用する。その結果、あなたは魔力は 1 に、魔物の体力は -1 になる。

この場合、魔物の体力が 0 以下になるまでにあなたが技を使う回数は 5 回であり、これがあり得る最小値です。


入力例 2

20 583988303060450752
-64 273760634
-238 960719353
-114 191410838
-250 357733867
232 304621362
-286 644706927
210 37849132
-230 556412112
-142 136397527
101 380675202
-140 152300688
190 442931589
-187 940659077
-12 312523039
32 126515475
-143 979861204
105 488280613
240 664922712
290 732741849
69 541282303

出力例 2

595990842

Score : 625 points

Problem Statement

A monster with health H has appeared right in front of you. Your magic power is now 0.

You can use N moves called move 1, move 2, \ldots, move N, any number of times in any order.

For each i = 1, 2, \ldots, N, move i can only be used when your magic power is at least C_i, and its use will decrease your magic power by C_i and the monster's health by D_i. Here, if C_i is negative, decreasing your magic power by C_i means increasing it by -C_i.

Find the minimum possible number of times you use moves before the monster's health is 0 or lower. The constraints of this problem guarantee that a finite number of uses of moves can make it 0 or lower (see below).

Constraints

  • 1 \leq N \leq 300
  • 1 \leq H \leq 10^{18}
  • -300 \leq C_i \leq 300
  • C_i \leq 0 for some 1 \leq i \leq N.
  • 1 \leq D_i \leq 10^9
  • All input values are integers.

Input

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

N H
C_1 D_1
C_2 D_2
\vdots
C_N D_N

Output

Print the answer.


Sample Input 1

3 48
3 20
-4 2
1 5

Sample Output 1

5

From the initial state where your magic power is 0 and the monster's health is 48, consider using moves as follows.

  • Use move 2. Now, your magic power is 4, and the monster's health is 46.
  • Use move 3. Now, your magic power is 3, and the monster's health is 41.
  • Use move 1. Now, your magic power is 0, and the monster's health is 21.
  • Use move 2. Now, your magic power is 4, and the monster's health is 19.
  • Use move 1. Now, your magic power is 1, and the monster's health is -1.

Here, you use moves five times before the monster's health is not greater than 0, which is the minimum possible number.


Sample Input 2

20 583988303060450752
-64 273760634
-238 960719353
-114 191410838
-250 357733867
232 304621362
-286 644706927
210 37849132
-230 556412112
-142 136397527
101 380675202
-140 152300688
190 442931589
-187 940659077
-12 312523039
32 126515475
-143 979861204
105 488280613
240 664922712
290 732741849
69 541282303

Sample Output 2

595990842