A - Two Integers

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

正整数 XY が与えられます。 X の倍数であって Y の倍数でない 10^{18} 以下の正整数が存在すれば一つ選んで出力してください。 存在しない場合は -1 を出力してください。

制約

  • 1 ≤ X,Y ≤ 10^9
  • X,Y は整数

入力

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

X Y

出力

X の倍数であって Y の倍数でない 10^{18} 以下の正整数を一つ出力せよ。 存在しない場合は -1 を出力せよ。


入力例 1

8 6

出力例 1

16

例えば 168 の倍数であって 6 の倍数ではありません。


入力例 2

3 3

出力例 2

-1

3 の倍数は 3 の倍数です。

Score : 100 points

Problem Statement

You are given positive integers X and Y. If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it. If it does not exist, print -1.

Constraints

  • 1 ≤ X,Y ≤ 10^9
  • X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.


Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.


Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.

B - Two Arrays

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

長さ N の数列 a_1,a_2,..,a_Nb_1,b_2,..,b_N が与えられます。 以下の操作を 0 回以上好きなだけ繰り返して、数列 ab を一致させられるか判定してください。

操作: 1 以上 N 以下の整数 i,j (一致していてもよい)を選び、次の2つのことを同時に行う。

  • a_i2 を足す
  • b_j1 を足す

制約

  • 1 ≤ N ≤ 10,000
  • 0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)
  • 入力は全て整数

入力

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

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

出力

操作を 0 回以上繰り返して数列 ab を一致させられるなら Yes を、 そうでないなら No を出力せよ。


入力例 1

3
1 2 3
5 2 2

出力例 1

Yes

例えば、次のように 3 回操作すればよいです。

  • 1 回目: i=1,j=2. これによって a = \{3,2,3\}, b = \{5,3,2\} となります。
  • 2 回目: i=1,j=2. これによって a = \{5,2,3\}, b = \{5,4,2\} となります。
  • 3 回目: i=2,j=3. これによって a = \{5,4,3\}, b = \{5,4,3\} となります。

入力例 2

5
3 1 4 1 5
2 7 1 8 2

出力例 2

No

入力例 3

5
2 7 1 8 2
3 1 4 1 5

出力例 3

No

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N. Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

  • Add 2 to a_i.
  • Add 1 to b_j.

Constraints

  • 1 ≤ N ≤ 10 000
  • 0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.


Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

  • First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.
  • Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.
  • Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No
C - Vacant Seat

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

これはインタラクティブな問題です。

N3 以上の奇数とします。

N 個の席が円状に並んでいます。 席には 0 から N - 1 まで番号が振られています。 各 i (0 ≤ i ≤ N - 2) について、席 i と席 i + 1 は隣り合っています。 また、席 N - 1 と席 0 は隣り合っています。

各席の状態は「空席」「男性が座っている」「女性が座っている」のどれかです。 ただし、同性どうしが隣り合う席に座っていることはありません。 N3 以上の奇数の場合、空席が少なくとも 1 つは存在することが示せます。

あなたには N のみが与えられ、各席の状態は与えられません。 あなたの目標は、どれか 1 つの空席の番号を当てることです。 そのために、あなたは次のクエリを繰り返し送ることができます。

  • 整数 i (0 ≤ i ≤ N - 1) を選ぶ。 席 i が空席ならば、正答となる。 そうでなければ、席 i に座っている人の性別が知らされる。

クエリを高々 20 回まで送ることで、どれか 1 つの空席の番号を当ててください。

制約

  • N は奇数である。
  • 3 ≤ N ≤ 99,999

入出力

最初に、N が次の形式で標準入力から与えられる。

N

次に、クエリを繰り返し送る。 クエリは次の形式で標準出力へ出力する。 行末には改行を出力せよ。

i

これに対するクエリの答えは、次の形式で標準入力から与えられる。

s

sVacant, Male, Female のどれかである。 これらはそれぞれ、席 i の状態が「空席」「男性が座っている」「女性が座っている」であることを表す。

注意

  • 出力の度に標準出力を flush せよ。 そうしない場合、TLE の可能性がある。
  • sVacant の場合、すぐにプログラムを終了せよ。 そうしない場合、ジャッジ結果は不定である。
  • クエリ回数が 20 を超えた場合、およびクエリの形式が正しくない場合、ジャッジ結果は不定である。

入出力例 1

このサンプルでは、N = 3 であり、席 0, 1, 2 の状態はそれぞれ「男性が座っている」「女性が座っている」「空席」である。

InputOutput
3
0
Male
1
Female
2
Vacant

Score : 500 points

Problem Statement

This is an interactive task.

Let N be an odd number at least 3.

There are N seats arranged in a circle. The seats are numbered 0 through N-1. For each i (0 ≤ i ≤ N - 2), Seat i and Seat i + 1 are adjacent. Also, Seat N - 1 and Seat 0 are adjacent.

Each seat is either vacant, or oppupied by a man or a woman. However, no two adjacent seats are occupied by two people of the same sex. It can be shown that there is at least one empty seat where N is an odd number at least 3.

You are given N, but the states of the seats are not given. Your objective is to correctly guess the ID number of any one of the empty seats. To do so, you can repeatedly send the following query:

  • Choose an integer i (0 ≤ i ≤ N - 1). If Seat i is empty, the problem is solved. Otherwise, you are notified of the sex of the person in Seat i.

Guess the ID number of an empty seat by sending at most 20 queries.

Constraints

  • N is an odd number.
  • 3 ≤ N ≤ 99 999

Input and Output

First, N is given from Standard Input in the following format:

N

Then, you should send queries. A query should be printed to Standart Output in the following format. Print a newline at the end.

i

The response to the query is given from Standard Input in the following format:

s

Here, s is Vacant, Male or Female. Each of these means that Seat i is empty, occupied by a man and occupied by a woman, respectively.

Notice

  • Flush Standard Output each time you print something. Failure to do so may result in TLE.
  • Immediately terminate the program when s is Vacant. Otherwise, the verdict is indeterminate.
  • The verdict is indeterminate if more than 20 queries or ill-formatted queries are sent.

Sample Input / Output 1

In this sample, N = 3, and Seat 0, 1, 2 are occupied by a man, occupied by a woman and vacant, respectively.

InputOutput
3
0
Male
1
Female
2
Vacant
D - Forest

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 600

問題文

N 頂点 M 辺の森が与えられます。頂点には 0 から N-1 の番号がついています。 辺は (x_i,y_i) の形で与えられます。これは頂点 x_iy_i が辺でつながっていることを意味します。

各頂点 i には a_i という値が定まっています。 あなたは与えられた森に辺を追加して連結にしたいです。 辺を追加するときには、まず異なる頂点二つを選択し( i , j とする)、 ij の間に辺を張ります。 この時コストが a_i+a_j かかります。そしてこれ以降,頂点 ij は永遠に選択できなくなります。

森を連結にする最小コストを求めてください。 連結にするのが不可能な場合はImpossibleと出力してください。

制約

  • 1 ≤ N ≤ 100,000
  • 0 ≤ M ≤ N-1
  • 1 ≤ a_i ≤ 10^9
  • 0 ≤ x_i,y_i ≤ N-1
  • 与えられるグラフは森
  • 入力は全て整数

入力

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

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

出力

森を連結にする最小コストを出力せよ。ただし、不可能な場合はImpossibleと出力せよ。


入力例 1

7 5
1 2 3 4 5 6 7
3 0
4 0
1 2
1 3
5 6

出力例 1

7

頂点 0, 5 をつなぐとグラフが連結になり,この時かかるコストは 1 + 6 = 7 です。


入力例 2

5 0
3 1 4 1 5

出力例 2

Impossible

どのように辺を追加してもグラフを連結にすることはできません。


入力例 3

1 0
5

出力例 3

0

最初からグラフは連結であるので,辺を追加する必要はありません。

Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1. The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i. You want to add edges in the given forest so that the forest becomes connected. To add an edge, you choose two different vertices i and j, then span an edge between i and j. This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

  • 1 ≤ N ≤ 100,000
  • 0 ≤ M ≤ N-1
  • 1 ≤ a_i ≤ 10^9
  • 0 ≤ x_i,y_i ≤ N-1
  • The given graph is a forest.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.


