A - 3.14

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

円周率の小数第 100 位までの値は

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

です。

1 以上 100 以下の整数 N が与えられます。

円周率を小数第 N 位まで出力してください。

より厳密には、円周率を小数第 N+1 位で切り捨て、末尾の 0 を取り除かずに出力してください。

制約

  • 1\leq N\leq 100
  • N は整数

入力

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

N

出力

円周率を小数第 N 位まで 1 行で出力せよ。


入力例 1

2

出力例 1

3.14

円周率を小数第 2+1 位で切り捨てると値は 3.14 になります。 よって、3.14 を出力してください。


入力例 2

32

出力例 2

3.14159265358979323846264338327950

末尾の 0 は取り除かずに出力してください。


入力例 3

100

出力例 3

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

Score : 100 points

Problem Statement

The number pi to the 100-th decimal place is

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679.

You are given an integer N between 1 and 100, inclusive.

Print the value of pi to the N-th decimal place.

More precisely, truncate the value of pi to N decimal places and print the result without removing the trailing 0s.

Constraints

  • 1\leq N\leq 100
  • N is an integer.

Input

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

N

Output

Print the value of pi to the N-th decimal place in a single line.


Sample Input 1

2

Sample Output 1

3.14

Truncating the value of pi to 2 decimal places results in 3.14. Thus, you should print 3.14.


Sample Input 2

32

Sample Output 2

3.14159265358979323846264338327950

Do not remove the trailing 0s.


Sample Input 3

100

Sample Output 3

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
B - Roulette

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

1 、人 2\ldots 、人 NN 人の人がルーレットの賭けに参加しました。 このルーレットの出目は、0 から 36 までの 37 個の整数のうちいずれかです。 i = 1, 2, \ldots, N について、人 i37 個の目のうち C_i 個の目 A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i} に賭けました。

ルーレットが回され、出目は X でした。 X に賭けた人たちのうち、賭けた目の個数が最も少ない人たちの番号を昇順にすべて出力してください。

より形式的には、1 以上 N 以下の整数 i であって、下記の 2 つの条件をともに満たすものを昇順にすべて出力してください。

  • iX に賭けている。
  • 任意の j = 1, 2, \ldots, N について「人 jX に賭けているならば、C_i \leq C_j 」が成り立つ。

出力するべき番号が 1 つも無い場合もあることに注意してください(入力例2を参照)。

制約

  • 1 \leq N \leq 100
  • 1 \leq C_i \leq 37
  • 0 \leq A_{i, j} \leq 36
  • 任意の i = 1, 2, \ldots, N について、A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i} はすべて異なる。
  • 0 \leq X \leq 36
  • 入力はすべて整数

入力

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

N
C_1
A_{1, 1} A_{1, 2} \ldots A_{1, C_1}
C_2
A_{2, 1} A_{2, 2} \ldots A_{2, C_2}
\vdots
C_N
A_{N, 1} A_{N, 2} \ldots A_{N, C_N}
X

出力

出力するべき番号を昇順にすベて並べた列を、B_1, B_2, \ldots, B_K とする。 下記の形式にしたがい、1 行目には出力するべき番号の個数 K を、 2 行目には B_1, B_2, \ldots, B_K を空白区切りで、それぞれ出力せよ。

K
B_1 B_2 \ldots B_K

入力例 1

4
3
7 19 20
4
4 19 24 0
2
26 10
3
19 31 24
19

出力例 1

2
1 4

ルーレットが回され、出目は 19 でした。 19 に賭けた人は人 1 、人 2 、人 43 人であり、それぞれが賭けた目の個数は 3, 4, 3 です。 よって、19 に賭けた人のうち、賭けた目の個数が最も少ない人は人 1 と人 42 人です。


入力例 2

3
1
1
1
2
1
3
0

出力例 2

0

ルーレットが回され出目は 0 でしたが、0 に賭けた人は一人もいないため、 出力するべき番号は 1 つもありません。

Score : 200 points

Problem Statement

N people, person 1, person 2, \ldots, person N, are playing roulette. The outcome of a spin is one of the 37 integers from 0 to 36. For each i = 1, 2, \ldots, N, person i has bet on C_i of the 37 possible outcomes: A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i}.

The wheel has been spun, and the outcome is X. Print the numbers of all people who have bet on X with the fewest bets, in ascending order.

More formally, print all integers i between 1 and N, inclusive, that satisfy both of the following conditions, in ascending order:

  • Person i has bet on X.
  • For each j = 1, 2, \ldots, N, if person j has bet on X, then C_i \leq C_j.

Note that there may be no number to print (see Sample Input 2).

Constraints

  • 1 \leq N \leq 100
  • 1 \leq C_i \leq 37
  • 0 \leq A_{i, j} \leq 36
  • A_{i, 1}, A_{i, 2}, \ldots, A_{i, C_i} are all different for each i = 1, 2, \ldots, N.
  • 0 \leq X \leq 36
  • All input values are integers.

Input

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

N
C_1
A_{1, 1} A_{1, 2} \ldots A_{1, C_1}
C_2
A_{2, 1} A_{2, 2} \ldots A_{2, C_2}
\vdots
C_N
A_{N, 1} A_{N, 2} \ldots A_{N, C_N}
X

Output

Let B_1, B_2, \ldots, B_K be the sequence of numbers to be printed in ascending order. Using the following format, print the count of numbers to be printed, K, on the first line, and B_1, B_2, \ldots, B_K separated by spaces on the second line:

K
B_1 B_2 \ldots B_K

Sample Input 1

4
3
7 19 20
4
4 19 24 0
2
26 10
3
19 31 24
19

Sample Output 1

2
1 4

