A - T or T

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

私たちは N 人で旅行しようとしており、その交通手段として電車とタクシーがあります。

電車を使うと 1 人あたり A 円かかります。

タクシーを使うと N 人で B 円かかります。

全員の交通費の合計は最小でいくらになるでしょうか。

制約

  • 入力は全て整数である。
  • 1 \leq N \leq 20
  • 1 \leq A \leq 50
  • 1 \leq B \leq 50

入力

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

N A B

出力

最小の合計交通費を表す整数を出力せよ。


入力例 1

4 2 9

出力例 1

8

電車を使うと 4 \times 2 = 8 円かかり、タクシーを使うと 9 円かかるので、全員の交通費の合計の最小値は 8 円です。


入力例 2

4 2 7

出力例 2

7

入力例 3

4 2 8

出力例 3

8

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 20
  • 1 \leq A \leq 50
  • 1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.


Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.


Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8
B - Good Distance

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

D 次元空間上に N 個の点があります。

i 番目の点の座標は (X_{i1}, X_{i2}, ..., X_{iD}) です。

座標 (y_1, y_2, ..., y_D) の点と座標 (z_1, z_2, ..., z_D) の点の距離は \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2} です。

i 番目の点と j 番目の点の距離が整数となるような組 (i, j) (i < j) はいくつあるでしょうか。

制約

  • 入力は全て整数である。
  • 2 \leq N \leq 10
  • 1 \leq D \leq 10
  • -20 \leq X_{ij} \leq 20
  • 同じ座標の点は与えられない。すなわち、i \neq j ならば X_{ik} \neq X_{jk} なる k が存在する。

入力

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

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

出力

i 番目の点と j 番目の点の距離が整数となるような組 (i, j) (i < j) の数を出力せよ。


入力例 1

3 2
1 2
5 5
-2 8

出力例 1

1

以下のように距離が整数となる点の組は 1 組です。

  • 1 番目の点と 2 番目の点の距離は \sqrt{|1-5|^2 + |2-5|^2} = 5 で、これは整数です。
  • 2 番目の点と 3 番目の点の距離は \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58} で、これは整数ではありません。
  • 3 番目の点と 1 番目の点の距離は \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5} で、これは整数ではありません。

入力例 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

出力例 2

2

入力例 3

5 1
1
2
3
4
5

出力例 3

10

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 10
  • 1 \leq D \leq 10
  • -20 \leq X_{ij} \leq 20
  • No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.


Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

  • The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.
  • The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.
  • The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10
C - Remainder Minimization 2019

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

非負整数 L, R が与えられます。 2 つの整数 i, jL \leq i < j \leq R を満たすように選びます。 (i \times j) \text{ mod } 2019 の最小値を求めてください。

制約

  • 入力は全て整数
  • 0 \leq L < R \leq 2 \times 10^9

入力

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

L R

出力

条件を満たすように i, j を選んだ時の、(i \times j) \text{ mod } 2019 の最小値を出力せよ。


入力例 1

2020 2040

出力例 1

2

(i, j) = (2020, 2021) とすると、(i \times j) \text{ mod } 2019 = 2 となります。


入力例 2

4 5

出力例 2

20

選び方は (i, j) = (4, 5)1 通りしか存在しません。

Score : 300 points

Problem Statement

You are given two non-negative integers L and R. We will choose two integers i and j such that L \leq i < j \leq R. Find the minimum possible value of (i \times j) \text{ mod } 2019.

Constraints

  • All values in input are integers.
  • 0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \text{ mod } 2019 when i and j are chosen under the given condition.


Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \text{ mod } 2019 = 2.


Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).

D - Rain Flows into Dams

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

円形に N 個の山が連なっており、時計回りに山 1, 山 2, , 山 N と呼ばれます。N奇数です。

これらの山の間に N 個のダムがあり、ダム 1, ダム 2, , ダム N と呼ばれます。ダム i (1 \leq i \leq N) は山 i と山 i+1 の間にあります (山 N+1 は山 1 のことを指します)。

i (1 \leq i \leq N) に 2x リットルの雨が降ると、ダム i-1 とダム i にそれぞれ x リットルずつ水が溜まります (ダム 0 はダム N のことを指します)。

ある日、各山に非負の偶数リットルの雨が降りました。

その結果、ダム i (1 \leq i \leq N) には合計で A_i リットルの水が溜まりました。

各山に降った雨の量を求めてください。この問題の制約下では解が一意に定まることが証明できます。

制約

  • 入力は全て整数である。
  • 3 \leq N \leq 10^5-1
  • N は奇数である。
  • 0 \leq A_i \leq 10^9
  • 入力が表す状況は、各山に非負の偶数リットルの雨が降った際に発生しうる。

入力

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

N
A_1 A_2 ... A_N

出力

1, 山 2, , 山 N に降った雨の量を表す N 個の整数をこの順に出力せよ。


入力例 1

3
2 2 4

出力例 1

4 0 4

1, 2, 3 に降った雨の量をそれぞれ 4 リットル, 0 リットル, 4 リットルとすると以下のように辻褄が合います。

  • ダム 1 には \frac{4}{2} + \frac{0}{2} = 2 リットルの水が溜まります。
  • ダム 2 には \frac{0}{2} + \frac{4}{2} = 2 リットルの水が溜まります。
  • ダム 3 には \frac{4}{2} + \frac{4}{2} = 4 リットルの水が溜まります。

入力例 2

5
3 8 7 5 5

出力例 2

2 4 12 2 8

入力例 3

3
1000000000 1000000000 0

出力例 3

