A - Rolling Dice

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

1,2,\ldots,6 の出目がある 6 面サイコロを A 回振ったとき、出た目の合計が B になることはありますか?

制約

  • 1 \leq A \leq 100
  • 1 \leq B \leq 1000
  • A, B は整数である。

入力

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

A B

出力

出た目の合計が B になり得る場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

2 11

出力例 1

Yes

6 面サイコロを 2 回振ったときに出た目の和が 11 になるのは次の 2 通りの場合です。

  • 1 回目のサイコロの出目が 6 で、2 回目のサイコロの出目が 5 である。
  • 1 回目のサイコロの出目が 5 で、2 回目のサイコロの出目が 6 である。

入力例 2

2 13

出力例 2

No

6 面サイコロを 2 回振ったときに出た目の和が 13 になることはありません。


入力例 3

100 600

出力例 3

Yes

Score : 100 points

Problem Statement

Is it possible to get a sum of B when throwing a die with six faces 1,2,\ldots,6 A times?

Constraints

  • 1 \leq A \leq 100
  • 1 \leq B \leq 1000
  • A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If it is possible to get a sum of B, print Yes; otherwise, print No.


Sample Input 1

2 11

Sample Output 1

Yes

There are two ways to get a sum of 11 when throwing a 6-faced die twice:

  • getting 6 in the first throw and 5 in the second throw;
  • getting 5 in the first throw and 6 in the second throw.

Sample Input 2

2 13

Sample Output 2

No

There is no way to get a sum of 13 when throwing a 6-faced die twice.


Sample Input 3

100 600

Sample Output 3

Yes
B - Factorial Yen Coin

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋王国では 1! 円硬貨 , 2! 円硬貨 , \dots, 10! 円硬貨が流通しています。ここで、N! = 1 \times 2 \times \dots \times N です。

高橋君は全ての種類の硬貨を 100 枚ずつ持っており、P 円の商品をお釣りが出ないようにちょうどの金額を支払って買おうとしています。

問題の制約下で条件を満たす支払い方は必ず存在することが証明できます。

最小で何枚の硬貨を使えば支払うことができますか?

制約

  • 1 \leq P \leq 10^7
  • P は整数である。

入力

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

P

出力

必要となる硬貨の最小枚数を出力せよ。


入力例 1

9

出力例 1

3

1! = 1 円硬貨、2! = 2 円硬貨、3! = 6 円硬貨を 1 枚ずつ使うと 3 枚の硬貨で 9 円の商品をちょうどの金額で支払うことができます。これより少ない枚数で支払う方法は存在しません。


入力例 2

119

出力例 2

10

1! 円硬貨を 1 枚、2! 円硬貨を 2 枚、3! 円硬貨を 3 枚、4! 円硬貨を 4 枚使えばよいです。


入力例 3

10000000

出力例 3

24

Score : 200 points

Problem Statement

The coins used in the Kingdom of Takahashi are 1!-yen coins, 2!-yen coins, \dots, and 10!-yen coins. Here, N! = 1 \times 2 \times \dots \times N.

Takahashi has 100 of every kind of coin, and he is going to buy a product worth P yen by giving the exact amount without receiving change.

We can prove that there is always such a way to make payment.

At least how many coins does he need to use in his payment?

Constraints

  • 1 \leq P \leq 10^7
  • P is an integer.

Input

Input is given from Standard Input in the following format:

P

Output

Print the minimum number of coins needed.


Sample Input 1

9

Sample Output 1

3

By giving one (1! =) 1-yen coin, one (2! =) 2-yen coin, and one (3! =) 6-yen coin, we can make the exact payment for the product worth 9 yen. There is no way to pay this amount using fewer coins.


Sample Input 2

119

Sample Output 2

10

We should use one 1!-yen coin, two 2!-yen coins, three 3!-yen coins, and four 4!-yen coins.


Sample Input 3

10000000

Sample Output 3

