A - Falling Asleep

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 200

問題文

ニワンゴ君は N 曲からなるプレイリストを作りました。 i 番目の曲はタイトルが s_i で、再生時間が t_i 秒です。 ここで、s_1,\ldots,s_N が相異なることが保証されます。

ニワンゴ君はこのプレイリストを再生しながら、作業をしていました。(すなわち、プレイリストの全曲が順番通りに 1 回ずつ、間に休止を挟むことなく再生されました。) しかし、作業中に居眠りをしてしまい、全曲の再生が終わったあとに起きました。 記録から、タイトルが X の曲の再生が終わるタイミングでニワンゴ君が寝てしまったことがわかりました。

ニワンゴ君が寝ている間に曲が再生された時間が何秒あったかを求めてください。

制約

  • 1 \leq N \leq 50
  • s_i,X は英小文字のみからなる長さ 1 以上 100 以下の文字列
  • s_1,\ldots,s_N は相異なる
  • s_i = X なる整数 i が存在する
  • 1 \leq t_i \leq 1000
  • t_i は整数

入力

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

N
s_1 t_1
\vdots
s_{N} t_N
X

出力

答えを出力せよ。


入力例 1

3
dwango 2
sixth 5
prelims 25
dwango

出力例 1

30
  • ニワンゴ君が寝ている間に再生された曲は sixthprelims です。
  • これらの再生時間の総和である 30 が答えです。

入力例 2

1
abcde 1000
abcde

出力例 2

0
  • ニワンゴ君が寝ている間に再生された曲はありません。
  • 再生時間の総和は 0 となります。

入力例 3

15
ypnxn 279
kgjgwx 464
qquhuwq 327
rxing 549
pmuduhznoaqu 832
dagktgdarveusju 595
wunfagppcoi 200
dhavrncwfw 720
jpcmigg 658
wrczqxycivdqn 639
mcmkkbnjfeod 992
htqvkgkbhtytsz 130
twflegsjz 467
dswxxrxuzzfhkp 989
szfwtzfpnscgue 958
pmuduhznoaqu

出力例 3

6348

Score : 200 points

Problem Statement

Niwango created a playlist of N songs. The title and the duration of the i-th song are s_i and t_i seconds, respectively. It is guaranteed that s_1,\ldots,s_N are all distinct.

Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, without any pause in between.) However, he fell asleep during his work, and he woke up after all the songs were played. According to his record, it turned out that he fell asleep at the very end of the song titled X.

Find the duration of time when some song was played while Niwango was asleep.

Constraints

  • 1 \leq N \leq 50
  • s_i and X are strings of length between 1 and 100 (inclusive) consisting of lowercase English letters.
  • s_1,\ldots,s_N are distinct.
  • There exists an integer i such that s_i = X.
  • 1 \leq t_i \leq 1000
  • t_i is an integer.

Input

Input is given from Standard Input in the following format:

N
s_1 t_1
\vdots
s_{N} t_N
X

Output

Print the answer.


Sample Input 1

3
dwango 2
sixth 5
prelims 25
dwango

Sample Output 1

30
  • While Niwango was asleep, two songs were played: sixth and prelims.
  • The answer is the total duration of these songs, 30.

Sample Input 2

1
abcde 1000
abcde

Sample Output 2

0
  • No songs were played while Niwango was asleep.
  • In such a case, the total duration of songs is 0.

Sample Input 3

15
ypnxn 279
kgjgwx 464
qquhuwq 327
rxing 549
pmuduhznoaqu 832
dagktgdarveusju 595
wunfagppcoi 200
dhavrncwfw 720
jpcmigg 658
wrczqxycivdqn 639
mcmkkbnjfeod 992
htqvkgkbhtytsz 130
twflegsjz 467
dswxxrxuzzfhkp 989
szfwtzfpnscgue 958
pmuduhznoaqu

Sample Output 3

6348
B - Fusing Slimes

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 600

問題文

数直線上に N 匹のスライムが並んでいます。 左から i 番目のスライムは位置 x_i にいます。