The wheel has been spun, and the outcome is 19. The people who has bet on 19 are person 1, person 2, and person 4, and the number of their bets are 3, 4, and 3, respectively. Therefore, among the people who has bet on 19, the ones with the fewest bets are person 1 and person 4.


Sample Input 2

3
1
1
1
2
1
3
0

Sample Output 2

0

The wheel has been spun and the outcome is 0, but no one has bet on 0, so there is no number to print.

C - Rotate Colored Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

英小文字からなる長さ N の文字列 S が与えられます。 S の各文字は色 1 、色 2\ldots 、色 MM 色のうちのいずれかで塗られており、 i = 1, 2, \ldots, N について、Si 文字目は色 C_i で塗られています。

i = 1, 2, \ldots, M について、この順番に下記の操作を行います。

  • S の色 i で塗られた文字からなる部分を、右に 1 つ巡回シフトする。 すなわち、S の 色 i で塗られた文字の位置が先頭のものから順に p_1, p_2, p_3, \ldots, p_k 文字目であるとき、 Sp_1, p_2, p_3, \ldots, p_k 文字目を、それぞれ、Sp_k, p_1,p_2, \ldots, p_{k-1} 文字目で同時に置き換える。

上記の操作をすべて行った後の、最終的な S を出力してください。

なお、M 色あるどの色についても、その色で塗られた S の文字が必ず 1 つ以上存在することが、制約として保証されます。

制約

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, C_i はすべて整数
  • S は英小文字からなる長さ N の文字列
  • 任意の整数 1 \leq i \leq M に対して、ある整数 1 \leq j \leq N が存在して C_j = i が成り立つ

入力

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

N M
S
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

出力例 1

cszapqbr

はじめ、S = apzbqrcs です。

  • i = 1 に対する操作では、S1, 4, 7 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cpzaqrbs となります。
  • i = 2 に対する操作では、S2, 5, 6, 8 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります。
  • i = 3 に対する操作では、S3 文字目からなる部分を右に 1 つ巡回シフトします。その結果、S = cszapqbr となります(操作の前後で S は変わりません)。

よって、最終的な S である cszapqbr を出力します。


入力例 2

2 1
aa
1 1

出力例 2

aa

Score : 300 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters. Each character of S is painted in one of the M colors: color 1, color 2, ..., color M; for each i = 1, 2, \ldots, N, the i-th character of S is painted in color C_i.

For each i = 1, 2, \ldots, M in this order, let us perform the following operation.

  • Perform a right circular shift by 1 on the part of S painted in color i. That is, if the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters are painted in color i from left to right, then simultaneously replace the p_1-th, p_2-th, p_3-th, \ldots, p_k-th characters of S with the p_k-th, p_1-th, p_2-th, \ldots, p_{k-1}-th characters of S, respectively.

Print the final S after the above operations.

The constraints guarantee that at least one character of S is painted in each of the M colors.

Constraints

  • 1 \leq M \leq N \leq 2 \times 10^5
  • 1 \leq C_i \leq M
  • N, M, and C_i are all integers.
  • S is a string of length N consisting of lowercase English letters.
  • For each integer 1 \leq i \leq M, there is an integer 1 \leq j \leq N such that C_j = i.

Input

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

N M
S
C_1 C_2 \ldots C_N

Output

Print the answer.


Sample Input 1

8 3
apzbqrcs
1 2 3 1 2 2 1 2

Sample Output 1

cszapqbr

Initially, S = apzbqrcs.

  • For i = 1, perform a right circular shift by 1 on the part of S formed by the 1-st, 4-th, 7-th characters, resulting in S = cpzaqrbs.
  • For i = 2, perform a right circular shift by 1 on the part of S formed by the 2-nd, 5-th, 6-th, 8-th characters, resulting in S = cszapqbr.
  • For i = 3, perform a right circular shift by 1 on the part of S formed by the 3-rd character, resulting in S = cszapqbr (here, S is not changed).

Thus, you should print cszapqbr, the final S.


Sample Input 2

2 1
aa
1 1

Sample Output 2

aa
D - LOWER

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

英大文字および英小文字からなる長さ N の文字列 S が与えられます。

これから、文字列 S に対して Q 回の操作を行います。 i 番目 (1\leq i\leq Q) の操作は整数 2 つと文字 1 つからなる組 (t _ i,x _ i,c _ i) で表され、それぞれ次のような操作を表します。

  • t _ i=1 のとき、Sx _ i 文字目を c _ i に変更する。
  • t _ i=2 のとき、S に含まれる大文字すべてをそれぞれ小文字に変更する(x _ i,c _ i は操作に使用しない)。
  • t _ i=3 のとき、S に含まれる小文字すべてをそれぞれ大文字に変更する(x _ i,c _ i は操作に使用しない)。

Q 回の操作がすべて終わったあとの S を出力してください。

制約

  • 1\leq N\leq5\times10^5
  • S は英大文字および英小文字からなる長さ N の文字列
  • 1\leq Q\leq5\times10^5
  • 1\leq t _ i\leq3\ (1\leq i\leq Q)
  • t _ i=1 ならば 1\leq x _ i\leq N\ (1\leq i\leq Q)
  • c _ i は英大文字もしくは英小文字
  • t _ i\neq 1 ならば x _ i=0 かつ c _ i= 'a'
  • N,Q,t _ i,x _ i はすべて整数

入力

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

N
S
Q
t _ 1 x _ 1 c _ 1
t _ 2 x _ 2 c _ 2
\vdots
t _ Q x _ Q c _ Q

出力

答えを 1 行で出力せよ。


入力例 1

7
AtCoder
5
1 4 i
3 0 a
1 5 b
2 0 a
1 4 Y