0 2000000000 0

Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

  • All values in input are integers.
  • 3 \leq N \leq 10^5-1
  • N is an odd number.
  • 0 \leq A_i \leq 10^9
  • The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.


Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

  • Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.
  • Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.
  • Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0
E - Virus Tree 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点、N-1 辺を持つ木が与えられます。 頂点には番号 1,2,...,N がつけられており、i 番目の辺は頂点 a_i,b_i をつないでいます。

あなたは K 色の絵の具を持っています。 木の頂点それぞれに対して、以下の条件を満たすように、K 色の中から 1 色を選んで塗ることにしました。

  • 二つの異なる頂点 x,y 間の距離が 2 以下ならば、頂点 x の色と頂点 y の色は異なる。

木を塗り分ける方法は何通りあるでしょうか。 総数を 1,000,000,007 で割った余りを求めてください。

木とは

木とはグラフの一種です。詳しくはこちらをご覧ください: Wikipedia「木 (数学)」

距離とは

二つの頂点 x,y 間の距離とは、x から y に到達する際にたどる必要のある最小の辺数です。

制約

  • 1 \leqq N,K \leqq 10^5
  • 1 \leqq a_i,b_i \leqq N
  • 与えられるグラフは木である。

入力

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

N K
a_1 b_1
a_2 b_2
.
.
.
a_{N-1} b_{N-1}

出力

木の塗り分け方の総数を 1,000,000,007 で割った余りを出力してください。


入力例 1

4 3
1 2
2 3
3 4

出力例 1

6

zu

塗り分け方は 6 通りです。


入力例 2

5 4
1 2
1 3
1 4
4 5

出力例 2

48

入力例 3

16 22
12 1
3 1
4 16
7 12
6 2
2 15
5 16
14 16
10 11
3 10
3 13
8 6
16 8
9 12
4 3

出力例 3

271414432

Score : 500 points

Problem Statement

You are given a tree with N vertices and N-1 edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex a_i and b_i.

You have coloring materials of K colors. For each vertex in the tree, you will choose one of the K colors to paint it, so that the following condition is satisfied:

  • If the distance between two different vertices x and y is less than or equal to two, x and y have different colors.

How many ways are there to paint the tree? Find the count modulo 1\ 000\ 000\ 007.

What is tree? A tree is a kind of graph. For detail, please see: Wikipedia "Tree (graph theory)"

What is distance? The distance between two vertices x and y is the minimum number of edges one has to traverse to get from x to y.

Constraints

  • 1 \leq N,K \leq 10^5
  • 1 \leq a_i,b_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N K
a_1 b_1
a_2 b_2
.
.
.
a_{N-1} b_{N-1}

Output

Print the number of ways to paint the tree, modulo 1\ 000\ 000\ 007.


Sample Input 1

4 3
1 2
2 3
3 4

Sample Output 1

6

Figure

There are six ways to paint the tree.


Sample Input 2

5 4
1 2
1 3
1 4
4 5

Sample Output 2

48

Sample Input 3

16 22
12 1
3 1
4 16
7 12
6 2
2 15
5 16
14 16
10 11
3 10
3 13
8 6
16 8
9 12
4 3

Sample Output 3

271414432
F - Colorful Tree

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N までの番号がつけられた N 個の頂点を持つ木があります。 この木の i 番目の辺は頂点 a_i と頂点 b_i を結び、その色は c_i、長さは d_i です。 ここで各辺の色は 1 以上 N-1 以下の整数で表されており、同じ整数は同じ色に、異なる整数は異なる色に対応します。

以下の Q 個の問いに答えてください。

  • j (1 \leq j \leq Q): 色 x_j のすべての辺の長さが y_j に変更されたと仮定して、二頂点 u_j, v_j 間の距離を求めよ。(辺の長さの変更はこれ以降の問いには影響しない。)

制約

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq a_i, b_i \leq N
  • 1 \leq c_i \leq N-1
  • 1 \leq d_i \leq 10^4
  • 1 \leq x_j \leq N-1
  • 1 \leq y_j \leq 10^4
  • 1 \leq u_j < v_j \leq N
  • 与えられるグラフは木である。
  • 入力中のすべての値は整数である。

入力

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

N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q

出力

Q 行出力せよ。j 行目 (1 \leq j \leq Q) に問 j への回答を出力すること。


入力例 1

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

出力例 1

130
200
60

この入力中のグラフは次のようなものです。

図

ここで、色 1 の辺は赤い実線で、色 2 の辺は緑の太線で、色 4 の辺は青い破線で示されています。

  • 1: 色 1 のすべての辺の長さが 100 に変更されたと仮定すると、頂点 1, 4 間の距離は 100 + 30 = 130 です。
  • 2: 色 1 のすべての辺の長さが 100 に変更されたと仮定すると、頂点 1, 5 間の距離は 100 + 100 = 200 です。
  • 3: 色 3 のすべての辺の長さが 1000 に変更されたと仮定すると (そのような辺は存在しません)、頂点 3, 4 間の距離は 20 + 10 + 30 = 60 です。この問いでは色 1 の辺の長さが元に戻っていることに注意してください。

Score : 600 points

Problem Statement

There is a tree with N vertices numbered 1 to N. The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively. Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.

Answer the following Q queries:

  • Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.)

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq a_i, b_i \leq N
  • 1 \leq c_i \leq N-1
  • 1 \leq d_i \leq 10^4
  • 1 \leq x_j \leq N-1
  • 1 \leq y_j \leq 10^4
  • 1 \leq u_j < v_j \leq N
  • The given graph is a tree.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q

Output

Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j.


Sample Input 1

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

Sample Output 1

130
200
60

The graph in this input is as follows:

Figure

Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line.

  • Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.
  • Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.
  • Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths.