C - Sugar Water

Time Limit: 3 sec / Memory Limit: 256 MB

配点: 300

問題文

すぬけ君はビーカーに砂糖水を作ろうとしています。 最初ビーカーは空です。すぬけ君は以下の 4 種類の操作をそれぞれ何回でも行うことができます。一度も行わない操作があっても構いません。

  • 操作 1: ビーカーに水を 100A [g] 入れる。
  • 操作 2: ビーカーに水を 100B [g] 入れる。
  • 操作 3: ビーカーに砂糖を C [g] 入れる。
  • 操作 4: ビーカーに砂糖を D [g] 入れる。

すぬけ君の実験環境下では、水 100 [g] あたり砂糖は E [g] 溶けます。

すぬけ君はできるだけ濃度の高い砂糖水を作りたいと考えています。

ビーカーに入れられる物質の質量 (水の質量と砂糖の質量の合計) が F [g] 以下であり、 ビーカーの中に砂糖を溶け残らせてはいけないとき、 すぬけ君が作る砂糖水の質量と、それに溶けている砂糖の質量を求めてください。 答えが複数ある場合はどれを答えても構いません。

a [g] と砂糖 b [g] を混ぜた砂糖水の濃度は \frac{100b}{a + b} [%]です。 また、この問題では、砂糖が全く溶けていない水も濃度 0 [%] の砂糖水と考えることにします。

制約

  • 1 ≦ A < B ≦ 30
  • 1 ≦ C < D ≦ 30
  • 1≦ E ≦ 100
  • 100A ≦ F ≦ 3,000
  • A, B, C, D, E, F はすべて整数である。

入力

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

A B C D E F

出力

整数を空白区切りで 2 つ出力せよ。 1 つ目は求める砂糖水の質量、2 つ目はそれに溶けている砂糖の質量とせよ。


入力例 1

1 2 10 20 15 200

出力例 1

110 10

この入力例の状況では、水 100 [g] あたり砂糖は 15 [g] 溶けます。 また、ビーカーに物質を 200 [g] まで入れることができます。

操作 1 と操作 3 を 1 回ずつ行うことで 110 [g] の砂糖水を作ることができます。 また、これ以上濃度の高い砂糖水を作ることはできません。 たとえば、以下のような操作は条件を満たしません。

  • 操作 1 と操作 4 を 1 回ずつ行うと、ビーカーに砂糖が溶け残ってしまいます。
  • 操作 2 を 1 回と操作 3 を 3 回行うと、ビーカーの中の物質の量が 200 [g] を超えてしまいます。

入力例 2

1 2 1 2 100 1000

出力例 2

200 100

ほかに、たとえば以下の出力も正解となります。

400 200

一方、以下の出力は不正解となります。

300 150

なぜなら、砂糖が 150 [g] 溶けた 300 [g] の砂糖水を作るにはビーカーに水をちょうど 150 [g] 入れる必要がありますが、そのようなことは不可能だからです。


入力例 3

17 19 22 26 55 2802

出力例 3

2634 934

Score : 300 points

Problem Statement

Snuke is making sugar water in a beaker. Initially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations.

  • Operation 1: Pour 100A grams of water into the beaker.
  • Operation 2: Pour 100B grams of water into the beaker.
  • Operation 3: Put C grams of sugar into the beaker.
  • Operation 4: Put D grams of sugar into the beaker.

In our experimental environment, E grams of sugar can dissolve into 100 grams of water.

Snuke will make sugar water with the highest possible density.

The beaker can contain at most F grams of substances (water and sugar combined), and there must not be any undissolved sugar in the beaker. Find the mass of the sugar water Snuke will make, and the mass of sugar dissolved in it. If there is more than one candidate, any of them will be accepted.

We remind you that the sugar water that contains a grams of water and b grams of sugar is \frac{100b}{a + b} percent. Also, in this problem, pure water that does not contain any sugar is regarded as 0 percent density sugar water.

Constraints

  • 1 \leq A < B \leq 30
  • 1 \leq C < D \leq 30
  • 1 \leq E \leq 100
  • 100A \leq F \leq 3 000
  • A, B, C, D, E and F are all integers.