出力例 1

atcYber

はじめ、文字列 SAtCoder です。

  • 1 番目の操作では、4 文字目を i に変更します。変更後の SAtCider です。
  • 2 番目の操作では、すべての小文字を大文字に変更します。変更後の SATCIDER です。
  • 3 番目の操作では、5 文字目を b に変更します。変更後の SATCIbER です。
  • 4 番目の操作では、すべての大文字を小文字に変更します。変更後の Satciber です。
  • 5 番目の操作では、4 文字目を Y に変更します。変更後の SatcYber です。

すべての操作が終わったあとの SatcYber なので、atcYber と出力してください。


入力例 2

35
TheQuickBrownFoxJumpsOverTheLazyDog
10
2 0 a
1 19 G
1 13 m
1 2 E
1 21 F
2 0 a
1 27 b
3 0 a
3 0 a
1 15 i

出力例 2

TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG

Score : 400 points

Problem Statement

You are given a string S of length N consisting of uppercase and lowercase English letters.

Let us perform Q operations on the string S. The i-th operation (1\leq i\leq Q) is represented by a tuple (t _ i,x _ i,c _ i) of two integers and one character, as follows.

  • If t _ i=1, change the x _ i-th character of S to c _ i.
  • If t _ i=2, convert all uppercase letters in S to lowercase (do not use x _ i,c _ i for this operation).
  • If t _ i=3, convert all lowercase letters in S to uppercase (do not use x _ i,c _ i for this operation).

Print the S after the Q operations.

Constraints

  • 1\leq N\leq5\times10^5
  • S is a string of length N consisting of uppercase and lowercase English letters.
  • 1\leq Q\leq5\times10^5
  • 1\leq t _ i\leq3\ (1\leq i\leq Q)
  • If t _ i=1, then 1\leq x _ i\leq N\ (1\leq i\leq Q).
  • c _ i is an uppercase or lowercase English letter.
  • If t _ i\neq 1, then x _ i=0 and c _ i= 'a'.
  • N,Q,t _ i,x _ i are all integers.

Input

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

N
S
Q
t _ 1 x _ 1 c _ 1
t _ 2 x _ 2 c _ 2
\vdots
t _ Q x _ Q c _ Q

Output

Print the answer in a single line.


Sample Input 1

7
AtCoder
5
1 4 i
3 0 a
1 5 b
2 0 a
1 4 Y

Sample Output 1

atcYber

Initially, the string S is AtCoder.

  • The first operation changes the 4-th character to i, changing S to AtCider.
  • The second operation converts all lowercase letters to uppercase, changing S to ATCIDER.
  • The third operation changes the 5-th character to b, changing S to ATCIbER.
  • The fourth operation converts all uppercase letters to lowercase, changing S to atciber.
  • The fifth operation changes the 4-th character to Y, changing S to atcYber.

After the operations, the string S is atcYber, so print atcYber.


Sample Input 2

35
TheQuickBrownFoxJumpsOverTheLazyDog
10
2 0 a
1 19 G
1 13 m
1 2 E
1 21 F
2 0 a
1 27 b
3 0 a
3 0 a
1 15 i

Sample Output 2

TEEQUICKBROWMFiXJUGPFOVERTBELAZYDOG
E - Roulettes

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 475

問題文

N 台のルーレットがあります。 i 番目 (1\leq i\leq N) のルーレットには P _ i 個の整数 S _ {i,1},S _ {i,2},\ldots,S _ {i,P _ i} が書かれており、C _ i 円支払うことで 1 回プレイできます。 i 番目のルーレットを 1 回プレイすると、1 以上 P _ i 以下の整数 j が一様ランダムに選ばれ、S _ {i,j} ポイントを得ることができます。

ルーレットで得られるポイントは、過去の結果と独立に決まります。

高橋くんは、ポイントを M ポイント以上獲得したいです。 高橋くんは、M ポイント以上獲得するまでに支払う金額をなるべく小さくするように行動します。 ただし、高橋くんはルーレットをプレイするたびこれまでのルーレットの結果を見て次にプレイするルーレットを選ぶことができます。

高橋くんがポイントを M ポイント以上獲得するまでに支払う金額の期待値を求めてください。

より厳密な定義

より厳密には、次のようになります。 高橋くんがルーレットを選ぶ戦略を決めるごとに、その戦略で M ポイント以上獲得するまでに支払う金額の期待値 E が次のように定義されます。

  • 自然数 X に対して、その戦略に従って高橋くんが M ポイント以上獲得するか、ルーレットを X 回プレイするまでに支払う金額の期待値を f(X) とする。E=\displaystyle\lim _ {X\to+\infty}f(X) とする。

この問題の条件のもとで、高橋くんがどのような戦略をとっても \displaystyle\lim _ {X\to+\infty}f(X) が有限の値になることが証明できます。 高橋くんが E を最小にするような戦略をとったときの E の値を求めてください。

制約

  • 1\leq N\leq 100
  • 1\leq M\leq 100
  • 1\leq C _ i\leq 10 ^ 4\ (1\leq i\leq N)
  • 1\leq P _ i\leq 100\ (1\leq i\leq N)
  • 0\leq S _ {i,j}\leq M\ (1\leq i\leq N,1\leq j\leq P _ i)
  • \displaystyle\sum _ {j=1}^{P _ i}S _ {i,j}\gt0\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

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

出力

高橋くんが M ポイント以上獲得するまでに支払う金額の期待値を 1 行で出力せよ。 出力された値と真の値の相対誤差もしくは絶対誤差が 10 ^ {-5} 以下のとき、正答と判定される。


入力例 1

