C - Qualification Contest

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

配点 : 200

問題文

N 人の人があるコンテストに参加し、i 位の人のハンドルネームは S_i でした。
上位 K 人のハンドルネームを辞書順に出力してください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。

以下では「 Si 文字目の文字」を S_i のように表します。また、 ST より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. ST のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_iT_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_jT_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、 ST の長さを比較して、ST より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • 1 \leq K \leq N \leq 100
  • K, N は整数
  • S_i は英小文字からなる長さ 10 以下の文字列
  • i \neq j ならば S_i \neq S_j

入力

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

N K
S_1
S_2
\vdots
S_N

出力

答えを改行区切りで出力せよ。


入力例 1

5 3
abc
aaaaa
xyz
a
def

出力例 1

aaaaa
abc
xyz

このコンテストには 5 人が参加し、1 位の人のハンドルネームは abc2 位の人のハンドルネームは aaaaa3 位の人のハンドルネームは xyz4 位の人のハンドルネームは a5 位の人のハンドルネームは def でした。

上位 3 人のハンドルネームは abcaaaaaxyz であるため、これを辞書順に並べ替えて aaaaaabcxyz の順に出力します。


入力例 2

4 4
z
zyx
zzz
rbg

出力例 2

rbg
z
zyx
zzz

入力例 3

3 1
abc
arc
agc

出力例 3

abc

Score : 200 points

Problem Statement

There were N participants in a contest. The participant ranked i-th had the nickname S_i.
Print the nicknames of the top K participants in lexicographical order.

What is lexicographical order?

Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings S and T.

Let S_i denote the i-th character of a string S. We write S \lt T if S is lexicographically smaller than T, and S \gt T if S is larger.

  1. Let L be the length of the shorter of S and T. For i=1,2,\dots,L, check whether S_i equals T_i.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, we get S \lt T; if S_j is larger, we get S \gt T.
  3. If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, we get S \lt T; if S is longer, we get S \gt T.

Constraints

  • 1 \leq K \leq N \leq 100
  • K and N are integers.
  • S_i is a string of length 10 consisting of lowercase English letters.
  • S_i \neq S_j if i \neq j.

Input

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

N K
S_1
S_2
\vdots
S_N

Output

Print the nicknames, separated by newlines.


Sample Input 1

5 3
abc
aaaaa
xyz
a
def

Sample Output 1

aaaaa
abc
xyz

This contest had five participants. The participants ranked first, second, third, fourth, and fifth had the nicknames abc, aaaaa, xyz, a, and def, respectively.

The nicknames of the top three participants were abc, aaaaa, xyz, so print these in lexicographical order: aaaaa, abc, xyz.


Sample Input 2

4 4
z
zyx
zzz
rbg

Sample Output 2

rbg
z
zyx
zzz

Sample Input 3

3 1
abc
arc
agc

Sample Output 3

abc
D - Practical Computing

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

配点 : 200

問題文

以下のような N 個の整数列 A_0,\ldots,A_{N-1} を求めてください。

  • i (0\leq i \leq N-1) について、A_i の長さは i+1 である。
  • i,j (0\leq i \leq N-1, 0 \leq j \leq i) について、A_ij+1 番目の値 a_{i,j} は次のように定められる。

    • j=0 または j=i の時、a_{i,j}=1
    • それ以外の時、a_{i,j} = a_{i-1,j-1} + a_{i-1,j}

制約

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

入力

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

N

出力

N 行出力せよ。 i 行目には A_{i-1} の値を順に空白区切りで出力せよ。


入力例 1

3

出力例 1

1
1 1
1 2 1

入力例 2

10

出力例 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Score : 200 points

Problem Statement

Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows.

  • For each i (0\leq i \leq N-1), the length of A_i is i+1.
  • For each i and j (0\leq i \leq N-1, 0 \leq j \leq i), the (j+1)-th term of A_i, denoted by a_{i,j}, is defined as follows.
    • a_{i,j}=1, if j=0 or j=i.
    • a_{i,j} = a_{i-1,j-1} + a_{i-1,j}, otherwise.

