A - I Scream

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

日本において、アイス製品は次の 4 種類に大別されます。

  • 乳固形分15 パーセント以上、乳脂肪分8 パーセント以上含まれるものを「アイスクリーム」とする。
  • 上に当てはまらず、乳固形分10 パーセント以上、乳脂肪分3 パーセント以上含まれるものを「アイスミルク」とする。
  • 上のいずれにも当てはまらず、乳固形分3 パーセント以上含まれるものを「ラクトアイス」とする。
  • 上のいずれにも当てはまらないものを「氷菓」とする。

ここで、乳固形分無脂乳固形分乳脂肪分2 つから成ります。
AtCoder 名物の製品「スヌケアイス」には、無脂乳固形分A パーセント、乳脂肪分B パーセント含まれています。
スヌケアイスに上の分類を適用したとすると、4 種類のどれに当てはまるでしょうか ?
答えは「出力」の項に従って整数で出力してください。

制約

  • 0 \le A \le 100
  • 0 \le B \le 100
  • A + B \le 100
  • A, B は整数

入力

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

A B

出力

以下に定められる整数を出力せよ :

  • スヌケアイスがアイスクリームに当てはまる場合 : 1
  • スヌケアイスがアイスミルクに当てはまる場合 : 2
  • スヌケアイスがラクトアイスに当てはまる場合 : 3
  • スヌケアイスが氷菓に当てはまる場合 : 4

入力例 1

10 8

出力例 1

1

このスヌケアイスには、無脂乳固形分が 10 パーセント、乳脂肪分が 8 パーセント含まれているため、乳固形分は 18 パーセント含まれています。
乳固形分が 15 パーセント以上、乳脂肪分が 8 パーセント以上含まれているため、アイスクリームに分類されます。従って 1 が正しい出力です。


入力例 2

1 2

出力例 2

3

乳固形分がちょうど 3 パーセント含まれているため、アイスクリームやアイスミルクには該当しませんが、ラクトアイスに該当するため 3 を出力します。


入力例 3

0 0

出力例 3

4

氷菓に分類されます。

Score : 100 points

Problem Statement

In Japan, there are four major categories of ice cream type products:

  • an ice cream type product with at least 15 percent milk solids and at least 8 percent milk fat is called an ice cream;
  • an ice cream type product with at least 10 percent milk solids and at least 3 percent milk fat that is not an ice cream is called an ice milk;
  • an ice cream type product with at least 3 percent milk solids that is not an ice cream or an ice milk is called a lacto ice;
  • an ice cream type product that is not an ice cream, an ice milk, or a lacto ice is called a flavored ice.

Here, milk solids consist of milk fat and milk solids-not-fat.
AtCoder's famous product Snuke Ice contains A percent milk solids-not-fat and B percent milk fat.
Which of the categories above does Snuke Ice fall into?
Print your answer as an integer according to the Output section.

Constraints

  • 0 \le A \le 100
  • 0 \le B \le 100
  • A + B \le 100
  • A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print an integer as follows:

  • if Snuke Ice is an ice cream, print 1;
  • if Snuke Ice is an ice milk, print 2;
  • if Snuke Ice is a lacto ice, print 3;
  • if Snuke Ice is a flavored ice, print 4.

Sample Input 1

10 8

Sample Output 1

1

This product contains 10 percent milk solids-not-fat and 8 percent milk fat, for a total of 18 percent milk solids.
Since it contains not less than 15 percent milk solids and not less than 8 percent milk fat, it is an ice cream; the correct output is 1.


Sample Input 2

1 2

Sample Output 2

3

Since it contains exactly 3 percent milk solids, it is not an ice cream or an ice milk but is a lacto ice; the correct output is 3.


Sample Input 3

0 0

Sample Output 3

4

It is a flavored ice.

B - Job Assignment

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

あなたの会社には従業員 1 から従業員 N までの N 人の従業員がいます。
今あなたは仕事 A と仕事 B の 2 つの仕事を受注したので、これらを完了しなければなりません。
従業員 i は仕事 A を A_i 分、仕事 B を B_i 分でこなすことができます。

あなたは仕事 A と仕事 B にそれぞれ従業員を 1 人割り当てます。
同じ従業員を両方の仕事に割り当てても構いませんが、その場合 2 つの仕事が終わるのにかかる時間は、それぞれの仕事が終わるのにかかる時間の和となります。
仕事 A と仕事 B に異なる従業員を割り当てた場合、2 つの仕事が終わるのにかかる時間は、各仕事が終わるのにかかる時間の長い方となります。
2 つの仕事が終わるのにかかる時間として考えられる最小の値を求めてください。