3 14
100 2 5 9
50 4 1 2 4 8
70 5 2 4 2 8 8

出力例 1

215.913355350494384765625

例えば、高橋くんはルーレットを次のようにプレイすることができます。

  • 50 円を支払ってルーレット 2 をプレイする。S _ {2,4}=8 ポイントを得る。
  • 50 円を支払ってルーレット 2 をプレイする。S _ {2,1}=1 ポイントを得る。
  • 100 円を支払ってルーレット 1 をプレイする。S _ {1,1}=5 ポイントを得る。得たポイントの合計が 8+1+5\geq14 ポイントとなったため、終了する。

この例では、14 ポイントを得るまでに 200 円を支払っています。

出力と真の値の相対誤差もしくは絶対誤差が 10 ^ {-5} 以下のとき正答と判定されるため、215.9112215.9155 などと出力しても正解になります。


入力例 2

2 100
1 2 1 2
10 6 0 0 0 0 0 100

出力例 2

60

100 ポイントが出るまでルーレット 2 を回し続けるのが最適です。


入力例 3

20 90
3252 9 0 4 2 7 3 2 3 2 4
2147 1 1
4033 8 0 4 1 7 5 2 5 0
3795 6 6 6 2 3 2 2
3941 7 2 4 4 7 2 0 5
2815 6 2 1 0 5 2 2
3020 2 3 6
3858 9 4 2 7 3 0 4 4 6 5
4533 10 3 6 4 0 6 4 4 2 7 7
4198 8 6 7 0 6 3 6 5 6
3739 8 2 7 1 5 1 4 4 7
2465 4 1 4 0 1
4418 9 7 6 2 4 6 1 5 0 7
5450 12 0 4 4 7 7 4 4 5 4 5 3 7
4196 9 1 6 5 5 7 2 3 6 3
4776 9 2 2 7 3 6 6 1 6 6
2286 3 3 5 6
3152 3 4 1 5
3509 7 0 6 7 0 1 0 3
2913 6 0 1 5 0 5 6

出力例 3

45037.072314895291126319493887599716

Score : 475 points

Problem Statement

There are N roulette wheels. The i-th (1\leq i\leq N) wheel has P _ i integers S _ {i,1},S _ {i,2},\ldots,S _ {i,P _ i} written on it, and you can play it once by paying C _ i yen. When you play the i-th wheel once, an integer j between 1 and P _ i, inclusive, is chosen uniformly at random, and you earn S _ {i,j} points.

The points you earn from the wheels are determined independently of past results.

Takahashi wants to earn at least M points. Takahashi will act to minimize the amount of money he pays before he earns at least M points. After each play, he can choose which wheel to play next based on the previous results.

Find the expected amount of money Takahashi will pay before he earns at least M points.

More formal definition

Here is a more formal statement. For a strategy that Takahashi can adopt in choosing which wheel to play, the expected amount of money E that he pays before he earns at least M points with that strategy is defined as follows.

  • For a natural number X, let f(X) be the expected amount of money Takahashi pays before he earns at least M points or plays the wheels X times in total according to that strategy. Let E=\displaystyle\lim _ {X\to+\infty}f(X).

Under the conditions of this problem, it can be proved that \displaystyle\lim _ {X\to+\infty}f(X) is finite no matter what strategy Takahashi adopts. Find the value of E when he adopts a strategy that minimizes E.

Constraints

  • 1\leq N\leq 100
  • 1\leq M\leq 100
  • 1\leq C _ i\leq 10 ^ 4\ (1\leq i\leq N)
  • 1\leq P _ i\leq 100\ (1\leq i\leq N)
  • 0\leq S _ {i,j}\leq M\ (1\leq i\leq N,1\leq j\leq P _ i)
  • \displaystyle\sum _ {j=1}^{P _ i}S _ {i,j}\gt0\ (1\leq i\leq N)
  • All input values are integers.

Input

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

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

Output

Print the expected amount of money Takahashi will pay until he earns at least M points in a single line. Your output will be considered correct when the relative or absolute error from the true value is at most 10 ^ {-5}.


Sample Input 1

3 14
100 2 5 9
50 4 1 2 4 8
70 5 2 4 2 8 8

Sample Output 1

215.913355350494384765625

For instance, Takahashi can play the wheels as follows.

  • Pay 50 yen to play roulette 2 and earn S _ {2,4}=8 points.
  • Pay 50 yen to play roulette 2 and earn S _ {2,1}=1 point.
  • Pay 100 yen to play roulette 1 and earn S _ {1,1}=5 points. He has earned a total of 8+1+5\geq14 points, so he quits playing.

In this case, he pays 200 yen before earning 14 points.

Your output will be considered correct when the relative or absolute error from the true value is at most 10 ^ {-5}, so outputs such as 215.9112 and 215.9155 would also be considered correct.


Sample Input 2

2 100
1 2 1 2
10 6 0 0 0 0 0 100

Sample Output 2

60

It is optimal to keep spinning roulette 2 until you get 100 points.


Sample Input 3

20 90
3252 9 0 4 2 7 3 2 3 2 4
2147 1 1
4033 8 0 4 1 7 5 2 5 0
3795 6 6 6 2 3 2 2
3941 7 2 4 4 7 2 0 5
2815 6 2 1 0 5 2 2
3020 2 3 6
3858 9 4 2 7 3 0 4 4 6 5
4533 10 3 6 4 0 6 4 4 2 7 7
4198 8 6 7 0 6 3 6 5 6
3739 8 2 7 1 5 1 4 4 7
2465 4 1 4 0 1
4418 9 7 6 2 4 6 1 5 0 7
5450 12 0 4 4 7 7 4 4 5 4 5 3 7
4196 9 1 6 5 5 7 2 3 6 3
4776 9 2 2 7 3 6 6 1 6 6
2286 3 3 5 6
3152 3 4 1 5
3509 7 0 6 7 0 1 0 3
2913 6 0 1 5 0 5 6