Constraints

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

Input

Input is given from Standard Input in the following format:

N

Output

Print N lines. The i-th line should contain the terms of A_{i-1} separated by spaces.


Sample Input 1

3

Sample Output 1

1
1 1
1 2 1

Sample Input 2

10

Sample Output 2

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
E - Filling 3x3 array

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

配点 : 300

問題文

6 個の整数 h_1, h_2, h_3, w_1, w_2, w_3 が与えられます。
縦横 3 \times 3 のマス目に、以下の条件をすべて満たすように各マスに正の整数を 1 つずつ書きこむことを考えます。

  • i=1,2,3 について、上から i 行目に書きこんだ数の和が h_i になる。
  • j=1,2,3 について、左から j 列目に書きこんだ数の和が w_j になる。

例えば (h_1, h_2, h_3) = (5, 13, 10), (w_1, w_2, w_3) = (6, 13, 9) のとき、以下の 3 通りの書きこみ方はすべて条件を満たしています。(条件を満たす書きこみ方は他にもあります)

image

さて、条件を満たす書きこみ方は全部で何通り存在しますか?

制約

  • 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
  • 入力される値はすべて整数

入力

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

h_1 h_2 h_3 w_1 w_2 w_3

出力

条件を満たす書きこみ方が何通りあるかを出力せよ。


入力例 1

3 4 6 3 3 7

出力例 1

1

条件を満たす数の書きこみ方は次の 1 通りのみです。よって 1 を出力します。

image2


入力例 2

3 4 5 6 7 8

出力例 2

0

条件を満たす書きこみ方が存在しないこともあります。


入力例 3

5 13 10 6 13 9

出力例 3

120

入力例 4

20 25 30 22 29 24

出力例 4

30613

Score : 300 points

Problem Statement

You are given six integers: h_1, h_2, h_3, w_1, w_2, and w_3.
Consider writing a positive integer on each square of a 3 \times 3 grid so that all of the following conditions are satisfied:

  • For i=1,2,3, the sum of numbers written in the i-th row from the top is h_i.
  • For j=1,2,3, the sum of numbers written in the j-th column from the left is w_i.

For example, if (h_1, h_2, h_3) = (5, 13, 10) and (w_1, w_2, w_3) = (6, 13, 9), then all of the following three ways satisfy the conditions. (There are other ways to satisfy the conditions.)

image

How many ways are there to write numbers to satisfy the conditions?

Constraints

  • 3 \leq h_1, h_2, h_3, w_1, w_2, w_3 \leq 30
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

h_1 h_2 h_3 w_1 w_2 w_3

Output

Print the number of ways to write numbers to satisfy the conditions.


Sample Input 1

3 4 6 3 3 7

Sample Output 1

1

The following is the only way to satisfy the conditions. Thus, 1 should be printed.

image2


Sample Input 2

3 4 5 6 7 8

Sample Output 2

0

There may not be a way to satisfy the conditions.


Sample Input 3

5 13 10 6 13 9

Sample Output 3

120

Sample Input 4

20 25 30 22 29 24

Sample Output 4

30613
F - False Hope

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

配点 : 300

問題文

3\times3 のマス目に 1 から 9 までの数字が書き込まれており、上から i 行目、左から j 列目 (1\leq i\leq3,1\leq j\leq3) に書き込まれている数字は c _ {i,j} です。

異なるマスに同じ数字が書き込まれている場合もありますが、同じ数字が縦・横・斜めに 3 つ連続して書き込まれていることはありません。 より厳密には、c _ {i,j} について次の条件のすべてが成り立っていることが保証されます。

  • どの 1\leq i\leq3 についても、c _ {i,1}=c _ {i,2}=c _ {i,3} ではない
  • どの 1\leq j\leq3 についても、c _ {1,j}=c _ {2,j}=c _ {3,j} ではない
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {3,1}=c _ {2,2}=c _ {1,3} ではない

