A - Full House

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

配点 : 100

問題文

5 枚のカードがあり、それぞれのカードには整数 A,B,C,D,E が書かれています。

この 5 枚組は以下の条件を満たすとき、またそのときに限って、フルハウスであると呼ばれます。

  • 同じ数が書かれたカード 3 枚と、別の同じ数が書かれたカード 2 枚からなる。

5 枚組がフルハウスであるか判定してください。

制約

  • 1 \leq A,B,C,D,E\leq 13
  • A,B,C,D,E 全てが同じ値ではない
  • 入力は全て整数

入力

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

A B C D E

出力

フルハウスである場合 Yes を、そうでないとき No を出力せよ。


入力例 1

1 2 1 2 1

出力例 1

Yes

1 が書かれたカード 3 枚と、2 が書かれたカード 2 枚からなるため、これはフルハウスです。


入力例 2

12 12 11 1 2

出力例 2

No

フルハウスの条件を満たしません。

Score : 100 points

Problem Statement

We have five cards with integers A, B, C, D, and E written on them, one on each card.

This set of five cards is called a Full house if and only if the following condition is satisfied:

  • the set has three cards with a same number written on them, and two cards with another same number written on them.

Determine whether the set is a Full house.

Constraints

  • 1 \leq A,B,C,D,E\leq 13
  • Not all of A, B, C, D, and E are the same.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E

Output

If the set is a Full house, print Yes; otherwise, print No.


Sample Input 1

1 2 1 2 1

Sample Output 1

Yes

The set has three cards with 1 written on them and two cards with 2 written on them, so it is a Full house.


Sample Input 2

12 12 11 1 2

Sample Output 2

No

The condition is not satisfied.

B - Ancestor

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

配点 : 200

問題文

N 人の人がいます。N 人の人には人 1,2,\dots,N と番号がついています。

i(2 \le i \le N) の親は人 P_i です。ここで、P_i < i が保証されます。

1 が人 N の何代前か求めてください。

制約

  • 2 \le N \le 50
  • 1 \le P_i < i(2 \le i \le N)
  • 入力は全て整数。

入力

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

N
P_2 P_3 \dots P_N

出力

答えを整数として出力せよ。


入力例 1

3
1 2

出力例 1

2

2 は人 3 の親であるため、人 31 代前です。

1 は人 2 の親であるため、人 32 代前です。

よって解は 2 です。


入力例 2

10
1 2 3 4 5 6 7 8 9

出力例 2

9

Score : 200 points

Problem Statement

There are N people, called Person 1, Person 2, \ldots, Person N.

The parent of Person i (2 \le i \le N) is Person P_i. Here, it is guaranteed that P_i < i.

How many generations away from Person N is Person 1?

Constraints

  • 2 \le N \le 50
  • 1 \le P_i < i(2 \le i \le N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_2 P_3 \dots P_N

Output

Print the answer as a positive integer.


Sample Input 1

3
1 2

Sample Output 1

2

Person 2 is a parent of Person 3, and thus is one generation away from Person 3.

Person 1 is a parent of Person 2, and thus is two generations away from Person 3.

Therefore, the answer is 2.


Sample Input 2

10
1 2 3 4 5 6 7 8 9

Sample Output 2

9
C - Monotonically Increasing

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

配点 : 300

問題文

長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。

注記

ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_NB_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で AB より早いと定義されます。

  • ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。

ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。

  • 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。

制約

  • 1 \le N \le M \le 10
  • 入力は全て整数。

入力

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

N M

出力

条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。


入力例 1

2 3

出力例 1

1 2 
1 3 
2 3 

条件を満たす数列は (1,2),(1,3),(2,3)3 個です。これらを辞書順で早い方から出力します。


入力例 2

3 5

出力例 2

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

Score : 300 points

Problem Statement

Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.

Notes

For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:

  • there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.

An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:

  • A_i < A_{i+1} for all integers i (1 \le i \le N-1).

Constraints

  • 1 \le N \le M \le 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).


Sample Input 1

2 3

Sample Output 1

1 2 
1 3 
2 3 

The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.


Sample Input 2

3 5

Sample Output 2

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 
D - Left Right Operation

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

配点 : 400

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

あなたは以下の連続する操作をちょうど一度だけ行います。

  • 整数 x\ (0\leq x \leq N) を選ぶ。x として 0 を選んだ場合何もしない。 x として 1 以上の整数を選んだ場合、A_1,A_2,\ldots,A_x をそれぞれ L で置き換える。

  • 整数 y\ (0\leq y \leq N) を選ぶ。y として 0 を選んだ場合何もしない。 y として 1 以上の整数を選んだ場合、A_{N},A_{N-1},\ldots,A_{N-y+1} をそれぞれ R で置き換える。