Inputs

Input is given from Standard Input in the following format:

A B C D E F

Outputs

Print two integers separated by a space. The first integer should be the mass of the desired sugar water, and the second should be the mass of the sugar dissolved in it.


Sample Input 1

1 2 10 20 15 200

Sample Output 1

110 10

In this environment, 15 grams of sugar can dissolve into 100 grams of water, and the beaker can contain at most 200 grams of substances.

We can make 110 grams of sugar water by performing Operation 1 once and Operation 3 once. It is not possible to make sugar water with higher density. For example, the following sequences of operations are infeasible:

  • If we perform Operation 1 once and Operation 4 once, there will be undissolved sugar in the beaker.
  • If we perform Operation 2 once and Operation 3 three times, the mass of substances in the beaker will exceed 200 grams.

Sample Input 2

1 2 1 2 100 1000

Sample Output 2

200 100

There are other acceptable outputs, such as:

400 200

However, the output below is not acceptable:

300 150

This is because, in order to make 300 grams of sugar water containing 150 grams of sugar, we need to pour exactly 150 grams of water into the beaker, which is impossible.


Sample Input 3

17 19 22 26 55 2802

Sample Output 3

2634 934
D - Restoring Road Network

Time Limit: 2 sec / Memory Limit: 256 MB

配点: 500

問題文

かつて存在した高橋王国には N 個の都市があり、いくつかの都市の組は道路で双方向に結ばれていました。 道路の構造は以下のようであったことがわかっています。

  • 都市間の移動は道路を通ってのみ行われ、どの都市からどの都市へも必要なら他の都市を経由することで移動できるようになっていた。
  • 道路の長さは道路によって異なっていたかもしれないが、全て正整数であった。

考古学者のすぬけ君は、高橋王国の遺跡で整数からなる N \times N の表 A を発見しました。 すぬけ君は、この表は高橋王国における都市間の道路に沿った最短距離を表した表ではないかと考えました。

すべての u, v について、Au 行目 v 列目の整数 A_{u, v} が都市 u から都市 v への最短経路の長さとなるような 道路の構造が存在するかどうか判定してください。 さらに、存在する場合、そのような道路の構造のうち、存在する道路の長さの和が最小となるようなものについて、その和を求めてください。

制約

  • 1≦N≦300
  • i ≠ j のとき、1 ≦ A_{i, j} = A_{j, i} ≦ 10^9
  • A_{i, i} = 0

入力

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

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}
...
A_{N, 1} A_{N, 2} ... A_{N, N}

出力

条件を満たす道路の構造が存在しない場合、-1 と出力せよ。 存在する場合、道路の長さの和の最小値を出力せよ。


入力例 1

3
0 1 3
1 0 2
3 2 0

出力例 1

3

条件を満たす道路の構造は以下のとおりです。

  • 都市 1 と都市 2 の間は長さ 1 の道路によって結ばれている。
  • 都市 2 と都市 3 の間は長さ 2 の道路によって結ばれている。
  • 都市 3 と都市 1 の間は道路で結ばれていない。

入力例 2

3
0 1 3
1 0 1
3 1 0

出力例 2

-1

都市 1 から都市 2 へ、および都市 2 から都市 3 へそれぞれ距離 1 で移動できることから、 都市 1 から都市 3 へは都市 2 を経由することで距離 2 で移動できることがわかります。 一方、都市 1 と都市 3 の間の最短距離は 3 でなければなりません。

よって条件を満たす道路の構造は存在しないことがわかります。


入力例 3

5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0

出力例 3

82

入力例 4

3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0

出力例 4

3000000000

Score : 500 points

Problem Statement

In Takahashi Kingdom, which once existed, there are N cities, and some pairs of cities are connected bidirectionally by roads. The following are known about the road network:

  • People traveled between cities only through roads. It was possible to reach any city from any other city, via intermediate cities if necessary.
  • Different roads may have had different lengths, but all the lengths were positive integers.