Sample Input 1

7 5
1 2 3 4 5 6 7
3 0
4 0
1 2
1 3
5 6

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.


Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.


Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.

E - Antennas on Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 900

問題文

N 頂点の木があります。 頂点には 0 から N - 1 まで番号が振られています。 また、i (0 ≤ i < N - 1) 番目の辺は頂点 a_ib_i を結んでいます。 各頂点対 u, v (0 ≤ u, v < N) に対して、距離 d(u, v) を「パス u-v に含まれる辺の本数」と定義します。

近い将来、いずれか 1 個の頂点に宇宙人が襲来することが予想されています。 すぬけ君は、宇宙人が襲来したときにその頂点をすぐに特定したいと考えています。 そのために、あらかじめいくつかの頂点にアンテナを設置しておくことにしました。

まず、アンテナの個数 K (1 ≤ K ≤ N) を自由に決めます。 そして、すべて相異なる K 個の頂点 x_0, x_1, ..., x_{K - 1} を自由に選び、各頂点にアンテナ 0, 1, ..., K - 1 を設置します。 頂点 v に宇宙人が襲来すると、アンテナ k (0 ≤ k < K) は距離 d(x_k, v) を出力します。 すぬけ君は、これら K 個の出力結果をもとに、宇宙人が襲来した頂点を特定します。 よって、どの頂点に宇宙人が襲来してもその頂点を一意に特定するためには、次の条件が成り立つ必要があります。

  • 各頂点 u (0 ≤ u < N) に対してベクトル (d(x_0, u), ..., d(x_{K - 1}, u)) を考えたとき、これら N 通りのベクトルはすべて相異なる。

条件を満たすようにアンテナを設置するとき、アンテナの個数 K の最小値を求めてください。

制約

  • 2 ≤ N ≤ 10^5
  • 0 ≤ a_i, b_i < N
  • 与えられるグラフは木である。

入力

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

N
a_0 b_0
a_1 b_1
:
a_{N - 2} b_{N - 2}

出力

条件を満たすようにアンテナを設置するとき、アンテナの個数 K の最小値を出力せよ。


入力例 1

5
0 1
0 2
0 3
3 4

出力例 1

2

例えば、頂点 1, 3 にアンテナを設置すればよいです。 このとき、次の 5 通りのベクトルはすべて相異なります。

  • (d(1, 0), d(3, 0)) = (1, 1)
  • (d(1, 1), d(3, 1)) = (0, 2)
  • (d(1, 2), d(3, 2)) = (2, 2)
  • (d(1, 3), d(3, 3)) = (2, 0)
  • (d(1, 4), d(3, 4)) = (3, 1)

入力例 2

2
0 1

出力例 2

1

例えば、頂点 0 にアンテナを設置すればよいです。


入力例 3

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

出力例 3

3

例えば、頂点 0, 4, 9 にアンテナを設置すればよいです。

Score : 900 points

Problem Statement

We have a tree with N vertices. The vertices are numbered 0 through N - 1, and the i-th edge (0 ≤ i < N - 1) comnnects Vertex a_i and b_i. For each pair of vertices u and v (0 ≤ u, v < N), we define the distance d(u, v) as the number of edges in the path u-v.

It is expected that one of the vertices will be invaded by aliens from outer space. Snuke wants to immediately identify that vertex when the invasion happens. To do so, he has decided to install an antenna on some vertices.

First, he decides the number of antennas, K (1 ≤ K ≤ N). Then, he chooses K different vertices, x_0, x_1, ..., x_{K - 1}, on which he installs Antenna 0, 1, ..., K - 1, respectively. If Vertex v is invaded by aliens, Antenna k (0 ≤ k < K) will output the distance d(x_k, v). Based on these K outputs, Snuke will identify the vertex that is invaded. Thus, in order to identify the invaded vertex no matter which one is invaded, the following condition must hold:

  • For each vertex u (0 ≤ u < N), consider the vector (d(x_0, u), ..., d(x_{K - 1}, u)). These N vectors are distinct.