操作後の A の要素の総和として考えられる最小値を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • -10^9 \leq L, R\leq 10^9
  • -10^9 \leq A_i\leq 10^9
  • 入力は全て整数

入力

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

N L R
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

5 4 3
5 5 0 6 3

出力例 1

14

x=2,y=2 として操作をすると、数列 A = (4,4,0,3,3) となり、要素の総和は 14 になります。

これが達成可能な最小値です。


入力例 2

4 10 10
1 2 3 4

出力例 2

10

x=0,y=0 として操作をすると、数列 A = (1,2,3,4) となり、要素の総和は 10 になります。

これが達成可能な最小値です。


入力例 3

10 -5 -3
9 -6 10 -1 2 10 -1 7 -15 5

出力例 3

-58

L,R,A_i は負であることがあります。

Score : 400 points

Problem Statement

You are given an integer sequence of length N: A=(A_1,A_2,\ldots,A_N).

You will perform the following consecutive operations just once:

  • Choose an integer x (0\leq x \leq N). If x is 0, do nothing. If x is 1 or greater, replace each of A_1,A_2,\ldots,A_x with L.

  • Choose an integer y (0\leq y \leq N). If y is 0, do nothing. If y is 1 or greater, replace each of A_{N},A_{N-1},\ldots,A_{N-y+1} with R.

Print the minimum possible sum of the elements of A after the operations.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • -10^9 \leq L, R\leq 10^9
  • -10^9 \leq A_i\leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L R
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

5 4 3
5 5 0 6 3

Sample Output 1

14

If you choose x=2 and y=2, you will get A = (4,4,0,3,3), for the sum of 14, which is the minimum sum achievable.


Sample Input 2

4 10 10
1 2 3 4

Sample Output 2

10

If you choose x=0 and y=0, you will get A = (1,2,3,4), for the sum of 10, which is the minimum sum achievable.


Sample Input 3

10 -5 -3
9 -6 10 -1 2 10 -1 7 -15 5

Sample Output 3

-58

L, R, and A_i may be negative.

E - Sugoroku 3

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

配点 : 500

問題文

マス 1 からマス NN 個のマスがあります。はじめ、あなたはマス 1 にいます。

また、マス 1 からマス N-1 にはそれぞれサイコロが置いてあります。マス i のサイコロは 0 以上 A_i 以下の整数を等確率にランダムで出します。(サイコロを振る操作は毎回独立です。)

あなたは、マス N に到達するまで、現在いるマスに置かれているサイコロを振り、出た目の数だけ進むことを繰り返します。厳密に言うと、マス X にいるときにサイコロで Y が出た場合はマス X+Y に移動します。

サイコロを振る回数の期待値 \bmod\ 998244353 を求めてください。

注記

求める期待値は必ず有理数となることが証明できます。またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ一つ存在することが証明できます。この R を求めてください。

制約

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • 入力は全て整数。

入力

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

N
A_1 A_2 \dots A_{N-1}

出力

答えを出力せよ。


入力例 1

3
1 1

出力例 1

4

求める期待値は 4 であるため、4 を出力します。

マス N に到達するまでの流れとしては、以下のようなものが考えられます。

  • マス 11 を出し、マス 2 に移動する。
  • マス 20 を出し、移動しない。
  • マス 21 を出し、マス 3 に移動する。

このようになる確率は \frac{1}{8} です。


入力例 2

5
3 1 2 1

出力例 2

332748122

Score : 500 points

Problem Statement

There are N squares called Square 1 though Square N. You start on Square 1.

Each of the squares from Square 1 through Square N-1 has a die on it. The die on Square i is labeled with the integers from 0 through A_i, each occurring with equal probability. (Die rolls are independent of each other.)

Until you reach Square N, you will repeat rolling a die on the square you are on. Here, if the die on Square x rolls the integer y, you go to Square x+y.

Find the expected value, modulo 998244353, of the number of times you roll a die.

Notes

It can be proved that the sought expected value is always a rational number. Additionally, if that value is represented \frac{P}{Q} using two coprime integers P and Q, there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Find this R.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 1 \le A_i \le N-i(1 \le i \le N-1)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_{N-1}

Output

Print the answer.


Sample Input 1

3
1 1

Sample Output 1

4

The sought expected value is 4, so 4 should be printed.

Here is one possible scenario until reaching Square N:

  • Roll 1 on Square 1, and go to Square 2.
  • Roll 0 on Square 2, and stay there.
  • Roll 1 on Square 2, and go to Square 3.