制約

  • 2 \le N \le 1000
  • 1 \le A_i \le 10^5
  • 1 \le B_i \le 10^5
  • 入力に含まれる値は全て整数

入力

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

N
A_1 B_1
A_2 B_2
A_3 B_3
\hspace{15pt} \vdots
A_N B_N

出力

2 つの仕事が終わるのにかかる時間として考えられる最小の値 [分] を出力せよ。


入力例 1

3
8 5
4 4
7 9

出力例 1

5

仕事 A には従業員 2 を、仕事 B には従業員 1 を割り当てると、仕事 A, B はそれぞれ 4, 5 分で完了します。
2 つの仕事に異なる従業員を割り当てたので、2 つの仕事が終わるのにかかる時間は \max(4, 5) = 5 [分] となります。
これより短い時間で 2 つの仕事が終わることはありません。


入力例 2

3
11 7
3 2
6 7

出力例 2

5

両方の仕事に従業員 2 を割り当てるのが最適です。
同じ従業員を両方の仕事に割り当てた場合 2 つの仕事が終わるのにかかる時間は、それぞれの仕事が終わるのにかかる時間の和となることに注意してください。

Score : 200 points

Problem Statement

Your company has N employees, called Employee 1 through N.
You have received two work orders, called Work A and B, which must be completed.
Employee i can complete Work A in A_i minutes and Work B in B_i minutes.

You will assign each work to one employee.
You can assign both works to the same employee, in which case the time it takes for him/her to complete them is the sum of the times it takes for him/her to do them individually.
If you assign the works to different employees, the time it takes for them to complete them is the longer of the times it takes for them to do their respective works.
Find the shortest possible time needed to complete the works.

Constraints

  • 2 \le N \le 1000
  • 1 \le A_i \le 10^5
  • 1 \le B_i \le 10^5
  • 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
A_3 B_3
\hspace{15pt} \vdots
A_N B_N

Output

Print the shortest possible time needed to complete the works, in minutes.


Sample Input 1

3
8 5
4 4
7 9

Sample Output 1

5

If you assign Work A to Employee 2 and Work B to Employee 1, they will complete them in 4 and 5 minutes, respectively.
Since you assigned the works to different employees, it will take \max(4, 5) = 5 minutes for the two works to be finished.
It is impossible to finish them earlier.


Sample Input 2

3
11 7
3 2
6 7

Sample Output 2

5

It is optimal to assign both works to Employee 2.
Note that if you assign both works to the same employee, the time it takes for him/her to complete them is the sum of the times it takes for him/her to do them individually.

C - Squared Error

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の数列 A が与えられます。
各要素同士の差の 2 乗の和、すなわち \displaystyle \sum_{i = 2}^{N} \sum_{j = 1}^{i - 1} (A_i - A_j)^2 を求めてください。

制約

  • 2 \le N \le 3 \times 10^5
  • |A_i| \le 200
  • 入力に含まれる値は全て整数

入力

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

N
A_1 A_2 A_3 \cdots A_N

出力

答えを出力せよ。


入力例 1

3
2 8 4

出力例 1

56

\sum_{i = 2}^{N} \sum_{j = 1}^{i - 1} (A_i - A_j)^2 = (8 - 2)^2 + (4 - 2) ^ 2 + (4 - 8) ^ 2 = 56 です。


入力例 2

5
-5 8 9 -4 -3

出力例 2

950

Score : 300 points

Problem Statement

Given is a number sequence A of length N.
Find the sum of squared differences of every pair of elements: \displaystyle \sum_{i = 2}^{N} \sum_{j = 1}^{i - 1} (A_i - A_j)^2.

Constraints

  • 2 \le N \le 3 \times 10^5
  • |A_i| \le 200
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \cdots A_N

Output

Print the answer.


Sample Input 1

3
2 8 4

Sample Output 1

56

We have \sum_{i = 2}^{N} \sum_{j = 1}^{i - 1} (A_i - A_j)^2 = (8 - 2)^2 + (4 - 2) ^ 2 + (4 - 8) ^ 2 = 56.


Sample Input 2

5
-5 8 9 -4 -3