Sample Output 3

45037.072314895291126319493887599716
F - A Certain Game

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 475

問題文

とあるゲームの大会に、プレイヤー 1 、プレイヤー 2\ldots 、プレイヤー NN 人のプレイヤーが参加します。 大会の開始直前、各プレイヤーはそれぞれ 1 人のみからなるチームをなし、全部で N 個のチームがあります。

大会では全部で N−1 回の試合があり、各試合では 2 つの異なるチームが選ばれ、一方が先攻を、もう一方が後攻を受け持って対戦し、その結果ちょうど一方のチームが勝ちます。 具体的には、i = 1, 2, \ldots, N-1 について i 回目の試合は下記の通りに進行します。

  • プレイヤー p_i の属するチームが先攻、プレイヤー q_i の属するチームが後攻として、対戦を行う。
  • その結果、先攻チームの人数を a 、後攻チームの人数を b として、\frac{a}{a+b} の確率で先攻のチームが、\frac{b}{a+b} の確率で後攻のチームが勝つ。
  • その後、勝負した 2 チームは 1 つのチームに併合される。

なお、各試合の対戦結果は他の試合の対戦結果とは独立です。

N 人のプレイヤーそれぞれについて、大会全体で自分が所属するチームが勝つという出来事が起こる回数の期待値 \text{mod } 998244353 を出力してください。

期待値 \text{mod } 998244353 の定義

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

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

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq p_i, q_i \leq N
  • i 回目の試合の直前、プレイヤー p_i が属するチームとプレイヤー q_i が属するチームは異なる。
  • 入力はすべて整数

入力

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

N
p_1 q_1
p_2 q_2
\vdots
p_{N-1} q_{N-1}

出力

i = 1, 2, \ldots, N について、大会全体でプレイヤー i が所属するチームが勝つという出来事が起こる回数の期待値 \text{mod } 998244353 である E_i を、 下記の形式にしたがって空白区切りで出力せよ。

E_1 E_2 \ldots E_N

入力例 1

5
1 2
4 3
5 3
1 4

出力例 1

698771048 698771048 964969543 964969543 133099248

チームに所属するプレイヤーの番号が x_1, x_2, \ldots, x_k であるチームを、チーム \lbrace x_1, x_2, \ldots, x_k \rbrace と呼びます。

  • 1 回目の試合では、プレイヤー 1 が所属するチーム \lbrace 1 \rbrace とプレイヤー 2 が所属するチーム \lbrace 2 \rbrace が対戦し、 \frac{1}{2} の確率でチーム \lbrace 1 \rbrace が、\frac{1}{2} の確率でチーム \lbrace 2 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 1, 2 \rbrace になります。
  • 2 回目の試合では、プレイヤー 4 が所属するチーム \lbrace 4 \rbrace とプレイヤー 3 が所属するチーム \lbrace 3 \rbrace が対戦し、 \frac{1}{2} の確率でチーム \lbrace 4 \rbrace が、\frac{1}{2} の確率でチーム \lbrace 3 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 3, 4 \rbrace になります。
  • 3 回目の試合では、プレイヤー 5 が所属するチーム \lbrace 5 \rbrace とプレイヤー 3 が所属するチーム \lbrace 3, 4 \rbrace が対戦し、 \frac{1}{3} の確率でチーム \lbrace 5 \rbrace が、\frac{2}{3} の確率でチーム \lbrace 3, 4 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 3, 4, 5 \rbrace になります。
  • 4 回目の試合では、プレイヤー 1 が所属するチーム \lbrace 1, 2 \rbrace とプレイヤー 4 が所属するチーム \lbrace 3, 4, 5 \rbrace が対戦し、 \frac{2}{5} の確率でチーム \lbrace 1, 2 \rbrace が、\frac{3}{5} の確率でチーム \lbrace 3, 4, 5 \rbrace が勝ちます。 その後、2 つのチームは併合され、1 つのチーム \lbrace 1, 2, 3, 4, 5 \rbrace になります。

プレイヤー 1, 2, 3, 4, 5 それぞれの、大会全体で自分が所属するチームが勝つという出来事が起こる回数の期待値 E_1, E_2, E_3, E_4, E_5 は、それぞれ \frac{9}{10}, \frac{9}{10}, \frac{53}{30}, \frac{53}{30}, \frac{14}{15} です。


入力例 2

15
9 2
8 10
13 6
12 11
7 10
4 10
14 2
5 4
1 15
15 2
6 9
8 11
6 3
2 8

出力例 2

43970290 310168785 806914186 501498951 950708909 272140427 335124893 168750835 310168785 168750835 280459129 280459129 272140427 476542843 43970290

Score : 475 points

Problem Statement

N players, player 1, player 2, ..., player N, participate in a game tournament. Just before the tournament starts, each player forms a one-person team, so there are N teams in total.

The tournament has a total of N-1 matches. In each match, two different teams are chosen. One team goes first, and the other goes second. Each match will result in exactly one team winning. Specifically, for each i = 1, 2, \ldots, N-1, the i-th match proceeds as follows.

  • The team with player p_i goes first, and the team with player q_i goes second.
  • Let a and b be the numbers of players in the first and second teams, respectively. The first team wins with probability \frac{a}{a+b}, and the second team wins with probability \frac{b}{a+b}.
  • Then, the two teams are combined into a single team.

The result of each match is independent of those of the others.