This scenario occurs with probability \frac{1}{8}.


Sample Input 2

5
3 1 2 1

Sample Output 2

332748122
F - Tournament

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

配点 : 500

問題文

1 から 2^N の番号がついた 2^N 人でじゃんけん大会を行います。

大会は以下の形式で行われます。

  • 参加者を人 1、人 2\ldots、人 2^N の順に横一列に並べる。
  • 現在の列の長さを 2M として、各 i\ (1\leq i \leq M) について左から 2i-1 番目の人と左から 2i 番目の人で試合を行い、負けた M 人を列から外す。これを N 回繰り返す。

ここで、人 i はちょうど j 回試合に勝つと C_{i,j} 円獲得できます。1 回も勝たなかった場合は何も貰えません。全ての試合の勝敗を自由に決められるとき、人 1、人 2\ldots、人 2^N が貰えるお金の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 16
  • 1 \leq C_{i,j} \leq 10^9
  • 入力は全て整数

入力

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

N 
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
\vdots
C_{2^N,1} C_{2^N,2} \ldots C_{2^N,N}

出力

答えを出力せよ。


入力例 1

2
2 5
6 5
2 1
7 9

出力例 1

15

初めの列は (1,2,3,4) です。

1 と人 2 の試合で人 2 が勝ち、人 3 と人 4 の試合で人 4 が勝ったとすると、列は (2,4) になります。

次に人 2 と人 4 の試合で人 4 が勝ったとすると、列は (4) になり、これで大会が終了となります。

このとき、人 2 はちょうど 1 回勝ち、人 4 はちょうど 2 回勝ったので、貰えるお金の合計は 0+6+0+9=15 となります。 これが貰えるお金の合計の最大値です。


入力例 2

3
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

出力例 2

4

Score : 500 points

Problem Statement

2^N people, numbered 1 to 2^N, will participate in a rock-paper-scissors tournament.

The tournament proceeds as follows:

  • The participants are arranged in a row in the order Person 1, Person 2, \ldots, Person 2^N from left to right.
  • Let 2M be the current length of the row. For each i\ (1\leq i \leq M), the (2i-1)-th and (2i)-th persons from the left play a game against each other. Then, the M losers are removed from the row. This process is repeated N times.

Here, if Person i wins exactly j games, they receive C_{i,j} yen (Japanese currency). A person winning zero games receives nothing. Find the maximum possible total amount of money received by the 2^N people if the results of all games can be manipulated freely.

Constraints

  • 1 \leq N \leq 16
  • 1 \leq C_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N 
C_{1,1} C_{1,2} \ldots C_{1,N}
C_{2,1} C_{2,2} \ldots C_{2,N}
\vdots
C_{2^N,1} C_{2^N,2} \ldots C_{2^N,N}

Output

Print the answer.


Sample Input 1

2
2 5
6 5
2 1
7 9

Sample Output 1

15

The initial row of the people is (1,2,3,4).

If Person 2 wins the game against Person 1, and Person 4 wins the game against Person 3, the row becomes (2,4).

Then, if Person 4 wins the game against Person 2, the row becomes (4), and the tournament ends.

Here, Person 2 wins exactly 1 game, and Person 4 wins exactly 2 games, so they receive 0+6+0+9=15 yen in total, which is the maximum possible sum.


Sample Input 2

3
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

Sample Output 2

4
G - Erasing Prime Pairs

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

配点 : 600

問題文

黒板に N 種類の整数が書かれています。 i 種類目の整数は A_i で、書かれている個数は B_i 個です。

あなたは次の操作を可能な限り繰り返すことができます。

  • 黒板に書かれている 2 個の整数 x,y であって、x+y が素数であるものを選ぶ。 選んだ 2 個の整数を黒板から消す。

操作を最大で何回行えるか求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^7
  • 1 \leq B_i \leq 10^9
  • A_i は全て異なる
  • 入力は全て整数

入力

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

N 
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

答えを出力せよ。


入力例 1

3
3 3
2 4
6 2

出力例 1

3

2 + 3 = 5 であり、5 は素数なので、23 を選んで消す操作が行えます。また、それ以外の操作は行えません。 24 個、 33 個あるので、操作を 3 回行うことができます。


入力例 2

1
1 4

出力例 2

2

1+ 1 = 2 であり、2 は素数なので、11 を選んで消す操作が行えます。14 個あるので、操作を 2 回行うことができます。

Score : 600 points

Problem Statement