Sample Output 2

950
D - Journey

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

頂点 1 から頂点 N までの N 頂点からなるグラフの頂点 1 に高橋君がいます。
今このグラフに辺は 1 つも張られていません。
高橋君は以下の操作を繰り返します。

操作 :

  1. (今高橋君がいる頂点も含めた) N 個の頂点の中から 1 つランダムに選ぶ。各頂点が選ばれる確率は全て \frac{1}{N} であり、選択は操作毎に独立である。
  2. 今高橋君がいる頂点と選ばれた頂点の間に無向辺を張り、選ばれた頂点に移動する。

グラフが連結になるまでに行われる操作の回数の期待値を求めてください。

制約

  • 2 \le N \le 10^5

入力

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

N

出力

答えを出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

2

出力例 1

2.00000000000

グラフが連結になるのは、操作において初めて頂点 2 が選ばれた時です。
i について i 回目の操作で初めて頂点 2 が選ばれる場合を考えると、答えは \sum_{i = 1}^{\infty} (i \times (\frac{1}{2})^i) = 2 となります。


入力例 2

3

出力例 2

4.50000000000

Score : 400 points

Problem Statement

We have a graph with N vertices called Vertex 1 through N. Takahashi is standing on Vertex 1.
The graph has no edges now.
Takahashi will repeatedly do the following operation:

  1. Choose one of the N vertices (including the one on which Takahashi is standing now). Every vertex is chosen with probability \frac{1}{N}, independently from previous operations.
  2. Add an edge between the vertex on which Takahashi is standing now and the chosen vertex, and go to the chosen vertex.

Find the expected value of the number of times he does the operation until the graph becomes connected.

Constraints

  • 2 \le N \le 10^5

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.
Your answer will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.


Sample Input 1

2

Sample Output 1

2.00000000000

The graph becomes connected when the operation chooses Vertex 2 for the first time.
By considering the case Vertex 2 is chosen for the first time in the i-th operation for each i, the answer is \sum_{i = 1}^{\infty} (i \times (\frac{1}{2})^i) = 2.


Sample Input 2

3

Sample Output 2

4.50000000000
E - Mex Min

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

\mathrm{mex}(x_1, x_2, x_3, \dots, x_k) を、x_1, x_2, x_3, \dots, x_k に含まれない最小の非負整数と定義します。
長さ N の整数列 A = (A_1, A_2, A_3, \dots, A_N) が与えられます。
0 \le i \le N - M を満たす全ての整数 i について \mathrm{mex}(A_{i + 1}, A_{i + 2}, A_{i + 3}, \dots, A_{i + M}) を計算したとき、この N - M + 1 個の値のうちの最小値を求めてください。

制約

  • 1 \le M \le N \le 1.5 \times 10^6
  • 0 \le A_i \lt N
  • 入力に含まれる値は全て整数

入力

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

N M
A_1 A_2 A_3 \cdots A_N

出力

答えを出力せよ。


入力例 1

3 2
0 0 1

出力例 1

1
  • i = 0 のとき : \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(0, 0) = 1
  • i = 1 のとき : \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(0, 1) = 2

よって 12 のうちの最小値である 1 が答えです。


入力例 2

3 2
1 1 1

出力例 2

0
  • i = 0 のとき : \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(1, 1) = 0
  • i = 1 のとき : \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(1, 1) = 0

となります。


入力例 3

3 2
0 1 0

出力例 3

2
  • i = 0 のとき : \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(0, 1) = 2
  • i = 1 のとき : \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(1, 0) = 2

となります。


入力例 4

7 3
0 0 1 2 0 1 0

出力例 4

2

Score : 500 points

Problem Statement

Let us define \mathrm{mex}(x_1, x_2, x_3, \dots, x_k) as the smallest non-negative integer that does not occur in x_1, x_2, x_3, \dots, x_k.
You are given an integer sequence of length N: A = (A_1, A_2, A_3, \dots, A_N).
For each integer i such that 0 \le i \le N - M, we compute \mathrm{mex}(A_{i + 1}, A_{i + 2}, A_{i + 3}, \dots, A_{i + M}). Find the minimum among the results of these N - M + 1 computations.

Constraints

  • 1 \le M \le N \le 1.5 \times 10^6
  • 0 \le A_i \lt N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 A_3 \dots A_N

Output

Print the answer.


Sample Input 1

3 2
0 0 1