For each of the N players, print the expected number of times the team with that player wins throughout the tournament, modulo 998244353.

How to print an expected value modulo 998244353

It can be proved that the sought expected value is always rational. Also, the constraints of this problem guarantee that if the sought expected value is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.

Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq p_i, q_i \leq N
  • Just before the i-th match, player p_i and player q_i belong to different teams.
  • All input values are integers.

Input

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

N
p_1 q_1
p_2 q_2
\vdots
p_{N-1} q_{N-1}

Output

For each i = 1, 2, \ldots, N, print E_i, the expected number, modulo 998244353, of times the team with player i wins throughout the tournament, separated by spaces, in the following format:

E_1 E_2 \ldots E_N

Sample Input 1

5
1 2
4 3
5 3
1 4

Sample Output 1

698771048 698771048 964969543 964969543 133099248

We call a team formed by player x_1, player x_2, \ldots, player x_k as team \lbrace x_1, x_2, \ldots, x_k \rbrace.

  • The first match is played by team \lbrace 1 \rbrace, with player 1, and team \lbrace 2 \rbrace, with player 2. Team \lbrace 1 \rbrace wins with probability \frac{1}{2}, and team \lbrace 2 \rbrace wins with probability \frac{1}{2}. Then, the two teams are combined into a single team \lbrace 1, 2 \rbrace.
  • The second match is played by team \lbrace 4 \rbrace, with player 4, and team \lbrace 3 \rbrace, with player 3. Team \lbrace 4 \rbrace wins with probability \frac{1}{2}, and team \lbrace 3 \rbrace wins with probability \frac{1}{2}. Then, the two teams are combined into a single team \lbrace 3, 4 \rbrace.
  • The third match is played by team \lbrace 5 \rbrace, with player 5, and team \lbrace 3, 4 \rbrace, with player 3. Team \lbrace 5 \rbrace wins with probability \frac{1}{3}, and team \lbrace 3, 4 \rbrace wins with probability \frac{2}{3}. Then, the two teams are combined into a single team \lbrace 3, 4, 5 \rbrace.
  • The fourth match is played by team \lbrace 1, 2 \rbrace, with player 1, and team \lbrace 3, 4, 5 \rbrace, with player 4. Team \lbrace 1, 2 \rbrace wins with probability \frac{2}{5}, and team \lbrace 3, 4, 5 \rbrace wins with probability \frac{3}{5}. Then, the two teams are combined into a single team \lbrace 1, 2, 3, 4, 5 \rbrace.

The expected numbers of times the teams with players 1, 2, 3, 4, 5 win throughout the tournament, E_1, E_2, E_3, E_4, E_5, are \frac{9}{10}, \frac{9}{10}, \frac{53}{30}, \frac{53}{30}, \frac{14}{15}, respectively.


Sample Input 2

15
9 2
8 10
13 6
12 11
7 10
4 10
14 2
5 4
1 15
15 2
6 9
8 11
6 3
2 8

Sample Output 2

43970290 310168785 806914186 501498951 950708909 272140427 335124893 168750835 310168785 168750835 280459129 280459129 272140427 476542843 43970290
G - Amulets

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 575

問題文

洞窟に、モンスター 1 、モンスター 2\ldots 、モンスター NN 体のモンスターがおり、各モンスターには正整数の攻撃力と、1 以上 M 以下の整数で表されるタイプが定められています。 具体的には、i = 1, 2, \ldots, N について、モンスター i の攻撃力は A_i でタイプは B_i です。

高橋君はお守り 1 、お守り 2\ldots 、お守り MM 個のお守りのうちのいくつかを持って、体力H の状態でこの洞窟に冒険に出かけます。

冒険では高橋君は(体力が 0 以下になって力尽きない限り)i = 1, 2, \ldots, N の順に下記の手順を行います。

  • もし高橋君がお守り B_i を冒険に持ってきていないなら、高橋君はモンスター i の攻撃を受け、高橋君の体力が A_i だけ減少する。
  • その後の時点での高橋君の体力が、
    • 0 より大きいならば、高橋君はモンスター i を倒す。
    • 0 以下ならば、高橋君はモンスター i を倒せずに力尽きて冒険を終了する。

K = 0, 1, \ldots, M のそれぞれの場合について独立に、下記の問題を解いてください。

高橋君が全 M 個のお守りの中から K 個を選んで冒険に持っていくときの、高橋君が倒すモンスターの数としてあり得る最大値を求めよ。

なお、任意の i = 1, 2, \ldots, M について、タイプが i であるモンスターが必ず 1 体以上いることが、制約として保証されます。

制約

  • 1 \leq M \leq N \leq 3 \times 10^5
  • 1 \leq H \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq M
  • 任意の 1 \leq i \leq M に対して、ある 1 \leq j \leq N が存在して B_j = i が成り立つ
  • 入力はすべて整数

入力

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

N M H
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

i = 0, 1, 2, \ldots, M について、K = i の場合の高橋君が倒すモンスターの数の最大値を X_i とする。 X_0, X_1, \ldots, X_M を下記の形式にしたがって空白区切りで出力せよ。

X_0 X_1 \ldots X_M

入力例 1

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

出力例 1

2 5 7 7

