A - Ball Distribution

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君は N 個のボールを K 人に配ろうとしています。

それぞれの人がボールを 1 個以上受け取るような配り方の中で、ボールが最も多い人と最も少ない人のボールの個数の差が最大で何個になるか求めてください。

制約

  • 1 \leq K \leq N \leq 100
  • 入力は全て整数である

入力

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

N K

出力

ボールの個数の差としてあり得る最大値を出力せよ。


入力例 1

3 2

出力例 1

1

それぞれの人がボールを 1 個以上受け取るように 3 個のボールを 2 人に配る方法は、1 人に 1 個、もう 1 人に 2 個配る方法のみです。

よってボールの個数の差の最大値は 1 です。


入力例 2

3 1

出力例 2

0

3 個のボールを 1 人に渡すしかなく、この時ボールの個数の差は 0 です。


入力例 3

8 5

出力例 3

3

例えば 5 人にボールをそれぞれ 1, 4, 1, 1, 1 個配った場合に、ボールが最も多い人と最も少ない人のボールの個数の差が 3 となり、これが最大です。

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

  • 1 \leq K \leq N \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.


Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.


Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.


Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.

B - Picking Up

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

2 次元平面上に N 個のボールがあり、i 番目のボールは (x_i, y_i) にあります。

まず、p \neq 0 または q \neq 0 を満たす 2 つの整数 p, q を決め、その後以下の操作を繰り返してボールを全て回収します。

  • 残っているボールを 1 つ選んで回収する。このボールの座標を (a, b) とする。この時、直前に選んだボールの座標が (a - p, b - q) である時コスト 0 、そうでない時コスト 1 かかる。ただし、1 つ目に選んだボールについては必ずコスト 1 かかる。

p, q を最適に選んだ場合にボールを全て回収するのにかかるコストの和の最小値を計算してください。

制約

  • 1 \leq N \leq 50
  • |x_i|, |y_i| \leq 10^9
  • x_i \neq x_j または y_i \neq y_j (i \neq j)
  • 入力は全て整数である

入力

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

N
x_1 y_1
:
x_N y_N

出力

ボールを全て回収するのにかかるコストの和の最小値を出力せよ。


入力例 1

2
1 1
2 2

出力例 1

1

p = 1, q = 1 とすると、(1, 1) のボール、(2, 2) のボールの順に選ぶことでコスト 1 でボールを全て回収することができます。


入力例 2

3
1 4
4 6
7 8

出力例 2

1

p = -3, q = -2 とすると、(7, 8) のボール、(4, 6) のボール、(1, 4) のボールの順に選ぶことでコスト 1 でボールを全て回収することができます。


入力例 3

4
1 1
1 2
2 1
2 2

出力例 3

2

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

  • Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

  • 1 \leq N \leq 50
  • |x_i|, |y_i| \leq 10^9
  • If i \neq j, x_i \neq x_j or y_i \neq y_j.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.


Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).


Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).


Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2
C - Successive Subtraction

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

黒板に A_1, A_2, ..., A_NN 個の整数が書かれています。

以下の操作を N-1 回繰り返して黒板にただ 1 つの整数が書かれているようにします。

  • 2 個の整数 x, y を選んで消し、新たに 1 個の整数 x-y を書く。

ただ 1 つ残る整数としてありうる値の最大値と、その最大値を達成する操作列を求めてください。

制約

  • 2 \leq N \leq 10^5
  • -10^4 \leq A_i \leq 10^4
  • 入力は全て整数である

入力

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

N
A_1 A_2 ... A_N

出力

ただ 1 つ残る整数としてありうる値の最大値 M と、その最大値を達成する操作列 x_i, y_i を以下の形式に従って出力せよ。

ただし、x_i, y_ii 回目の操作で選ぶ x, y を表す。

また、最大値を達成する操作列が複数存在する場合は、そのうちどれを出力しても良い。

M
x_1 y_1
:
x_{N-1} y_{N-1}

入力例 1

3
1 -1 2

出力例 1

4
-1 1
2 -2

1 回目の操作で x として -1y として1 を選ぶと、黒板に書かれている整数は (-2, 2) になります。

2 回目の操作で x として 2y として-2 を選ぶと、黒板に書かれている整数は (4) になります。