Snuke the archeologist found a table with N rows and N columns, A, in the ruin of Takahashi Kingdom. He thought that it represented the shortest distances between the cities along the roads in the kingdom.

Determine whether there exists a road network such that for each u and v, the integer A_{u, v} at the u-th row and v-th column of A is equal to the length of the shortest path from City u to City v. If such a network exist, find the shortest possible total length of the roads.

Constraints

  • 1 \leq N \leq 300
  • If i ≠ j, 1 \leq A_{i, j} = A_{j, i} \leq 10^9.
  • A_{i, i} = 0

Inputs

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}
...
A_{N, 1} A_{N, 2} ... A_{N, N}

Outputs

If there exists no network that satisfies the condition, print -1. If it exists, print the shortest possible total length of the roads.


Sample Input 1

3
0 1 3
1 0 2
3 2 0

Sample Output 1

3

The network below satisfies the condition:

  • City 1 and City 2 is connected by a road of length 1.
  • City 2 and City 3 is connected by a road of length 2.
  • City 3 and City 1 is not connected by a road.

Sample Input 2

3
0 1 3
1 0 1
3 1 0

Sample Output 2

-1

As there is a path of length 1 from City 1 to City 2 and City 2 to City 3, there is a path of length 2 from City 1 to City 3. However, according to the table, the shortest distance between City 1 and City 3 must be 3.

Thus, we conclude that there exists no network that satisfies the condition.


Sample Input 3

5
0 21 18 11 28
21 0 13 10 26
18 13 0 23 13
11 10 23 0 17
28 26 13 17 0

Sample Output 3

82

Sample Input 4

3
0 1000000000 1000000000
1000000000 0 1000000000
1000000000 1000000000 0

Sample Output 4

3000000000
E - Bichrome Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点: 700

問題文

N 頂点からなる木があります。頂点 1 は木の根であり、頂点 i (2 ≦ i ≦ N) の親は頂点 P_i です。

すぬけ君は、この木の各頂点に白または黒の色と非負整数の重みを割り当てることにしました。

すぬけ君にはお気に入りの数列 X_1, X_2, ..., X_N があります。そこで、色および重みの割り当てが、すべての v について以下の条件を満たすようにしたいと考えました。

  • 頂点 v を根とする部分木に含まれる頂点のうち、頂点 v と同じ色であるものの重みの和は X_v である。

ここで、頂点 v を根とする部分木とは、頂点 v およびそのすべての子孫からなる木を指すものとします。

このような色および重みの割り当てが可能かどうか判定してください。

制約

  • 1 ≦ N ≦ 1,000
  • 1 ≦ P_i ≦ i - 1
  • 0 ≦ X_i ≦ 5,000

入力

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

N
P_2 P_3 ... P_N
X_1 X_2 ... X_N

出力

条件を満たす色および重みの割り当てが可能である場合 POSSIBLE と、不可能である場合 IMPOSSIBLE と出力せよ。


入力例 1

3
1 1
4 3 2

出力例 1

POSSIBLE

たとえば、以下のような色と重みの割り当ては条件を満たします。

  • 頂点 1 の色を白、重みを 2 とする。
  • 頂点 2 の色を黒、重みを 3 とする。
  • 頂点 3 の色を白、重みを 2 とする。

他にも条件を満たす割り当て方は存在します。


入力例 2

3
1 2
1 2 3

出力例 2

IMPOSSIBLE

頂点 2 と頂点 3 に同じ色を割り当てる場合、頂点 2 に非負の重みを割り当てることができません。

頂点 2 と頂点 3 に異なる色を割り当てる場合、頂点 1 にどちらの色を割り当てても、非負の重みを割り当てることができません。

よって、条件を満たす色および重みの割り当て方は存在しません。


入力例 3

8
1 1 1 3 4 5 5
4 1 6 2 2 1 3 3

出力例 3

POSSIBLE

入力例 4

1

0

出力例 4

POSSIBLE

Score : 700 points

Problem Statement

We have a tree with N vertices. Vertex 1 is the root of the tree, and the parent of Vertex i (2 \leq i \leq N) is Vertex P_i.