K = 1 の問題を考えます。この場合、高橋君はお守り 2 を持っていくことで、5 体のモンスターを倒し、倒すモンスターの数の最大値を達成することができます。 その際の冒険は、下記の通りに進行します。

  • i = 1 について、高橋君はお守り 2 を持っているため、モンスター 1 の攻撃を免れます。その後、高橋君はモンスター 1 を倒します。
  • i = 2 について、高橋君はお守り 1 を持っていないため、モンスター 2 の攻撃を受けて体力が 6 になります。その後、高橋君はモンスター 2 を倒します。
  • i = 3 について、高橋君はお守り 2 を持っているため、モンスター 3 の攻撃を免れます。その後、高橋君はモンスター 3 を倒します。
  • i = 4 について、高橋君はお守り 2 を持っているため、モンスター 4 の攻撃を免れます。その後、高橋君はモンスター 4 を倒します。
  • i = 5 について、高橋君はお守り 1 を持っていないため、モンスター 5 の攻撃を受けて体力が 1 になります。その後、高橋君はモンスター 5 を倒します。
  • i = 6 について、高橋君はお守り 3 を持っていないため、モンスター 6 の攻撃を受けて体力が -8 になります。その後、高橋君はモンスター 6 を倒せずに力尽きて冒険を終了します。

同様に、K = 0 の場合は 2 体のモンスターを、 K = 2 の場合はお守り 2, 3 を持っていくことで 7 体のモンスター全てを、 K = 3 の場合はお守り 1, 2, 3 を持っていくことで 7 体のモンスター全てを倒すことができます。


入力例 2

15 5 400
29 5
27 4
79 1
27 2
30 3
4 1
89 2
88 3
75 5
3 1
39 4
12 1
62 4
38 2
49 1

出力例 2

8 12 15 15 15 15

Score : 575 points

Problem Statement

There are N monsters in a cave: monster 1, monster 2, \ldots, monster N. Each monster has a positive integer attack power and a type represented by an integer between 1 and M, inclusive. Specifically, for i = 1, 2, \ldots, N, the attack power and type of monster i are A_i and B_i, respectively.

Takahashi will go on an adventure in this cave with a health of H and some of the M amulets: amulet 1, amulet 2, \ldots, amulet M.

In the adventure, Takahashi performs the following steps for i = 1, 2, \ldots, N in this order (as long as his health does not drop to 0 or below).

  • If Takahashi has not brought amulet B_i with him, monster i will attack him and decrease his health by A_i.
  • Then,
    • if his health is greater than 0, he defeats monster i;
    • otherwise, he dies without defeating monster i and ends his adventure.

Solve the following problem for each K = 0, 1, \ldots, M independently.

Find the maximum number of monsters that Takahashi can defeat when choosing K of the M amulets to bring on the adventure.

The constraints guarantee that there is at least one monster of type i for each i = 1, 2, \ldots, M.

Constraints

  • 1 \leq M \leq N \leq 3 \times 10^5
  • 1 \leq H \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq M
  • For each 1 \leq i \leq M, there is 1 \leq j \leq N such that B_j = i.
  • All input values are integers.

Input

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

N M H
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

For each i = 0, 1, 2, \ldots, M, let X_i be the maximum number of monsters that Takahashi can defeat when K = i. Print X_0, X_1, \ldots, X_M separated by spaces in the following format:

X_0 X_1 \ldots X_M

Sample Input 1

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

Sample Output 1

2 5 7 7

Consider the case K = 1. Here, Takahashi can bring amulet 2 to defeat the maximum possible number of monsters, which is 5. The adventure proceeds as follows.

  • For i = 1, he avoids the attack of monster 1 since he has amulet 2. Then, he defeats monster 1.
  • For i = 2, he takes the attack of monster 2 and his health becomes 6 since he does not have amulet 1. Then, he defeats monster 2.
  • For i = 3, he avoids the attack of monster 3 since he has amulet 2. Then, he defeats monster 3.
  • For i = 4, he avoids the attack of monster 4 since he has amulet 2. Then, he defeats monster 4.
  • For i = 5, he takes the attack of monster 5 and his health becomes 1 since he does not have amulet 1. Then, he defeats monster 5.
  • For i = 6, he takes the attack of monster 6 and his health becomes -8 since he does not have amulet 3. Then, he dies without defeating monster 6 and ends his adventure.

Similarly, when K=0, he can defeat 2 monsters; when K=2, he can defeat all 7 monsters by bringing amulets 2 and 3; when K=3, he can defeat all 7 monsters by bringing amulets 1, 2, and 3.


Sample Input 2

15 5 400
29 5
27 4
79 1
27 2
30 3
4 1
89 2
88 3
75 5
3 1
39 4
12 1
62 4
38 2
49 1

Sample Output 2

8 12 15 15 15 15
Ex - Disk and Segments

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 625

問題文

座標平面上に N 本の線分があり、i 本目 (1\leq i\leq N) の線分は 2(a _ i,b _ i),(c _ i,d _ i) を端点とする線分です。 ここで、どの線分も端点を含みます。 また、どの 2 線分も互いに共有点を持ちません。

この平面上に 1 つだけ閉円盤を配置し、どの線分とも共有点を持つようにしたいです。 つまり、円を 1 つ描くことで、どの線分もその円周もしくはその内部(あるいはその両方)と共有点を持つようにしたいです。 そのような円盤の半径としてありえる最小の値を求めてください。

制約

  • 2\leq N\leq 100
  • 0\leq a _ i,b _ i,c _ i,d _ i\leq1000\ (1\leq i\leq N)
  • (a _ i,b _ i)\neq(c _ i,d _ i)\ (1\leq i\leq N)
  • i 番目の線分と j 番目の線分の共有点は存在しない (1\leq i\lt j\leq N)
  • 入力はすべて整数

入力

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

N
a _ 1 b _ 1 c _ 1 d _ 1
a _ 2 b _ 2 c _ 2 d _ 2
\vdots
a _ N b _ N c _ N d _ N

出力

答えを 1 行で出力せよ。出力された値と真の値の相対誤差もしくは絶対誤差が 10 ^ {−5} 以下のとき、正答と判定される。