よって 4 がただ 1 つ残り、5 以上の整数がただ 1 つ残ることはないので、これが最大です。


入力例 2

3
1 1 1

出力例 2

1
1 1
1 0

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

  • Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

  • 2 \leq N \leq 10^5
  • -10^4 \leq A_i \leq 10^4
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.


Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0
D - Squirrel Merchant

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

リスの直大君は、 N 個のドングリを持っています。 ある日直大君は、複数の貴金属取引所に行くことでドングリを増やすことにしました。

直大君は次のように行動します。

  1. N 個のドングリを持って巣から出る。
  2. 取引所 A に行く。
  3. 取引所 B に行く。
  4. 取引所 A に行く。
  5. 巣に帰る。

取引所 X(X=A,B) では、以下の操作を任意の順序で任意の整数回行うことができます(一度も行わなくてもよいです)。

  • ドングリ g_{X} 個を失う。金 1 グラムを得る。
  • ドングリ g_{X} 個を得る。金 1 グラムを失う。
  • ドングリ s_{X} 個を失う。銀 1 グラムを得る。
  • ドングリ s_{X} 個を得る。銀 1 グラムを失う。
  • ドングリ b_{X} 個を失う。銅 1 グラムを得る。
  • ドングリ b_{X} 個を得る。銅 1 グラムを失う。

もちろん、直大君の持っているドングリ、金、銀、銅のいずれかの数が負の量になるような操作を行うことはできません。

直大君が巣に持ち帰れるドングリの数は最大いくつになるでしょうか。 直大君はリスなので、巣に持ち帰った金、銀、銅は全くの無価値であることに注意して下さい。

制約

  • 1 \leqq N \leqq 5000
  • 1 \leqq g_{X} \leqq 5000
  • 1 \leqq s_{X} \leqq 5000
  • 1 \leqq b_{X} \leqq 5000
  • 入力は全て整数である。

入力

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

N
g_A s_A b_A
g_B s_B b_B

出力

直大君が巣に持ち帰れるドングリの数の最大値を出力してください。


入力例 1

23
1 1 1
2 1 1

出力例 1

46

下記のようにすることで、ドングリ 46 個を巣に持ち帰れます。

  • 取引所 A でドングリ 23 個を金 23 グラムにする。 {ドングリ、金、銀、銅}={ 0,23,0,0 }
  • 取引所 B で金 23 グラムをドングリ 46個 にする。{ドングリ、金、銀、銅}={ 46,0,0,0 }
  • 取引所 A では何もしない。{ドングリ、金、銀、銅}={ 46,0,0,0 }

47 個以上のドングリを得ることはできないため、答えは 46 です。

Score : 600 points

Problem Statement

The squirrel Chokudai has N acorns. One day, he decides to do some trades in multiple precious metal exchanges to make more acorns.

His plan is as follows:

  1. Get out of the nest with N acorns in his hands.
  2. Go to Exchange A and do some trades.
  3. Go to Exchange B and do some trades.
  4. Go to Exchange A and do some trades.
  5. Go back to the nest.

In Exchange X (X = A, B), he can perform the following operations any integer number of times (possibly zero) in any order:

  • Lose g_{X} acorns and gain 1 gram of gold.
  • Gain g_{X} acorns and lose 1 gram of gold.
  • Lose s_{X} acorns and gain 1 gram of silver.
  • Gain s_{X} acorns and lose 1 gram of silver.
  • Lose b_{X} acorns and gain 1 gram of bronze.
  • Gain b_{X} acorns and lose 1 gram of bronze.

Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze.

What is the maximum number of acorns that he can bring to the nest? Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq g_{X} \leq 5000
  • 1 \leq s_{X} \leq 5000
  • 1 \leq b_{X} \leq 5000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
g_A s_A b_A
g_B s_B b_B

Output

Print the maximum number of acorns that Chokudai can bring to the nest.


Sample Input 1

23
1 1 1
2 1 1

Sample Output 1

46

He can bring 46 acorns to the nest, as follows:

  • In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }
  • In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }
  • In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }

He cannot have 47 or more acorns, so the answer is 46.

E - Balanced Piles

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

N 個のマスが横一列に並んでおり、左から順に 1 から N までの番号がつけられています。高橋君はこれらのマスに積み木を積もうとしています。 まだそれぞれのマスには積み木が 1 つも積まれていません。