To each vertex in the tree, Snuke will allocate a color, either black or white, and a non-negative integer weight.

Snuke has a favorite integer sequence, X_1, X_2, ..., X_N, so he wants to allocate colors and weights so that the following condition is satisfied for all v.

  • The total weight of the vertices with the same color as v among the vertices contained in the subtree whose root is v, is X_v.

Here, the subtree whose root is v is the tree consisting of Vertex v and all of its descendants.

Determine whether it is possible to allocate colors and weights in this way.

Constraints

  • 1 \leq N \leq 1 000
  • 1 \leq P_i \leq i - 1
  • 0 \leq X_i \leq 5 000

Inputs

Input is given from Standard Input in the following format:

N
P_2 P_3 ... P_N
X_1 X_2 ... X_N

Outputs

If it is possible to allocate colors and weights to the vertices so that the condition is satisfied, print POSSIBLE; otherwise, print IMPOSSIBLE.


Sample Input 1

3
1 1
4 3 2

Sample Output 1

POSSIBLE

For example, the following allocation satisfies the condition:

  • Set the color of Vertex 1 to white and its weight to 2.
  • Set the color of Vertex 2 to black and its weight to 3.
  • Set the color of Vertex 3 to white and its weight to 2.

There are also other possible allocations.


Sample Input 2

3
1 2
1 2 3

Sample Output 2

IMPOSSIBLE

If the same color is allocated to Vertex 2 and Vertex 3, Vertex 2 cannot be allocated a non-negative weight.

If different colors are allocated to Vertex 2 and 3, no matter which color is allocated to Vertex 1, it cannot be allocated a non-negative weight.

Thus, there exists no allocation of colors and weights that satisfies the condition.


Sample Input 3

8
1 1 1 3 4 5 5
4 1 6 2 2 1 3 3

Sample Output 3

POSSIBLE

Sample Input 4

1

0

Sample Output 4

POSSIBLE
F - Collecting Balls

Time Limit: 2 sec / Memory Limit: 256 MB

配点: 1200

問題文

xy 平面上に 2N 個のボールがあります。このうち i 番目のボールの位置は (x_i, y_i) です。 ここで、すべての i について x_i, y_i1 以上 N 以下の整数であり、2 つ以上のボールが同じ位置にあることはありません。

すぬけ君は、これらのボールを回収するために、タイプ A, B のロボットを N 台ずつ用意し、 位置 (1, 0), (2, 0), ..., (N, 0) にタイプ A のロボットを、位置 (0, 1), (0, 2), ..., (0, N) にタイプ B のロボットをそれぞれ 1 台ずつ設置しました。

それぞれのタイプのロボットは起動されると以下のように動作します。

  • タイプ A のロボットは、位置 (a, 0) で起動されると、直線 x = a 上にあるボールのうち y 座標の値が最小のものを回収して停止する。そのようなボールが存在しない場合は何もせずに停止する。
  • タイプ B のロボットは、位置 (0, b) で起動されると、直線 y = b 上にあるボールのうち x 座標の値が最小のものを回収して停止する。そのようなボールが存在しない場合は何もせずに停止する。

一度停止したロボットをもう一度起動することはできません。また、動作中のロボットがある場合、それが停止するまで新たなロボットを起動することはできません。

すぬけ君は、ロボットを起動しようとして、起動する順序によってはすべてのボールを回収することができない可能性があることに気づきました。

ロボットを起動する順序として考えられる (2N)! 通りのうち、すべてのボールを回収できるような順序の個数を 1,000,000,007 で割った余りを求めてください。

制約

  • 2 ≦ N ≦ 10^5
  • 1 ≦ x_i ≦ N
  • 1 ≦ y_i ≦ N
  • i ≠ j のとき、x_i ≠ x_j または y_i ≠ y_j

入力

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

N
x_1 y_1
...
x_{2N} y_{2N}

出力

すべてのボールを回収できるようなロボットの起動順序の個数を 1,000,000,007 で割った余りを出力せよ。


入力例 1