There are integers with N different values written on a blackboard. The i-th value is A_i and is written B_i times.

You may repeat the following operation as many times as possible:

  • Choose two integers x and y written on the blackboard such that x+y is prime. Erase these two integers.

Find the maximum number of times the operation can be performed.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 10^7
  • 1 \leq B_i \leq 10^9
  • All A_i are distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N 
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer.


Sample Input 1

3
3 3
2 4
6 2

Sample Output 1

3

We have 2 + 3 = 5, and 5 is prime, so you can choose 2 and 3 to erase them, but nothing else. Since there are four 2s and three 3s, you can do the operation three times.


Sample Input 2

1
1 4

Sample Output 2

2

We have 1 + 1 = 2, and 2 is prime, so you can choose 1 and 1 to erase them. Since there are four 1s, you can do the operation twice.

Ex - Intersection 2

実行時間制限: 7 sec / メモリ制限: 1024 MB

配点 : 600

問題文

2 次元平面上に N 本の直線があります。i 本目の直線は A_i x + B_i y + C_i = 0 です。どの 2 本の直線も平行でないことが保証されます。

これらの直線の交点は、重複ありで \frac{N(N-1)}{2} 個ありますが、このうち原点から K 番目に近い点の原点との距離を出力してください。

制約

  • 2 \le N \le 5 \times 10^4
  • 1 \le K \le \frac{N(N-1)}{2}
  • -1000 \le |A_i|,|B_i|,|C_i| \le 1000(1 \le i \le N)
  • どの 2 本の直線も平行でない。
  • A_i \neq 0 または B_i \neq 0(1 \le i \le N)
  • 入力は全て整数。

入力

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

N K
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

出力

答えを表す数値を出力せよ。

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


入力例 1

3 2
1 1 1
2 1 -3
1 -1 2

出力例 1

2.3570226040

i 本目の直線を直線 i ということとします。

  • 直線 1 と直線 2 の交点は (4,-5) であり、原点との距離は \sqrt{41} \simeq 6.4031242374 です。
  • 直線 1 と直線 3 の交点は (\frac{-3}{2},\frac{1}{2}) であり、原点との距離は \frac{\sqrt{10}}{2} \simeq 1.5811388300 です。
  • 直線 2 と直線 3 の交点は (\frac{1}{3},\frac{7}{3}) であり、原点との距離は \frac{5\sqrt{2}}{3} \simeq 2.3570226040 です。

よって、2 番目に原点に近い点は (\frac{1}{3},\frac{7}{3}) であり、出力する値は \frac{5\sqrt{2}}{3} です。


入力例 2

6 7
5 1 9
4 4 -3
8 -1 2
0 1 -8
4 0 -4
2 -3 0

出力例 2

4.0126752298

Score : 600 points

Problem Statement

There are N lines in a two-dimensional plane. The i-th line is A_i x + B_i y + C_i = 0. It is guaranteed that no two of the lines are parallel.

In this plane, there are \frac{N(N-1)}{2} intersection points of two lines, including duplicates. Print the distance between the origin and the K-th nearest point to the origin among these \frac{N(N-1)}{2} points.

Constraints

  • 2 \le N \le 5 \times 10^4
  • 1 \le K \le \frac{N(N-1)}{2}
  • -1000 \le |A_i|,|B_i|,|C_i| \le 1000(1 \le i \le N)
  • No two of the lines are parallel.
  • A_i \neq 0 or B_i \neq 0(1 \le i \le N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

Output

Print a real number representing the answer.

Your output is considered correct when its absolute or relative error from the judge's output is at most 10^{-4}.


Sample Input 1

3 2
1 1 1
2 1 -3
1 -1 2

Sample Output 1

2.3570226040

Let us call the i-th line Line i.

  • The intersection point of Line 1 and Line 2 is (4,-5), whose distance to the origin is \sqrt{41} \simeq 6.4031242374.
  • The intersection point of Line 1 and Line 3 is (\frac{-3}{2},\frac{1}{2}), whose distance to the origin is \frac{\sqrt{10}}{2} \simeq 1.5811388300.
  • The intersection point of Line 2 and Line 3 is (\frac{1}{3},\frac{7}{3}), whose distance to the origin is \frac{5\sqrt{2}}{3} \simeq 2.3570226040.

Therefore, the second nearest intersection point is (\frac{1}{3},\frac{7}{3}), and \frac{5\sqrt{2}}{3} should be printed.


Sample Input 2

6 7
5 1 9
4 4 -3
8 -1 2
0 1 -8
4 0 -4
2 -3 0

Sample Output 2

4.0126752298