Find the minumum value of K, the number of antennas, when the condition is satisfied.

Constraints

  • 2 ≤ N ≤ 10^5
  • 0 ≤ a_i, b_i < N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_0 b_0
a_1 b_1
:
a_{N - 2} b_{N - 2}

Output

Print the minumum value of K, the number of antennas, when the condition is satisfied.


Sample Input 1

5
0 1
0 2
0 3
3 4

Sample Output 1

2

For example, install an antenna on Vertex 1 and 3. Then, the following five vectors are distinct:

  • (d(1, 0), d(3, 0)) = (1, 1)
  • (d(1, 1), d(3, 1)) = (0, 2)
  • (d(1, 2), d(3, 2)) = (2, 2)
  • (d(1, 3), d(3, 3)) = (2, 0)
  • (d(1, 4), d(3, 4)) = (3, 1)

Sample Input 2

2
0 1

Sample Output 2

1

For example, install an antenna on Vertex 0.


Sample Input 3

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

Sample Output 3

3

For example, install an antenna on Vertex 0, 4, 9.

F - XOR Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

N 頂点の木が与えられます。頂点には 0 から N-1 の番号がついています。 辺は 1 から N-1 までの番号がついていて、辺 i は頂点 x_iy_i をつなぎ、また a_i という値を保持しています。 あなたは以下の操作を何回でもすることが出来ます:

  • ある単純pathとある非負整数 x を選び、そのpathを構成する各辺 e について、 a_e ← a_e ⊕ x (⊕ は xor)と変化させる。

目標はすべての辺 e について a_e = 0 とすることです。 目標を達成するために必要な最小の操作回数を求めてください。

制約

  • 2 ≤ N ≤ 10^5
  • 0 ≤ x_i,y_i ≤ N-1
  • 0 ≤ a_i ≤ 15
  • 与えられるグラフは木
  • 入力は全て整数

入力

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

N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_{N-1} y_{N-1} a_{N-1}

出力

目標を達成するために必要な操作回数の最小値を出力せよ。


入力例 1

5
0 1 1
0 2 3
0 3 6
3 4 4

出力例 1

3

以下のようにして三回で目標を達成できます。

  • まず、頂点 1,2 を結ぶ path に対して x=1 で操作する
  • 次に、頂点 2,3 を結ぶ path に対して x=2 で操作する
  • 最後に、頂点 0,4 を結ぶ path に対して x=4 で操作する

入力例 2

2
1 0 0

出力例 2

0

Score : 1000 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 0 through N-1, and the edges are numbered 1 through N-1. Edge i connects Vertex x_i and y_i, and has a value a_i. You can perform the following operation any number of times:

  • Choose a simple path and a non-negative integer x, then for each edge e that belongs to the path, change a_e by executing a_e ← a_e ⊕ x (⊕ denotes XOR).

Your objective is to have a_e = 0 for all edges e. Find the minimum number of operations required to achieve it.

Constraints

  • 2 ≤ N ≤ 10^5
  • 0 ≤ x_i,y_i ≤ N-1
  • 0 ≤ a_i ≤ 15
  • The given graph is a tree.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_{N-1} y_{N-1} a_{N-1}

Output

Find the minimum number of operations required to achieve the objective.


Sample Input 1

5
0 1 1
0 2 3
0 3 6
3 4 4

Sample Output 1

3

The objective can be achieved in three operations, as follows:

  • First, choose the path connecting Vertex 1, 2, and x = 1.
  • Then, choose the path connecting Vertex 2, 3, and x = 2.
  • Lastly, choose the path connecting Vertex 0, 4, and x = 4.

Sample Input 2

2
1 0 0

Sample Output 2

0
G - Colorful Doors

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 2000

問題文

左岸と右岸を繋ぐ橋があります。 橋の上の相異なる 2 N 箇所には、それぞれある色のドアが置かれています。 各ドアの色は 1 から N までの整数で表されます。 各 k (1 \leq k \leq N) について、色 k のドアはちょうど 2 個存在します。