入力例 1

4
2 3 2 10
4 0 12 6
4 8 6 3
7 8 10 8

出力例 1

3.319048676309097923796460081961

与えられた線分は以下の図のようになります。 図のように、中心が \left(\dfrac{32-\sqrt{115}}4,\dfrac{21-\sqrt{115}}2\right) で半径が \dfrac{24-\sqrt{115}}4 である閉円盤はすべての線分と共通点を持ちます。

半径が \dfrac{24-\sqrt{115}}4 未満の円盤をどう配置しても、すべての線分と共有点を持つようにすることはできないため、答えは \dfrac{24-\sqrt{115}}4 です。

出力と真の値との絶対誤差もしくは相対誤差が 10^{-5} 以下であれば正答と判定されるため、3.319083.31902 などと出力しても正解になります。


入力例 2

20
0 18 4 28
2 21 8 21
3 4 10 5
3 14 10 13
5 9 10 12
6 9 10 6
6 28 10 18
12 11 15 13
12 17 12 27
13 17 20 18
13 27 19 26
16 1 16 13
16 22 19 25
17 22 20 19
18 4 23 4
18 5 23 11
22 16 22 23
23 15 30 15
23 24 30 24
24 0 24 11

出力例 2

12.875165712523887403637822024952

図のように、中心が \left(\dfrac{19817-8\sqrt{5991922}}{18},\dfrac{-2305+\sqrt{5991922}}9\right) で半径が \dfrac{3757\sqrt{29}-44\sqrt{206618}}{18} である閉円盤はすべての線分と共通点を持ちます。


入力例 3

30
526 655 528 593
628 328 957 211
480 758 680 794
940 822 657 949
127 23 250 385
281 406 319 305
277 598 190 439
437 450 725 254
970 478 369 466
421 225 348 141
872 64 600 9
634 460 759 337
878 514 447 534
142 237 191 269
983 34 554 284
694 160 589 239
391 631 22 743
377 656 500 606
390 576 184 312
556 707 457 699
796 870 186 773
12 803 505 586
343 541 42 165
478 340 176 2
39 618 6 651
753 883 47 833
551 593 873 672
983 729 338 747
721 77 541 255
0 32 98 597

出力例 3

485.264732620930836460637042310401

Score : 625 points

Problem Statement

There are N line segments in a coordinate plane, and the i-th line segment (1\leq i\leq N) has two points (a _ i,b _ i) and (c _ i,d _ i) as its endpoints. Here, each line segment includes its endpoints. Additionally, no two line segments share a point.

We want to place a single closed disk in this plane so that it shares a point with each line segment. In other words, we want to draw a single circle so that each line segment shares a point with either the circumference of the circle or its interior (or both). Find the smallest possible radius of such a disk.

Constraints

  • 2\leq N\leq 100
  • 0\leq a _ i,b _ i,c _ i,d _ i\leq1000\ (1\leq i\leq N)
  • (a _ i,b _ i)\neq(c _ i,d _ i)\ (1\leq i\leq N)
  • The i-th and j-th line segments do not share a point (1\leq i\lt j\leq N).
  • All input values are integers.

Input

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

N
a _ 1 b _ 1 c _ 1 d _ 1
a _ 2 b _ 2 c _ 2 d _ 2
\vdots
a _ N b _ N c _ N d _ N

Output

Print the answer in a single line. Your output will be considered correct when the absolute or relative error from the true value is at most 10 ^ {−5}.


Sample Input 1

4
2 3 2 10
4 0 12 6
4 8 6 3
7 8 10 8

Sample Output 1

3.319048676309097923796460081961

The given line segments are shown in the figure below. The closed disk shown in the figure, centered at \left(\dfrac{32-\sqrt{115}}4,\dfrac{21-\sqrt{115}}2\right) with a radius of \dfrac{24-\sqrt{115}}4, shares a point with all the line segments.

It is impossible to place a disk with a radius less than \dfrac{24-\sqrt{115}}4 so that it shares a point with all the line segments, so the answer is \dfrac{24-\sqrt{115}}4.

Your output will be considered correct if the absolute or relative error from the true value is at most 10^{-5}, so outputs such as 3.31908 and 3.31902 would also be considered correct.


Sample Input 2

20
0 18 4 28
2 21 8 21
3 4 10 5
3 14 10 13
5 9 10 12
6 9 10 6
6 28 10 18
12 11 15 13
12 17 12 27
13 17 20 18
13 27 19 26
16 1 16 13
16 22 19 25
17 22 20 19
18 4 23 4
18 5 23 11
22 16 22 23
23 15 30 15
23 24 30 24
24 0 24 11

Sample Output 2

12.875165712523887403637822024952

The closed disk shown in the figure, centered at \left(\dfrac{19817-8\sqrt{5991922}}{18},\dfrac{-2305+\sqrt{5991922}}9\right) with a radius of \dfrac{3757\sqrt{29}-44\sqrt{206618}}{18}, shares a point with all the line segments.


Sample Input 3

30
526 655 528 593
628 328 957 211
480 758 680 794
940 822 657 949
127 23 250 385
281 406 319 305
277 598 190 439
437 450 725 254
970 478 369 466
421 225 348 141
872 64 600 9
634 460 759 337
878 514 447 534
142 237 191 269
983 34 554 284
694 160 589 239
391 631 22 743
377 656 500 606
390 576 184 312
556 707 457 699
796 870 186 773
12 803 505 586
343 541 42 165
478 340 176 2
39 618 6 651
753 883 47 833
551 593 873 672
983 729 338 747
721 77 541 255
0 32 98 597

Sample Output 3

485.264732620930836460637042310401