ここで、1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9} が成立することが保証されます。

ニワンゴ君は操作を N-1 回行います。i 回目の操作は以下の手順からなります。

  • 1 以上 N-i 以下の整数を等確率で選ぶ(これを k とする)
  • 左から k 番目にいるスライムを右隣にいるスライムの位置まで移動させる
  • その後、同じ位置にいる 2 匹のスライムを合体させ、1 匹のスライムにする

N-1 回の操作によって、スライムが移動した距離の総和の期待値に (N-1)! をかけた値(これは整数になることが示せます)を 10^{9}+7 で割ったあまりを求めてください。なお、合体後のスライムが移動した場合は 1 体のスライムの移動として数えます。

制約

  • 2 \leq N \leq 10^{5}
  • 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}
  • x_i は整数

部分点

  • N \leq 2000 であるようなテストケースすべてに正解すると、400 点が与えられる。

入力

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

N
x_1 x_2 \ldots x_N

出力

答えを出力せよ。


入力例 1

3
1 2 3

出力例 1

5
  • 確率 \frac{1}{2} で最初に左から 1 番目のスライムが選ばれ、このときの移動距離の総和は 2 となります。
  • 確率 \frac{1}{2} で最初に左から 2 番目のスライムが選ばれ、このときの移動距離の総和は 3 となります。
  • 移動距離の総和の期待値である 2.52! をかけた値である 5 が答えとなります。

入力例 2

12
161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932

出力例 2

750927044
  • 期待値の (N-1)! 倍を 10^9+7 で割ったあまりを求めてください。

Score : 600 points

Problem Statement

There are N slimes standing on a number line. The i-th slime from the left is at position x_i.

It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}.

Niwango will perform N-1 operations. The i-th operation consists of the following procedures:

  • Choose an integer k between 1 and N-i (inclusive) with equal probability.
  • Move the k-th slime from the left, to the position of the neighboring slime to the right.
  • Fuse the two slimes at the same position into one slime.

Find the total distance traveled by the slimes multiplied by (N-1)! (we can show that this value is an integer), modulo (10^{9}+7). If a slime is born by a fuse and that slime moves, we count it as just one slime.

Constraints

  • 2 \leq N \leq 10^{5}
  • 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}
  • x_i is an integer.

Subtasks

  • 400 points will be awarded for passing the test cases satisfying N \leq 2000.

Input

Input is given from Standard Input in the following format:

N
x_1 x_2 \ldots x_N

Output

Print the answer.


Sample Input 1

3
1 2 3

Sample Output 1

5
  • With probability \frac{1}{2}, the leftmost slime is chosen in the first operation, in which case the total distance traveled is 2.
  • With probability \frac{1}{2}, the middle slime is chosen in the first operation, in which case the total distance traveled is 3.
  • The answer is the expected total distance traveled, 2.5, multiplied by 2!, which is 5.

Sample Input 2

12
161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932

Sample Output 2

750927044
  • Find the expected value multiplied by (N-1)!, modulo (10^9+7).
C - Cookie Distribution

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 800

問題文

N 人の子供たちがいます。子供たちには 1,2,\ldots,N と番号が振られています。 これから K 日間、子供たちにクッキーが配られることになりました。 i 日目には N 人の中から a_i 人の子供が等確率で選ばれ、選ばれた子供たちはそれぞれクッキーを 1 枚受け取ります。(K 回の子供の選択はすべて独立に行われます。)

K 日間で子供 i が受け取るクッキーの枚数を c_i として、子供たちの うれしさc_1 \times c_2 \times \ldots \times c_N で定義します。 うれしさの期待値に \binom{N}{a_1} \times \binom{N}{a_2} \times \ldots \times \binom{N}{a_K} をかけた値(これは整数となることが示せます)を 10^9+7 で割ったあまりを求めてください。

注記

\binom{n}{k} は異なる n 個の対象から k 個を選ぶ選び方の総数を表します。