すぬけ君は、左岸から右岸へ橋を渡ることにしました。 すぬけ君は常に右へ向かって歩き続けますが、その間に次の現象が起こります。

  • すぬけ君が色 k (1 \leq k \leq N) のドアに触れた瞬間、すぬけ君は色 k の他方のドアのすぐ右へワープする。

すぬけ君はいずれ右岸に辿り着くことが示せます。

i (1 \leq i \leq 2 N - 1) について、左から i 番目および i + 1 番目のドアに挟まれた区間を、区間 i と呼ぶことにします。 橋を渡り終えたすぬけ君は、各 i (1 \leq i \leq 2 N - 1) について、区間 i を歩いたか否かを記録しました。 この記録が、長さ 2 N - 1 の文字列 s として与えられます。 各 i (1 \leq i \leq 2 N - 1) について、すぬけ君が区間 i を歩いたならば、si 文字目は 1 であり、そうでないならば、si 文字目は 0 です。

図: 入力例 3 に対応するドアの配置の例

記録に矛盾しないようなドアの配置が存在するか判定し、存在するならばひとつ構成してください。

制約

  • 1 \leq N \leq 10^5
  • |s| = 2 N - 1
  • s01 のみからなる。

入力

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

N
s

出力

記録に矛盾しないようなドアの配置が存在しないならば、No を出力せよ。 存在するならば、1 行目に Yes を出力し、2 行目にドアの配置をひとつ次のフォーマットで出力せよ。 ただし、各 i (1 \leq i \leq 2 N) について、c_i は左から i 番目のドアの色である。

c_1 c_2 ... c_{2 N}

入力例 1

2
010

出力例 1

Yes
1 1 2 2

入力例 2

2
001

出力例 2

No

入力例 3

3
10110

出力例 3

Yes
1 3 2 1 2 3

以下の図は問題文中のものと同様です。


入力例 4

3
10101

出力例 4

No

入力例 5

6
00111011100

出力例 5

Yes
1 6 1 2 3 4 4 2 3 5 6 5

Score : 2000 points

Problem Statement

There is a bridge that connects the left and right banks of a river. There are 2 N doors placed at different positions on this bridge, painted in some colors. The colors of the doors are represented by integers from 1 through N. For each k (1 \leq k \leq N), there are exactly two doors painted in Color k.

Snuke decides to cross the bridge from the left bank to the right bank. He will keep on walking to the right, but the following event will happen while doing so:

  • At the moment Snuke touches a door painted in Color k (1 \leq k \leq N), he teleports to the right side of the other door painted in Color k.

It can be shown that he will eventually get to the right bank.

For each i (1 \leq i \leq 2 N - 1), the section between the i-th and (i + 1)-th doors from the left will be referred to as Section i. After crossing the bridge, Snuke recorded whether or not he walked through Section i, for each i (1 \leq i \leq 2 N - 1). This record is given to you as a string s of length 2 N - 1. For each i (1 \leq i \leq 2 N - 1), if Snuke walked through Section i, the i-th character in s is 1; otherwise, the i-th character is 0.

Figure: A possible arrangement of doors for Sample Input 3

Determine if there exists an arrangement of doors that is consistent with the record. If it exists, construct one such arrangement.

Constraints

  • 1 \leq N \leq 10^5
  • |s| = 2 N - 1
  • s consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

N
s

Output

If there is no arrangement of doors that is consistent with the record, print No. If there exists such an arrangement, print Yes in the first line, then print one such arrangement in the second line, in the following format:

c_1 c_2 ... c_{2 N}

Here, for each i (1 \leq i \leq 2 N), c_i is the color of the i-th door from the left.


Sample Input 1

2
010

Sample Output 1

Yes
1 1 2 2

Sample Input 2

2
001

Sample Output 2

No

Sample Input 3

3
10110

Sample Output 3

Yes
1 3 2 1 2 3

The figure below is identical to the one in the statement.


Sample Input 4

3
10101

Sample Output 4

No

Sample Input 5

6
00111011100

Sample Output 5

Yes
1 6 1 2 3 4 4 2 3 5 6 5
H - Generalized Insertion Sort

Time Limit: 3 sec / Memory Limit: 256 MB

配点 : 2000

問題文

N 頂点の根つき木が与えられます。 頂点には番号 0, 1, ..., N-1 がついており、根は頂点 0 です。 そして、頂点 i(i = 1, 2, ..., N-1) の親は頂点 p_i です。

はじめ、頂点 i には整数 a_i が書かれています。 なお、(a_0, a_1, ..., a_{N-1})(0, 1, ..., N-1) の順列になっています。

あなたは以下の操作を 25,000 回まで好きな回数実行できます。頂点 i に書かれた値が i となるようにしてください。

  • 頂点を 1 個選び v とする。頂点 0 と頂点 v の間のパスを考える。
  • パス上の頂点の値を回転させる。つまり、パス上の各辺 (i, p_i) について、頂点 p_i に書かれた値を頂点 i に(この操作の直前に)書かれていた値に書き換え、頂点 v の値は頂点 0 に(この操作の直前に)書かれていた値に書き換える。
  • なお、頂点 0 を選んでもよいが、この場合はなにもしないという操作になる。

制約

  • 2 \leq N \leq 2000
  • 0 \leq p_i \leq i-1
  • (a_0, a_1, ..., a_{N-1})(0, 1, ..., N-1) の順列である

入力

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

N
p_1 p_2 ... p_{N-1}
a_0 a_1 ... a_{N-1}

出力

1 行目に操作の回数 Q を出力せよ。 2 行目から Q+1 行目には、操作で選んだ頂点を順に出力せよ。


入力例 1

5
0 1 2 3
2 4 0 1 3

出力例 1

2
3
4
  • 1 回目の操作後、頂点 0, 1, .., 4 に書かれた値は 4, 0, 1, 2, 3 となる

入力例 2

5
0 1 2 2
4 3 1 2 0

出力例 2

3
4
3
1
  • 1 回目の操作後、頂点 0, 1, .., 4 に書かれた値は 3, 1, 0, 2, 4 となる
  • 2 回目の操作後、頂点 0, 1, .., 4 に書かれた値は 1, 0, 2, 3, 4 となる

Score : 2000 points

Problem Statement

You are given a rooted tree with N vertices. The vertices are numbered 0, 1, ..., N-1. The root is Vertex 0, and the parent of Vertex i (i = 1, 2, ..., N-1) is Vertex p_i.

Initially, an integer a_i is written in Vertex i. Here, (a_0, a_1, ..., a_{N-1}) is a permutation of (0, 1, ..., N-1).

You can execute the following operation at most 25 000 times. Do it so that the value written in Vertex i becomes i.

  • Choose a vertex and call it v. Consider the path connecting Vertex 0 and v.
  • Rotate the values written on the path. That is, For each edge (i, p_i) along the path, replace the value written in Vertex p_i with the value written in Vertex i (just before this operation), and replace the value of v with the value written in Vertex 0 (just before this operation).
  • You may choose Vertex 0, in which case the operation does nothing.

Constraints

  • 2 \leq N \leq 2000
  • 0 \leq p_i \leq i-1
  • (a_0, a_1, ..., a_{N-1}) is a permutation of (0, 1, ..., N-1).

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_{N-1}
a_0 a_1 ... a_{N-1}

Output

In the first line, print the number of operations, Q. In the second through (Q+1)-th lines, print the chosen vertices in order.


Sample Input 1

5
0 1 2 3
2 4 0 1 3

Sample Output 1

2
3
4
  • After the first operation, the values written in Vertex 0, 1, .., 4 are 4, 0, 1, 2, 3.

Sample Input 2

5
0 1 2 2
4 3 1 2 0

Sample Output 2

3
4
3
1
  • After the first operation, the values written in Vertex 0, 1, .., 4 are 3, 1, 0, 2, 4.
  • After the second operation, the values written in Vertex 0, 1, .., 4 are 1, 0, 2, 3, 4.
I - Simple APSP Problem

Time Limit: 3 sec / Memory Limit: 256 MB

配点 : 2000

問題文

H \times W のマス目が与えられます。 左上隅のマス目を (0, 0)、右下隅のマス目を (H-1, W-1) と表すことにします。

マス目のうち、N 個のマス目 (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) は黒く塗られており、残りは白く塗られています。

白いマス目 A, B の間の最短距離を、A から B まで白いマス目のみを使用して移動するときの最短移動回数とします。 ただし、1 回の移動では、上下左右の隣りあったマス目に移動できるものとします。

白いマス目は全部で H × W - N 個あるため、白いマス目から 2 つマス目を選ぶ方法は _{(H×W-N)}C_2 通りあります。

この _{(H×W-N)}C_2 通りそれぞれについて、選んだマスの間の最短距離を求め、 それを全て足して 1,000,000,007=10^9+7 で割った余りを求めてください。

制約

  • 1 \leq H, W \leq 10^6
  • 1 \leq N \leq 30
  • 0 \leq x_i \leq H-1
  • 0 \leq y_i \leq W-1
  • i \neq j ならば、x_i \neq x_j または y_i \neq y_j
  • 白いマス目が 1 つ以上存在する
  • 全ての白いマス目 A, B について、白いマス目のみを使用して A から B へ移動できる

入力

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

H W
N
x_1 y_1
x_2 y_2
:
x_N y_N

出力

最短距離の総和を 10^9+7 で割った余りを出力してください。


入力例 1

2 3
1
1 1

出力例 1

20