24
C - Fair Candy Distribution

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

高橋王国には N 人の国民がいます。 全ての国民には国民番号が割り振られており、 i 人目の国民の国民番号は a_i です。ここで、a_i は互いに異なります。

高橋君は K 個のお菓子を持っています。高橋君は次のルールに従って、持っているお菓子が無くなるまで国民にお菓子を配ることにしました。

  • 持っているお菓子が N 個以上ある場合、全員に 1 個ずつお菓子を配る。
  • そうでない場合、その時点で高橋くんが持っているお菓子の個数を K' として、国民番号が小さい方から K' 人に 1 個ずつ配る。より厳密には、a_i の値が小さい方から K' 人を選び、選んだ人に 1 個ずつお菓子を配る。

全てのお菓子を配り終えたとき、i 人目の国民は何個のお菓子を持っていますか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{18}
  • 1 \leq a_i \leq 10^9
  • a_i は互いに異なる。
  • 入力は全て整数である。

入力

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

N K
a_1 a_2 \ldots a_N

出力

N 行出力せよ。i 行目には i 人目の国民がもらったお菓子の個数を出力せよ。


入力例 1

2 7
1 8

出力例 1

4
3

高橋君はお菓子を次の手順で配ります。

  • 全員に 1 個ずつお菓子を配り、高橋君の持っているお菓子は 5 個になる。
  • 全員に 1 個ずつお菓子を配り、高橋君の持っているお菓子は 3 個になる。
  • 全員に 1 個ずつお菓子を配り、高橋君の持っているお菓子は 1 個になる。
  • 1 人目の国民に 1 個お菓子を配り、高橋君の持っているお菓子は無くなる。

最終的に 1 人目の国民は 4 個、2 人目の国民は 3 個のお菓子を手に入れることができます。


入力例 2

1 3
33

出力例 2

3

国民が 1 人しかいないので、高橋君は全てのお菓子を 1 人目の国民に配ることになります。


入力例 3

7 1000000000000
99 8 2 4 43 5 3

出力例 3

142857142857
142857142857
142857142858
142857142857
142857142857
142857142857
142857142857

Score : 300 points

Problem Statement

There are N citizens in the Kingdom of Takahashi. Each citizen has a national ID number; the ID of the i-th citizen is a_i. Here, all a_i are pairwise different.

Takahashi has K pieces of sweets. He has decided to hand out these pieces to the citizens in the following way until he has no more pieces.

  • When he has N or more pieces, hand out one piece to every citizen.
  • Otherwise, let K' be the number of pieces he has at the moment, and hand out one piece to each of the citizens with the K' smallest IDs.

When all pieces are handed out, how many pieces will the i-th citizen have?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{18}
  • 1 \leq a_i \leq 10^9
  • All a_i are pairwise different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \ldots a_N

Output

Print N lines. The i-th line should contain the number of pieces of sweets received by the i-th citizen.


Sample Input 1

2 7
1 8

Sample Output 1

4
3

Takahashi will hand out the pieces as follows.

  • Hand out one piece to everyone, leaving Takhashi with 5 pieces.
  • Hand out one piece to everyone, leaving Takhashi with 3 pieces.
  • Hand out one piece to everyone, leaving Takhashi with 1 piece.
  • Hand out one piece to the 1-st citizen, leaving Takhashi with no pieces.

In the end, the 1-st citizen will receive 4 pieces, and the 2-nd citizen will receive 3 pieces.


Sample Input 2

1 3
33

Sample Output 2

3

Since there is just one citizen, Takahashi will hand out all pieces to that 1-st citizen.


Sample Input 3

7 1000000000000
99 8 2 4 43 5 3

Sample Output 3

142857142857
142857142857
142857142858
142857142857
142857142857
142857142857
142857142857
D - Shortest Path Queries 2

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋王国には N 個の都市と M 本の道路があります。