制約

  • 1 \leq N \leq 1000
  • 1 \leq K \leq 20
  • 1 \leq a_i \leq N

入力

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

N K
a_1 a_2 \ldots a_K

出力

答えを出力せよ。


入力例 1

3 2
3 2

出力例 1

12
  • 1 日目では、子供 1,2,3 のいずれもクッキーを受け取ります。
  • 2 日目では、子供 1,2,3 のいずれか 1 人がクッキーを受け取りません。
  • どの場合もうれしさは 4 のため、うれしさの期待値は 4 となります。これに \binom{3}{3} \times \binom{3}{2} をかけた値である 12 を出力してください。

入力例 2

856 16
399 263 665 432 206 61 784 548 422 313 848 478 827 26 398 63

出力例 2

337587117
  • 期待値の \binom{N}{a_1} \times \binom{N}{a_2} \times \ldots \times \binom{N}{a_K} 倍を 10^9+7 で割ったあまりを求めてください。

Score : 800 points

Problem Statement

There are N children, numbered 1,2,\ldots,N. In the next K days, we will give them some cookies. In the i-th day, we will choose a_i children among the N with equal probability, and give one cookie to each child chosen. (We make these K choices independently.)

Let us define the happiness of the children as c_1 \times c_2 \times \ldots \times c_N, where c_i is the number of cookies received by Child i in the K days. Find the expected happiness multiplied by \binom{N}{a_1} \times \binom{N}{a_2} \times \ldots \times \binom{N}{a_K} (we can show that this value is an integer), modulo (10^{9}+7).

Notes

\binom{n}{k} denotes the number of possible choices of k objects out of given distinct n objects, disregarding order.

Constraints

  • 1 \leq N \leq 1000
  • 1 \leq K \leq 20
  • 1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_K

Output

Print the answer.


Sample Input 1

3 2
3 2

Sample Output 1

12
  • On the first day, all the children 1, 2, and 3 receive one cookie.
  • On the second day, two out of the three children 1, 2, and 3 receive one cookie.
  • Their happiness is 4 in any case, so the expected happiness is 4. Print this value multiplied by \binom{3}{3} \times \binom{3}{2}, which is 12.

Sample Input 2

856 16
399 263 665 432 206 61 784 548 422 313 848 478 827 26 398 63

Sample Output 2

337587117
  • Find the expected value multiplied by \binom{N}{a_1} \times \binom{N}{a_2} \times \ldots \times \binom{N}{a_K}, modulo (10^9+7).
D - Arrangement

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 800

問題文

ニワンゴ君は N 枚のカードを持っています。カードには 1,2,\ldots,N と番号が振られています。 ニワンゴ君はこれらのカードを一列に並べることにしました。

ニワンゴ君は以下の N 個の条件の全てを満たすカードの並べ方が存在するかどうかを知りたいです。 ニワンゴ君のためにそのような並べ方が存在するかどうかを判定し、存在する場合は辞書順最小の並べ方を求めてください。

  • カード 1 の右隣のカードは(存在するならば) a_1 でない
  • カード 2 の右隣のカードは(存在するならば) a_2 でない
  • \vdots
  • カード N の右隣のカードは(存在するならば) a_N でない

制約

  • 2 \leq N \leq 10^{5}
  • 1 \leq a_i \leq N
  • a_i \neq i

入力

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

N
a_1 a_2 \ldots a_N

出力

条件を満たす並べ方が存在しない場合は -1 を、存在する場合は条件を満たす辞書順最小のカードの並びを下記のフォーマットで出力せよ。 ここで、b_i は左から i 番目のカードの番号である。

b_1 b_2 \ldots b_N

入力例 1

4
2 3 4 1

出力例 1

1 3 2 4
  • (1,3,2,4) よりも辞書順で小さい並べ方は (1,2,3,4) がありますが、これはカード 1 の右隣のカードは 2 でない、という条件に反するため不適切です。

入力例 2

2
2 1

出力例 2