積み木をバランス良く積みたい高橋君は、以下の操作を繰り返して全てのマスに積み木がちょうど H 個ずつ積まれている状態にしようとしています。

  • 1 マスに積まれている積み木の最大値を M 個、最小値を m 個とする。m 個の積み木が置かれているマスを 1 つ選び (複数ある場合はどれを選んでもよい)、そのマスに積まれた積み木が M 個以上 M + D 個以下になるように積み木を正の個数積む。

高橋君のために、この操作を繰り返して全てのマスに積み木がちょうど H 個積まれている状態にする方法が何通りあるか数えてあげてください。答えは非常に大きくなる場合があるので、10^9+7 で割った余りを出力してください。

制約

  • 2 \leq N \leq 10^6
  • 1 \leq D \leq H \leq 10^6
  • 入力は全て整数である

入力

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

N H D

出力

全てのマスに積み木がちょうど H 個積まれている状態にする方法の個数を 10^9+7 で割った余りを出力せよ。


入力例 1

2 2 1

出力例 1

6

(マス 1 に積まれた積み木の個数, マス 2 に積まれた積み木の個数) は次のように変化させることができます。

  • (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) -> (2, 2)

  • (0, 0) -> (0, 1) -> (1, 1) -> (2, 1) -> (2, 2)

  • (0, 0) -> (0, 1) -> (2, 1) -> (2, 2)

  • (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) -> (2, 2)

  • (0, 0) -> (1, 0) -> (1, 1) -> (2, 1) -> (2, 2)

  • (0, 0) -> (1, 0) -> (1, 2) -> (2, 2)

よって、全てのマスに積み木がちょうど 2 個積まれている状態にする方法の個数は 6 通りです。


入力例 2

2 30 15

出力例 2

94182806

入力例 3

31415 9265 3589

出力例 3

312069529

個数を 10^9+7 で割った余りを出力することに注意してください。

Score : 800 points

Problem Statement

There are N squares arranged in a row, numbered 1 to N from left to right. Takahashi will stack building blocks on these squares, on which there are no blocks yet.

He wants to stack blocks on the squares evenly, so he will repeat the following operation until there are H blocks on every square:

  • Let M and m be the maximum and minimum numbers of blocks currently stacked on a square, respectively. Choose a square on which m blocks are stacked (if there are multiple such squares, choose any one of them), and add a positive number of blocks on that square so that there will be at least M and at most M + D blocks on that square.

Tell him how many ways there are to have H blocks on every square by repeating this operation. Since there can be extremely many ways, print the number modulo 10^9+7.

Constraints

  • 2 \leq N \leq 10^6
  • 1 \leq D \leq H \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N H D

Output

Print the number of ways to have H blocks on every square, modulo 10^9+7.


Sample Input 1

2 2 1

Sample Output 1

6

The possible transitions of (the number of blocks on Square 1, the number of blocks on Square 2) are as follows:

  • (0, 0) -> (0, 1) -> (1, 1) -> (1, 2) -> (2, 2)

  • (0, 0) -> (0, 1) -> (1, 1) -> (2, 1) -> (2, 2)

  • (0, 0) -> (0, 1) -> (2, 1) -> (2, 2)

  • (0, 0) -> (1, 0) -> (1, 1) -> (1, 2) -> (2, 2)

  • (0, 0) -> (1, 0) -> (1, 1) -> (2, 1) -> (2, 2)

  • (0, 0) -> (1, 0) -> (1, 2) -> (2, 2)

Thus, there are six ways to have two blocks on every square.


Sample Input 2

2 30 15

Sample Output 2

94182806

Sample Input 3

31415 9265 3589

Sample Output 3

312069529

Be sure to print the number modulo 10^9+7.

F - Diverta City

Time Limit: 3 sec / Memory Limit: 1024 MB

配点: 900

問題文

Diverta City は N 個の街からなる新しい都市であり、街には番号が 1, 2, ..., N と付けられています。

市長のりんごさんは、すべての 2 つの街の組を 1 つの双方向の道路で結ぶ計画です。それぞれの道路の長さはまだ決まっていません。

ある街から出発して、他のすべての街を一度ずつ訪れる経路を「ハミルトン経路」と呼びます。ここで、あるハミルトン経路を逆にたどった経路は、元のハミルトン経路と同じものとして扱います。