都市には 1 から N の番号が、道路には 1 から M の番号が割り振られています。道路 i は都市 A_i から B_i へ向かう一方通行の道で、移動するのに C_i 分かかります。

f(s, t, k) を次のクエリへの答えとして定めます。

  • 都市 s を出発して都市 t に到着するまでの最短時間を計算してください。ただし、通ってよい都市は s, t および番号が k 以下の都市のみとします。また、都市 t に到着できない場合や s = t である場合におけるクエリの答えは 0 とします。

全ての s,t,k に対して f(s,t,k) を計算して総和を出力してください。より厳密には、\displaystyle \sum_{s = 1}^N \sum_{t = 1}^N \sum_{k = 1}^N f(s, t, k) を出力してください。

制約

  • 1 \leq N \leq 400
  • 0 \leq M \leq N(N-1)
  • 1 \leq A_i \leq N (1 \leq i \leq M)
  • 1 \leq B_i \leq N (1 \leq i \leq M)
  • A_i \neq B_i (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^6 (1 \leq i \leq M)
  • i \neq j ならば A_i \neq A_j または B_i \neq B_j である。
  • 入力は全て整数である。

入力

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

N M
A_1 B_1 C_1
\vdots
A_M B_M C_M

出力

\displaystyle \sum_{s = 1}^N \sum_{t = 1}^N \sum_{k = 1}^N f(s, t, k) を出力せよ。


入力例 1

3 2
1 2 3
2 3 2

出力例 1

25

f(s,t,k) \neq 0 であるような s,t,k を以下に挙げます。

  • k = 1 のとき:f(1,2,1) = 3, f(2,3,1) = 2
  • k = 2 のとき:f(1,2,2) = 3, f(2,3,2) = 2, f(1,3,2) = 5
  • k = 3 のとき:f(1,2,3) = 3, f(2,3,3) = 2, f(1,3,3) = 5

入力例 2

3 0

出力例 2

0

全ての s,t,k に対して f(s,t,k) = 0 です。


入力例 3

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

出力例 3

517

Score : 400 points

Problem Statement

There are N cities and M roads in the Kingdom of Takahashi.

The cities are numbered 1 through N, and the roads are numbered 1 through M. Road i is a one-way road that leads from City A_i to City B_i, and it takes C_i minutes to go through it.

Let us define f(s, t, k) as the answer to the following query.

  • Compute the minimum time needed to get from City s to City t. Here, other than the Cities s and t, it is only allowed to pass through Cities 1 through k. If City t is unreachable or s = t, the answer should be 0.

Compute f(s,t,k) for all triples s,t,k and print the sum of them. More formally, print \displaystyle \sum_{s = 1}^N \sum_{t = 1}^N \sum_{k = 1}^N f(s, t, k).

Constraints

  • 1 \leq N \leq 400
  • 0 \leq M \leq N(N-1)
  • 1 \leq A_i \leq N (1 \leq i \leq M)
  • 1 \leq B_i \leq N (1 \leq i \leq M)
  • A_i \neq B_i (1 \leq i \leq M)
  • 1 \leq C_i \leq 10^6 (1 \leq i \leq M)
  • A_i \neq A_j or B_i \neq B_j, if i \neq j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
\vdots
A_M B_M C_M

Output

Print \displaystyle \sum_{s = 1}^N \sum_{t = 1}^N \sum_{k = 1}^N f(s, t, k).


Sample Input 1

3 2
1 2 3
2 3 2

Sample Output 1

25

The triples s,t,k such that f(s,t,k) \neq 0 are as follows.

  • For k = 1: f(1,2,1) = 3, f(2,3,1) = 2.
  • For k = 2: f(1,2,2) = 3, f(2,3,2) = 2, f(1,3,2) = 5.
  • For k = 3: f(1,2,3) = 3, f(2,3,3) = 2, f(1,3,3) = 5.

Sample Input 2

3 0

Sample Output 2

0

We have f(s,t,k) = 0 for all s,t,k.


Sample Input 3

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

Sample Output 3

517
E - Digit Products

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 以下の正の整数のうち、各桁の数字の積が K 以下であるものは何個ありますか?

制約

  • 1 \leq N \leq 10^{18}
  • 1 \leq K \leq 10^9
  • 入力は全て整数である。

入力

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

N K

出力

条件を満たす整数の個数を出力せよ。


入力例 1

13 2

出力例 1

5

13 以下の正の整数のうち、各桁の数字の積が 2 以下であるものは 1,2,10,11,125 つです。


入力例 2

100 80

出力例 2

99

100 以下の正の整数のうち、99 以外のものが条件を満たします。


入力例 3

1000000000000000000 1000000000

出力例 3

841103275147365677

答えが 32 bit 整数に収まらない可能性があることに注意してください。

Score : 500 points

Problem Statement

For how many positive integers at most N is the product of the digits at most K?

Constraints

  • 1 \leq N \leq 10^{18}
  • 1 \leq K \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of integers satisfying the condition.


Sample Input 1

13 2

Sample Output 1

5

Out of the positive integers at most 13, there are five such that the product of the digits is at most 2: 1, 2, 10, 11, and 12.


Sample Input 2

100 80

Sample Output 2

99

Out of the positive integers at most 100, all but 99 satisfy the condition.


Sample Input 3

1000000000000000000 1000000000

Sample Output 3

841103275147365677

Note that the answer may not fit into a 32-bit integer.

F - Cumulative Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

非負整数 n, m に対して関数 f(n, m) を正の整数 K を用いて次のように定めます。

\displaystyle f(n, m) = \begin{cases} 0 & (n = 0) \\ n^K & (n \gt 0, m = 0) \\ f(n-1, m) + f(n, m-1) & (n \gt 0, m \gt 0) \end{cases}

N, M, K が与えられるので、f(N, M)(10 ^ 9 + 7) で割った余りを求めてください。

制約

  • 0 \leq N \leq 10^{18}
  • 0 \leq M \leq 30
  • 1 \leq K \leq 2.5 \times 10^6
  • 入力は全て整数である。

入力

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

N M K

出力

f(N, M)(10 ^ 9 + 7) で割った余りを出力せよ。


入力例 1

3 4 2

出力例 1

35

K = 2 の時、 n \leq 3, m \leq 4 における f(n, m) の値は次のようになります。

m = 0 m = 1 m = 2 m = 3 m = 4
n = 0 0 0 0 0 0
n = 1 1 1 1 1 1
n = 2 4 5 6 7 8
n = 3 9 14 20 27 35

入力例 2

0 1 2

出力例 2

0

入力例 3

1000000000000000000 30 123456

出力例 3

297085514

Score : 600 points

Problem Statement

For non-negative integers n and m, let us define the function f(n, m) as follows, using a positive integer K.

\displaystyle f(n, m) = \begin{cases} 0 & (n = 0) \\ n^K & (n \gt 0, m = 0) \\ f(n-1, m) + f(n, m-1) & (n \gt 0, m \gt 0) \end{cases}

Given N, M, and K, find f(N, M) modulo (10 ^ 9 + 7).

Constraints

  • 0 \leq N \leq 10^{18}
  • 0 \leq M \leq 30
  • 1 \leq K \leq 2.5 \times 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K

Output

Print f(N, M) modulo (10 ^ 9 + 7).


Sample Input 1

3 4 2

Sample Output 1

35

When K = 2, the values f(n, m) for n \leq 3, m \leq 4 are as follows.

m = 0 m = 1 m = 2 m = 3 m = 4
n = 0 0 0 0 0 0
n = 1 1 1 1 1 1
n = 2 4 5 6 7 8
n = 3 9 14 20 27 35

Sample Input 2

0 1 2

Sample Output 2

0

Sample Input 3

1000000000000000000 30 123456

Sample Output 3

297085514