マス目の色は以下のようになっています(.: 白いマス目, #: 黒いマス目)。

...
.#.

ここで,以下のように白いマス目にアルファベットを振ります。

ABC
D#E

すると,

  • dist(A, B) = 1
  • dist(A, C) = 2
  • dist(A, D) = 1
  • dist(A, E) = 3
  • dist(B, C) = 1
  • dist(B, D) = 2
  • dist(B, E) = 2
  • dist(C, D) = 3
  • dist(C, E) = 1
  • dist(D, E) = 4

であり,これらの総和は 20 となります。

ただし,dist(A, B) はマス目A, Bの最短距離とします。


入力例 2

2 3
1
1 2

出力例 2

16

以下のように白いマス目にアルファベットを振ります。

ABC
DE#

すると,

  • dist(A, B) = 1
  • dist(A, C) = 2
  • dist(A, D) = 1
  • dist(A, E) = 2
  • dist(B, C) = 1
  • dist(B, D) = 2
  • dist(B, E) = 1
  • dist(C, D) = 3
  • dist(C, E) = 2
  • dist(D, E) = 1

であり,これらの総和は 16 となります。


入力例 3

3 3
1
1 1

出力例 3

64

入力例 4

4 4
4
0 1
1 1
2 1
2 2

出力例 4

268

入力例 5

1000000 1000000
1
0 0

出力例 5

333211937

Score : 2000 points

Problem Statement

You are given an H \times W grid. The square at the top-left corner will be represented by (0, 0), and the square at the bottom-right corner will be represented by (H-1, W-1).

Of those squares, N squares (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.

Let the shortest distance between white squares A and B be the minimum number of moves required to reach B from A visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.

Since there are H × W - N white squares in total, there are _{(H×W-N)}C_2 ways to choose two of the white squares.

For each of these _{(H×W-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 1 000 000 007=10^9+7.

Constraints

  • 1 \leq H, W \leq 10^6
  • 1 \leq N \leq 30
  • 0 \leq x_i \leq H-1
  • 0 \leq y_i \leq W-1
  • If i \neq j, then either x_i \neq x_j or y_i \neq y_j.
  • There is at least one white square.
  • For every pair of white squares A and B, it is possible to reach B from A visiting only white squares.

Input

Input is given from Standard Input in the following format:

H W
N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the sum of the shortest distances, modulo 10^9+7.


Sample Input 1

2 3
1
1 1

Sample Output 1

20

We have the following grid (.: a white square, #: a black square).

...
.#.

Let us assign a letter to each white square as follows:

ABC
D#E

Then, we have:

  • dist(A, B) = 1
  • dist(A, C) = 2
  • dist(A, D) = 1
  • dist(A, E) = 3
  • dist(B, C) = 1
  • dist(B, D) = 2
  • dist(B, E) = 2
  • dist(C, D) = 3
  • dist(C, E) = 1
  • dist(D, E) = 4

where dist(A, B) is the shortest distance between A and B. The sum of these distances is 20.


Sample Input 2

2 3
1
1 2

Sample Output 2

16

Let us assign a letter to each white square as follows:

ABC
DE#

Then, we have:

  • dist(A, B) = 1
  • dist(A, C) = 2
  • dist(A, D) = 1
  • dist(A, E) = 2
  • dist(B, C) = 1
  • dist(B, D) = 2
  • dist(B, E) = 1
  • dist(C, D) = 3
  • dist(C, E) = 2
  • dist(D, E) = 1

The sum of these distances is 16.


Sample Input 3

3 3
1
1 1

Sample Output 3

64

Sample Input 4

4 4
4
0 1
1 1
2 1
2 2

Sample Output 4

268

Sample Input 5

1000000 1000000
1
0 0

Sample Output 5

333211937
J - Rectangles

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 2100

問題文

サイズ 1×1×1 の小立方体に区切られたサイズ A×B×C の直方体があります。 小立方体には (0, 0, 0) から (A-1, B-1, C-1) までの座標が付けられています。

p, q, r を整数として、次のような abc 個の小立方体の集合を考えます。

\{(\ (p + i) mod A, (q + j) mod B, (r + k) mod C\ ) | i,j,k0 i < a, 0 j < b, 0 k < c をみたす整数 \}

ある整数 p, q, r を用いて上の形で書けるような小立方体の集合を、サイズ a×b×c のトーラス直方体と呼びます。

サイズ a×b×c のトーラス直方体の集合であって、以下の条件を満たすものの個数を mod 10^9+7 で求めてください。

  • 集合内のどの二つのトーラス直方体も共通部分を持たない
  • 集合内の全てのトーラス直方体の和集合はサイズ A×B×C の直方体全体である

制約

  • 1 a < A 100
  • 1 b < B 100
  • 1 c < C 100
  • 入力は全て整数

入力

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

a b c A B C

出力

条件を満たすサイズ a×b×c のトーラス直方体の集合の個数を 10^9+7 で割ったあまりを出力せよ。


入力例 1

1 1 1 2 2 2

出力例 1

1

入力例 2

2 2 2 4 4 4

出力例 2

744

入力例 3

2 3 4 6 7 8

出力例 3

0

入力例 4

2 3 4 98 99 100

出力例 4

471975164

Score : 2100 points

Problem Statement

We have a rectangular parallelepiped of dimensions A×B×C, divided into 1×1×1 small cubes. The small cubes have coordinates from (0, 0, 0) through (A-1, B-1, C-1).

Let p, q and r be integers. Consider the following set of abc small cubes:

\{(\ (p + i) mod A, (q + j) mod B, (r + k) mod C\ ) | i, j and k are integers satisfying 0 i < a, 0 j < b, 0 k < c \}

A set of small cubes that can be expressed in the above format using some integers p, q and r, is called a torus cuboid of size a×b×c.

Find the number of the sets of torus cuboids of size a×b×c that satisfy the following condition, modulo 10^9+7:

  • No two torus cuboids in the set have intersection.
  • The union of all torus cuboids in the set is the whole rectangular parallelepiped of dimensions A×B×C.

Constraints

  • 1 a < A 100
  • 1 b < B 100
  • 1 c < C 100
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

a b c A B C

Output

Print the number of the sets of torus cuboids of size a×b×c that satisfy the condition, modulo 10^9+7.


Sample Input 1

1 1 1 2 2 2

Sample Output 1

1

Sample Input 2

2 2 2 4 4 4

Sample Output 2

744

Sample Input 3

2 3 4 6 7 8

Sample Output 3

0

Sample Input 4

2 3 4 98 99 100

Sample Output 4

471975164