2
1 1
1 2
2 1
2 2

出力例 1

8

位置 (1,0), (2, 0) に設置されたロボットをそれぞれ A1, A2 と呼び、位置 (0, 1), (0, 2) に設置されたロボットをそれぞれ B1, B2 と呼ぶことにします。 このとき、条件を満たす起動順序は以下の 8 通りあります。

  • A1, B1, A2, B2
  • A1, B1, B2, A2
  • A1, B2, B1, A2
  • A2, B1, A1, B2
  • B1, A1, B2, A2
  • B1, A1, A2, B2
  • B1, A2, A1, B2
  • B2, A1, B1, A2

よって 8 を出力します。


入力例 2

4
3 2
1 2
4 1
4 2
2 2
4 4
2 1
1 3

出力例 2

7392

入力例 3

4
1 1
2 2
3 3
4 4
1 2
2 1
3 4
4 3

出力例 3

4480

入力例 4

8
6 2
5 1
6 8
7 8
6 5
5 7
4 3
1 4
7 6
8 3
2 8
3 6
3 2
8 5
1 5
5 8

出力例 4

82060779

入力例 5

3
1 1
1 2
1 3
2 1
2 2
2 3

出力例 5

0

条件を満たす順序が存在しない場合、答えは 0 となります。

Score : 1200 points

Problem Statement

There are 2N balls in the xy-plane. The coordinates of the i-th of them is (x_i, y_i). Here, x_i and y_i are integers between 1 and N (inclusive) for all i, and no two balls occupy the same coordinates.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B. Then, he placed the type-A robots at coordinates (1, 0), (2, 0), ..., (N, 0), and the type-B robots at coordinates (0, 1), (0, 2), ..., (0, N), one at each position.

When activated, each type of robot will operate as follows.

  • When a type-A robot is activated at coordinates (a, 0), it will move to the position of the ball with the lowest y-coordinate among the balls on the line x = a, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

  • When a type-B robot is activated at coordinates (0, b), it will move to the position of the ball with the lowest x-coordinate among the balls on the line y = b, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Once deactivated, a robot cannot be activated again. Also, while a robot is operating, no new robot can be activated until the operating robot is deactivated.

When Snuke was about to activate a robot, he noticed that he may fail to collect all the balls, depending on the order of activating the robots.

Among the (2N)! possible orders of activating the robots, find the number of the ones such that all the balls can be collected, modulo 1 000 000 007.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq x_i \leq N
  • 1 \leq y_i \leq N
  • If i ≠ j, either x_i ≠ x_j or y_i ≠ y_j.

Inputs

Input is given from Standard Input in the following format:

N
x_1 y_1
...
x_{2N} y_{2N}

Outputs

Print the number of the orders of activating the robots such that all the balls can be collected, modulo 1 000 000 007.


Sample Input 1

2
1 1
1 2
2 1
2 2

Sample Output 1

8

We will refer to the robots placed at (1, 0) and (2, 0) as A1 and A2, respectively, and the robots placed at (0, 1) and (0, 2) as B1 and B2, respectively. There are eight orders of activation that satisfy the condition, as follows:

  • A1, B1, A2, B2
  • A1, B1, B2, A2
  • A1, B2, B1, A2
  • A2, B1, A1, B2
  • B1, A1, B2, A2
  • B1, A1, A2, B2
  • B1, A2, A1, B2
  • B2, A1, B1, A2

Thus, the output should be 8.


Sample Input 2

4
3 2
1 2
4 1
4 2
2 2
4 4
2 1
1 3

Sample Output 2

7392

Sample Input 3

4
1 1
2 2
3 3
4 4
1 2
2 1
3 4
4 3

Sample Output 3

4480

Sample Input 4

8
6 2
5 1
6 8
7 8
6 5
5 7
4 3
1 4
7 6
8 3
2 8
3 6
3 2
8 5
1 5
5 8

Sample Output 4

82060779

Sample Input 5

3
1 1
1 2
1 3
2 1
2 2
2 3

Sample Output 5

0

When there is no order of activation that satisfies the condition, the output should be 0.