ハミルトン経路は N! / 2 種類あり、そのすべての全長 (経路上の道路の長さの合計) が異なるようにして、多様性のある都市をつくりたいです。

そのような道路の長さの組み合わせを 1 つ見つけてください。ただし、次の条件を満たさなければならないです。

  • 道路の長さはすべて 正の整数
  • 道路が長すぎても建設コストが大きくなるので、それぞれのハミルトン経路の全長を 10^{11} 以下にする

制約

  • N2 以上 10 以下の整数

入力

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

N

出力

目的を達成する道路の長さの組み合わせの一つを以下の形式で出力せよ。

w_{1, 1} \ w_{1, 2} \ w_{1, 3} \ ... \ w_{1, N}
w_{2, 1} \ w_{2, 2} \ w_{2, 3} \ ... \ w_{2, N}
  :  :     :
w_{N, 1} \ w_{N, 2} \ w_{N, 3} \ ... \ w_{N, N}

ここで、w_{i, j} は街 i と街 j を結ぶ道路であり、次の条件を満たす必要がある。

  • w_{i, i} = 0
  • w_{i, j} = w_{j, i} \ (i \neq j)
  • 1 \leq w_{i, j} \leq 10^{11} \ (i \neq j)

目的を達成する道路の長さの組み合わせが複数存在する場合は、そのうちのどれを出力しても正解となる。


入力例 1

3

出力例 1

0 6 15
6 0 21
15 21 0

ハミルトン経路は 3 種類あります。それぞれの歩行距離は以下のようになります。

  • 1 → 2 → 3: 歩行距離は 6 + 21 = 27
  • 1 → 3 → 2: 歩行距離は 15 + 21 = 36
  • 2 → 1 → 3: 歩行距離は 6 + 15 = 21

この 3 つの歩行距離はすべて異なるので、条件を満たします。


入力例 2

4

出力例 2

0 111 157 193
111 0 224 239
157 224 0 258
193 239 258 0

ハミルトン経路は 12 通りあり、歩行距離はすべて異なります。

Score : 900 points

Problem Statement

Diverta City is a new city consisting of N towns numbered 1, 2, ..., N.

The mayor Ringo is planning to connect every pair of two different towns with a bidirectional road. The length of each road is undecided.

A Hamiltonian path is a path that starts at one of the towns and visits each of the other towns exactly once. The reversal of a Hamiltonian path is considered the same as the original Hamiltonian path.

There are N! / 2 Hamiltonian paths. Ringo wants all these paths to have distinct total lengths (the sum of the lengths of the roads on a path), to make the city diverse.

Find one such set of the lengths of the roads, under the following conditions:

  • The length of each road must be a positive integer.
  • The maximum total length of a Hamiltonian path must be at most 10^{11}.

Constraints

  • N is a integer between 2 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print a set of the lengths of the roads that meets the objective, in the following format:

w_{1, 1} \ w_{1, 2} \ w_{1, 3} \ ... \ w_{1, N}
w_{2, 1} \ w_{2, 2} \ w_{2, 3} \ ... \ w_{2, N}
  :  :     :
w_{N, 1} \ w_{N, 2} \ w_{N, 3} \ ... \ w_{N, N}

where w_{i, j} is the length of the road connecting Town i and Town j, which must satisfy the following conditions:

  • w_{i, i} = 0
  • w_{i, j} = w_{j, i} \ (i \neq j)
  • 1 \leq w_{i, j} \leq 10^{11} \ (i \neq j)

If there are multiple sets of lengths of the roads that meet the objective, any of them will be accepted.


Sample Input 1

3

Sample Output 1

0 6 15
6 0 21
15 21 0

There are three Hamiltonian paths. The total lengths of these paths are as follows:

  • 1 → 2 → 3: The total length is 6 + 21 = 27.
  • 1 → 3 → 2: The total length is 15 + 21 = 36.
  • 2 → 1 → 3: The total length is 6 + 15 = 21.

They are distinct, so the objective is met.


Sample Input 2

4

Sample Output 2

0 111 157 193
111 0 224 239
157 224 0 258
193 239 258 0

There are 12 Hamiltonian paths, with distinct total lengths.