高橋くんは、それぞれのマスに書かれている数字をランダムな順番で知ります。 高橋くんは、縦・横・斜めの列のうちの 1 つでも次の条件を満たしたときがっかりします。

  • はじめに知ったほうの 2 マスに書かれた数字が同じであり、最後に知ったマスに書かれた数字がそれと異なる。

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を求めてください。

制約

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} ではない (1\leq i\leq3)
  • c _ {1,j}=c _ {2,j}=c _ {3,j} ではない (1\leq j\leq3)
  • c _ {1,1}=c _ {2,2}=c _ {3,3} ではない
  • c _ {1,3}=c _ {2,2}=c _ {3,1} ではない

入力

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

出力

高橋くんががっかりせずにすべてのマスに書かれた数字を知る確率を 1 行で出力せよ。 真の値からの絶対誤差が 10 ^ {-8} 以下であるとき、正答と判定される。


入力例 1

3 1 9
2 5 6
2 7 1

出力例 1

0.666666666666666666666666666667

例えば、高橋くんが c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 の順に知った場合、高橋くんはがっかりしてしまいます。

対して、高橋くんが c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} の順に数字を知った場合、がっかりすることなくすべての数字を知ることができます。

高橋くんががっかりすることなくすべての数字を知ることができる確率は \dfrac 23 です。 絶対誤差が 10 ^ {-8} 以下であれば正答と判定されるため、0.6666666570.666666676 のように出力しても正解になります。


入力例 2

7 7 6
8 6 8
7 7 6

出力例 2

0.004982363315696649029982363316

入力例 3

3 6 7
1 9 7
5 7 5

出力例 3

0.4

Score : 300 points

Problem Statement

There is a 3\times3 grid with numbers between 1 and 9, inclusive, written in each square. The square at the i-th row from the top and j-th column from the left (1\leq i\leq3,1\leq j\leq3) contains the number c _ {i,j}.

The same number may be written in different squares, but not in three consecutive cells vertically, horizontally, or diagonally. More precisely, it is guaranteed that c _ {i,j} satisfies all of the following conditions.

  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Takahashi will see the numbers written in each cell in random order. He will get disappointed when there is a line (vertical, horizontal, or diagonal) that satisfies the following condition.

  • The first two squares he sees contain the same number, but the last square contains a different number.

Find the probability that Takahashi sees the numbers in all the squares without getting disappointed.

Constraints

  • c _ {i,j}\in\lbrace1,2,3,4,5,6,7,8,9\rbrace\ (1\leq i\leq3,1\leq j\leq3)
  • c _ {i,1}=c _ {i,2}=c _ {i,3} does not hold for any 1\leq i\leq3.
  • c _ {1,j}=c _ {2,j}=c _ {3,j} does not hold for any 1\leq j\leq3.
  • c _ {1,1}=c _ {2,2}=c _ {3,3} does not hold.
  • c _ {3,1}=c _ {2,2}=c _ {1,3} does not hold.

Input

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

c _ {1,1} c _ {1,2} c _ {1,3}
c _ {2,1} c _ {2,2} c _ {2,3}
c _ {3,1} c _ {3,2} c _ {3,3}

Output

Print one line containing the probability that Takahashi sees the numbers in all the squares without getting disappointed. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}.


Sample Input 1

3 1 9
2 5 6
2 7 1

Sample Output 1

0.666666666666666666666666666667

For example, if Takahashi sees c _ {3,1}=2,c _ {2,1}=2,c _ {1,1}=3 in this order, he will get disappointed.

On the other hand, if Takahashi sees c _ {1,1},c _ {1,2},c _ {1,3},c _ {2,1},c _ {2,2},c _ {2,3},c _ {3,1},c _ {3,2},c _ {3,3} in this order, he will see all numbers without getting disappointed.

The probability that Takahashi sees all the numbers without getting disappointed is \dfrac 23. Your answer will be considered correct if the absolute error from the true value is at most 10 ^ {-8}, so outputs such as 0.666666657 and 0.666666676 would also be accepted.


Sample Input 2

7 7 6
8 6 8
7 7 6