Sample Output 1

1

We have:

  • for i = 0: \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(0, 0) = 1
  • for i = 1: \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(0, 1) = 2

Thus, the answer is the minimum among 1 and 2, which is 1.


Sample Input 2

3 2
1 1 1

Sample Output 2

0

We have:

  • for i = 0: \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(1, 1) = 0
  • for i = 1: \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(1, 1) = 0

Sample Input 3

3 2
0 1 0

Sample Output 3

2

We have:

  • for i = 0: \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(0, 1) = 2
  • for i = 1: \mathrm{mex}(A_{i + 1}, A_{i + 2}) = \mathrm{mex}(1, 0) = 2

Sample Input 4

7 3
0 0 1 2 0 1 0

Sample Output 4

2
F - Digits Paradise in Hexadecimal

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

この問題において、十六進表記では 0 ~ 9, A ~ F を数字として扱い、A ~ F はそれぞれ十から十五を表すものとします。
また、特別の記述がない限り問題文中で扱われる数は全て十進表記されているものとします。

1 以上 N 以下の整数のうち、先頭に 0 のない十六進表記で書くとちょうど K 種類の数字が現れるようなものはいくつあるでしょうか ?
10^9 + 7 で割ったあまりを出力してください。

制約

  • 1 \le N \lt {16}^{2 \times 10^5}
  • N は先頭が 0 でない十六進表記で与えられる
  • 1 \le K \le 16
  • 入力に含まれる値は全て整数

入力

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

N K

N は十六進表記で与えられる。

出力

答えを 10^9 + 7 で割ったあまりを出力せよ。


入力例 1

10 1

出力例 1

15

N は十六進表記で与えられているため、十進数に直すと 16 です。
1 以上 16 以下の整数を、先頭に 0 のない十六進表記で書くと下記のようになります。

  • 1 から 15 まで : 十六進表記に直すと 1 桁なので、出現する数字は 1 種類である
  • 16 : 十六進表記に直すと 10 なので、出現する数字は 2 種類である

よって、十六進表記に直した時に出現する数字が 1 種類なのは 15 個です。


入力例 2

FF 2

出力例 2

225

出現する数字が 2 種類なのは、1 以上 255 以下の 255 個の整数のうち、十六進表記で 1, 2, 3, \dots, \mathrm{E}, \mathrm{F}, 11, 22, 33, \dots, \mathrm{EE}, \mathrm{FF} と表される 15 + 15 = 30 個を除いたものです。


入力例 3

100 2

出力例 3

226

入力例 4

1A8FD02 4

出力例 4

3784674

入力例 5

DEADBEEFDEADBEEEEEEEEF 16

出力例 5

153954073

答えを 10^9 + 7 で割った余りを出力してください。

Score : 600 points

Problem Statement

In this problem, hexadecimal notations use 0, ..., 9, A, ..., F, representing the values zero through fifteen, respectively.
Unless otherwise specified, all notations of numbers are decimal notations.

How many integers between 1 and N (inclusive) have exactly K distinct digits in the hexadecimal notation without leading zeros?
Print this count modulo (10^9 + 7).

Constraints

  • 1 \le N \lt {16}^{2 \times 10^5}
  • N is given in hexadecimal notation without leading 0s.
  • 1 \le K \le 16
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Here, N is in hexadecimal notation.

Output

Print the count modulo 10^9 + 7.


Sample Input 1

10 1

Sample Output 1

15

The hexadecimal number N is 16 in decimal.
In hexadecimal, the integers between 1 and 16 are written as follows:

  • 1 through 15: are 1-digit numbers in hexadecimal, containing one distinct digit.
  • 16: is 10 in hexadecimal, containing two distinct digits.

Thus, there are 15 numbers that contain one distinct digit in hexadecimal.


Sample Input 2

FF 2

Sample Output 2

225

All of the 255 numbers except the following 30 numbers contain two distinct digits in hexadecimal: 1, 2, 3, \dots, \mathrm{E}, \mathrm{F}, 11, 22, 33, \dots, \mathrm{EE}, \mathrm{FF} in hexadecimal.


Sample Input 3

100 2

Sample Output 3

226

Sample Input 4

1A8FD02 4

Sample Output 4

3784674

Sample Input 5

DEADBEEFDEADBEEEEEEEEF 16

Sample Output 5

153954073

Print the count modulo (10^9 + 7).