-1
  • 条件を満たす並べ方が存在しない場合は -1 を出力してください。

入力例 3

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

出力例 3

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

Score : 800 points

Problem Statement

Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.

Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.

  • To the immediate right of Card 1 (if any) is NOT Card a_1.
  • To the immediate right of Card 2 (if any) is NOT Card a_2.
  • \vdots
  • To the immediate right of Card N (if any) is NOT Card a_N.

Constraints

  • 2 \leq N \leq 10^{5}
  • 1 \leq a_i \leq N
  • a_i \neq i

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

If no arrangements satisfy the conditions, print -1. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:

b_1 b_2 \ldots b_N

Here, b_i represents the i-th card from the left.


Sample Input 1

4
2 3 4 1

Sample Output 1

1 3 2 4
  • The arrangement (1,2,3,4) is lexicographically smaller than (1,3,2,4), but is invalid, since it violates the condition "to the immediate right of Card 1 is not Card 2."

Sample Input 2

2
2 1

Sample Output 2

-1
  • If no arrangements satisfy the conditions, print -1.

Sample Input 3

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

Sample Output 3

1 3 2 4 6 5 7 9 8 10 12 11 13
E - Span Covering

Time Limit: 2.525 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

ニワンゴ君は半開区間 [0,X) で表される土地を購入しました。

ニワンゴ君はこの土地に N 枚のシートを敷くことにしました。シートには 1,2, \ldots, N と番号が振られており、これらは区別されます。 シート i は、0 \leq j \leq X - L_i を満たす整数 j を選んで [j, j + L_i) を覆うように敷くことができます。

[0,X) にシートに覆われていない座標が存在しないようなシートの敷き方の総数を 10^9+7 で割ったあまりを求めてください。 ただし、2 つの敷き方が異なるとは、整数 i(1 \leq i \leq N) であって、シート i が覆っている領域が異なるものが存在することを指します。

制約

  • 1 \leq N \leq 100
  • 1 \leq L_i \leq X \leq 500
  • 入力中の値はすべて整数

入力

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

N X
L_1 L_2 \ldots L_N

出力

答えを出力せよ。


入力例 1

3 3
1 1 2

出力例 1

10
  • 区間全体が覆われているかどうかを無視すると、シートの敷き方の総数は 18 通り考えられます。
  • そのうち、[0,1) が覆われないような敷き方が 4 通り、[2,3) が覆われないような敷き方が 4 通りあります。
  • それ以外の敷き方は [0,3) を全てシートで覆うことができるので、答えは 10 通りです。

入力例 2

18 477
324 31 27 227 9 21 41 29 50 34 2 362 92 11 13 17 183 119

出力例 2

134796357
  • 敷き方の総数を 10^9+7 で割ったあまりを求めてください。

Score : 1100 points

Problem Statement

Niwango bought a piece of land that can be represented as a half-open interval [0, X).

Niwango will lay out N vinyl sheets on this land. The sheets are numbered 1,2, \ldots, N, and they are distinguishable. For Sheet i, he can choose an integer j such that 0 \leq j \leq X - L_i and cover [j, j + L_i) with this sheet.

Find the number of ways to cover the land with the sheets such that no point in [0, X) remains uncovered, modulo (10^9+7). We consider two ways to cover the land different if and only if there is an integer i (1 \leq i \leq N) such that the region covered by Sheet i is different.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq L_i \leq X \leq 500
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 \ldots L_N

Output

Print the answer.


Sample Input 1

3 3
1 1 2

Sample Output 1

10
  • If we ignore whether the whole interval is covered, there are 18 ways to lay out the sheets.
  • Among them, there are 4 ways that leave [0, 1) uncovered, and 4 ways that leave [2, 3) uncovered.
  • Each of the other ways covers the whole interval [0,3), so the answer is 10.

Sample Input 2

18 477
324 31 27 227 9 21 41 29 50 34 2 362 92 11 13 17 183 119

Sample Output 2

134796357
  • Find the number of ways modulo (10^9+7).