Sample Output 2

0.004982363315696649029982363316

Sample Input 3

3 6 7
1 9 7
5 7 5

Sample Output 3

0.4
G - Hidden Weights

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

配点 : 400

問題文

N 頂点 M 辺の有向グラフが与えられます。j 番目の有向辺は頂点 u_j から頂点 v_j に向かっており、重み w_j を持っています。

各頂点に -10^{18} 以上 10^{18} 以下の整数を書き込む方法であって、次の条件を満たすものを 1 つ見つけてください。

  • 頂点 i に書き込まれている値を x_i とする。すべての辺 j=1,2,\dots,M について、x_{v_j} - x_{u_j} = w_j が成り立つ。

与えられる入力について、条件を満たす書き込み方が少なくとも 1 つ存在することが保証されます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq \min\{2 \times 10^5,N(N-1)/2\}
  • 1 \leq u_j, v_j \leq N
  • u_j \neq v_j
  • i \neq j なら (u_i,v_i) \neq (u_j,v_j) かつ (u_i,v_i) \neq (v_j,u_j)
  • |w_j| \leq 10^9
  • 入力はすべて整数
  • 条件を満たす書き込み方が少なくとも 1 つ存在する

入力

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

出力

頂点 i に書き込む整数を x_i として、x_1,x_2,\dots,x_N をこの順に空白区切りで 1 行で出力せよ。答えが複数ある場合、どれを出力しても良い。


入力例 1

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

出力例 1

3 5 2

x=(3,5,2) とすることで、x_2-x_1=w_1=2,x_2-x_3=w_2=3,x_3-x_1=w_3=-1 となり、条件を満たします。

他にも、たとえば x=(-1,1,-2) としても正解となります。


入力例 2

4 2
2 1 5
3 4 -3

出力例 2

5 0 6 3

他にも、たとえば x=(0,-5,4,1)x=(5,0,4,1) としても正解となります。


入力例 3

5 7
2 1 18169343
3 1 307110901
4 1 130955934
2 3 -288941558
2 5 96267410
5 3 -385208968
4 3 -176154967

出力例 3

200401298 182231955 -106709603 69445364 278499365

Score : 400 points

Problem Statement

You are given a directed graph with N vertices and M edges. The j-th directed edge goes from vertex u_j to vertex v_j and has a weight of w_j.

Find one way to write an integer between -10^{18} and 10^{18}, inclusive, to each vertex such that the following condition is satisfied.

  • Let x_i be the value written on vertex i. For all edges j=1,2,\dots,M, it holds that x_{v_j} - x_{u_j} = w_j.

It is guaranteed that at least one such assignment exists for the given input.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq \min\{2 \times 10^5,N(N-1)/2\}
  • 1 \leq u_j, v_j \leq N
  • u_j \neq v_j
  • If i \neq j, then (u_i, v_i) \neq (u_j, v_j) and (u_i, v_i) \neq (v_j, u_j)
  • |w_j| \leq 10^9
  • All input values are integers.
  • There exists at least one assignment satisfying the conditions.

Input

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

N M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_M v_M w_M

Output

Let x_i be the integer written on vertex i. Print x_1, x_2, \dots, x_N in this order, separated by spaces, on a single line. If there are multiple solutions, you may print any of them.


Sample Input 1

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

Sample Output 1

3 5 2

By setting x = (3, 5, 2), we have x_2 - x_1 = w_1 = 2, x_2 - x_3 = w_2 = 3, x_3 - x_1 = w_3 = -1, satisfying the conditions.

For example, x = (-1, 1, -2) is also a valid answer.


Sample Input 2

4 2
2 1 5
3 4 -3

Sample Output 2

5 0 6 3

For example, x = (0, -5, 4, 1) and x = (5, 0, 4, 1) are also valid answers.


Sample Input 3

5 7
2 1 18169343
3 1 307110901
4 1 130955934
2 3 -288941558
2 5 96267410
5 3 -385208968
4 3 -176154967

Sample Output 3

200401298 182231955 -106709603 69445364 278499365