A - Biscuits

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

いくつかのビスケットの入った袋が N 個あります.i 番目の袋には A_i 個のビスケットが入っています.

高木君は,このうちいくつかの袋を選んで,選んだ袋に入っているビスケットをすべて食べるということを行います. このとき,袋を一つも選ばなかったり,すべての袋を選んだりしてもかまいません.

高木君は,食べるビスケットの枚数を 2 で割ると余りが P に等しくなるようにしたいです. このような袋の選び方は何通りあるか求めてください.

制約

  • 1 \leq N \leq 50
  • P = 0, 1
  • 1 \leq A_i \leq 100

入力

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

N P
A_1 A_2 ... A_N

出力

高木君が食べるビスケットの枚数を 2 で割ると P に等しくなるような,袋の選び方は何通りあるかを出力せよ.


入力例 1

2 0
1 3

出力例 1

2

食べるビスケットの枚数が 2 で割って 0 に等しくなるような選び方は 2 通りです:

  • どちらの袋も選ばない.食べるビスケットの枚数は 0 である.
  • どちらの袋も選ぶ.食べるビスケットの枚数は 4 である.

入力例 2

1 1
50

出力例 2

0

入力例 3

3 0
1 1 1

出力例 3

4

同じ枚数のビスケットが入っている場合でも,異なる袋は区別します.


入力例 4

45 1
17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26

出力例 4

17592186044416

Score : 200 points

Problem Statement

There are N bags of biscuits. The i-th bag contains A_i biscuits.

Takaki will select some of these bags and eat all of the biscuits inside. Here, it is also possible to select all or none of the bags.

He would like to select bags so that the total number of biscuits inside is congruent to P modulo 2. How many such ways to select bags there are?

Constraints

  • 1 \leq N \leq 50
  • P = 0 or 1
  • 1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N P
A_1 A_2 ... A_N

Output

Print the number of ways to select bags so that the total number of biscuits inside is congruent to P modulo 2.


Sample Input 1

2 0
1 3

Sample Output 1

2

There are two ways to select bags so that the total number of biscuits inside is congruent to 0 modulo 2:

  • Select neither bag. The total number of biscuits is 0.
  • Select both bags. The total number of biscuits is 4.

Sample Input 2

1 1
50

Sample Output 2

0

Sample Input 3

3 0
1 1 1

Sample Output 3

4

Two bags are distinguished even if they contain the same number of biscuits.


Sample Input 4

45 1
17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26

Sample Output 4

17592186044416
B - Moderate Differences

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 400

問題文

N 個のマスが一列に並んでいます. 一番左のマスには整数 A が,一番右のマスには整数 B が書かれており,他のマスには何も書かれていません.

青橋君は,何も書かれていないマスに整数を書き込み,次の条件を満たすようにしたいです:

  • どの隣接する 2 マスについても,書かれている整数の差は C 以上 D 以下である.

青橋君は,この条件を満たす限り,いくらでも大きい整数や小さい整数を書き込むことができます. 青橋君が条件を満たすように整数を書き込むことができるかを判定してください.

制約

  • 3 \leq N \leq 500000
  • 0 \leq A \leq 10^9
  • 0 \leq B \leq 10^9
  • 0 \leq C \leq D \leq 10^9
  • 入力はすべて整数

入力

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

N A B C D

出力

青橋君が条件を満たすように整数を書き込むことができるなら YES を,できないなら NO を出力せよ.


入力例 1

5 1 5 2 4

出力例 1

YES

例えば,左のマスから順に 1, -1, 3, 7, 5 となるように整数を書き込めばよいです.


入力例 2

4 7 6 4 5

出力例 2

NO

入力例 3

48792 105960835 681218449 90629745 90632170

出力例 3

NO

入力例 4

491995 412925347 825318103 59999126 59999339

出力例 4

YES

Score : 400 points

Problem Statement

There are N squares in a row. The leftmost square contains the integer A, and the rightmost contains the integer B. The other squares are empty.

Aohashi would like to fill the empty squares with integers so that the following condition is satisfied:

  • For any two adjacent squares, the (absolute) difference of the two integers in those squares is between C and D (inclusive).

As long as the condition is satisfied, it is allowed to use arbitrarily large or small integers to fill the squares. Determine whether it is possible to fill the squares under the condition.

Constraints

  • 3 \leq N \leq 500000
  • 0 \leq A \leq 10^9
  • 0 \leq B \leq 10^9
  • 0 \leq C \leq D \leq 10^9
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B C D

Output

Print YES if it is possible to fill the squares under the condition; print NO otherwise.


Sample Input 1

5 1 5 2 4

Sample Output 1

YES

For example, fill the squares with the following integers: 1, -1, 3, 7, 5, from left to right.


Sample Input 2

4 7 6 4 5

Sample Output 2

NO

Sample Input 3

48792 105960835 681218449 90629745 90632170

Sample Output 3

NO

Sample Input 4

491995 412925347 825318103 59999126 59999339

Sample Output 4

YES
C - Snuke and Spells

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1000

問題文

N 個のボールが一列に並んでいます. 左から i 番目のボールには,最初整数 A_i が書かれています.

すぬけ君が魔法を唱えると,これらのボールは次のようにして消滅します:

  • ボールがちょうど k 個あるとき,k が書かれているボールすべてが同時に消滅する.

すぬけ君の目標は,魔法を何回か唱えることでボールをすべて消滅させることです. これは不可能な場合があるので,すぬけ君はできるだけ少ない個数のボールに書かれている整数を変更することで,この目標を達成できるようにしたいです.

これらのボールは,書かれている整数が全部で M 回自然に変化することがわかっています. j 回目の変化においては,左から X_j 番目のボールに書かれている整数が Y_j に変わります.

各変化の後の状態で,次の変化が起こる前にすぬけ君が目標を達成しようとするとき,少なくとも何個のボールに書かれている整数を変更する必要があるかを求めてください.すぬけ君は整数の変更を十分速く行うことができるものとします.ただし,すぬけ君は実際には整数の変更を行わないことに注意してください.

制約

  • 1 \leq N \leq 200000
  • 1 \leq M \leq 200000
  • 1 \leq A_i \leq N
  • 1 \leq X_j \leq N
  • 1 \leq Y_j \leq N

部分点

  • 500 点分のテストケースでは,N \leq 200 および M \leq 200 が成り立つ.

入力

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

N M
A_1 A_2 ... A_N
X_1 Y_1
X_2 Y_2
:
X_M Y_M

出力

M 行出力せよ. j 行目には,j 回目の変化の後で,すぬけ君が目標を達成するためには,少なくとも何個のボールに書かれている整数を変更する必要があるかを出力せよ.


入力例 1

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

出力例 1

0
1
1
  • 1 回目の変化の後,ボールに書かれている整数は左のボールから順に 2, 1, 3, 4, 5 になります.この状態ですぬけ君が 5 回魔法を唱えると,すべてのボールが消滅します.そのため,0 個のボールに書かれている整数を変更すればよいです.
  • 2 回目の変化の後,ボールに書かれている整数は左のボールから順に 2, 5, 3, 4, 5 になります.この場合,少なくとも 1 個のボールに書かれている整数を変更する必要があります.例えば,左から 5 番目のボールに書かれている整数を 2 に変更した後,すぬけ君が 4 回魔法を唱えればよいです.
  • 3 回目の変化の後,ボールに書かれている整数は左のボールから順に 2, 5, 3, 4, 4 になります.この場合,少なくとも 1 個のボールに書かれている整数を変更する必要があります.例えば,左から 3 番目のボールに書かれている整数を 2 に変更した後,すぬけ君が 3 回魔法を唱えればよいです.

入力例 2

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

出力例 2

0
1
2
3

入力例 3

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

出力例 3

1
0
1
2
2
3
3
3
3
2

Score : 1000 points

Problem Statement

There are N balls in a row. Initially, the i-th ball from the left has the integer A_i written on it.

When Snuke cast a spell, the following happens:

  • Let the current number of balls be k. All the balls with k written on them disappear at the same time.

Snuke's objective is to vanish all the balls by casting the spell some number of times. This may not be possible as it is. If that is the case, he would like to modify the integers on the minimum number of balls to make his objective achievable.

By the way, the integers on these balls sometimes change by themselves. There will be M such changes. In the j-th change, the integer on the X_j-th ball from the left will change into Y_j.

After each change, find the minimum number of modifications of integers on the balls Snuke needs to make if he wishes to achieve his objective before the next change occurs. We will assume that he is quick enough in modifying integers. Here, note that he does not actually perform those necessary modifications and leaves them as they are.

Constraints

  • 1 \leq N \leq 200000
  • 1 \leq M \leq 200000
  • 1 \leq A_i \leq N
  • 1 \leq X_j \leq N
  • 1 \leq Y_j \leq N

Subscore

  • In the test set worth 500 points, N \leq 200 and M \leq 200.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
X_1 Y_1
X_2 Y_2
:
X_M Y_M

Output

Print M lines. The j-th line should contain the minimum necessary number of modifications of integers on the balls to make Snuke's objective achievable.


Sample Input 1

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

Sample Output 1

0
1
1
  • After the first change, the integers on the balls become 2, 1, 3, 4, 5, from left to right. Here, all the balls can be vanished by casting the spell five times. Thus, no modification is necessary.
  • After the second change, the integers on the balls become 2, 5, 3, 4, 5, from left to right. In this case, at least one modification must be made. One optimal solution is to modify the integer on the fifth ball from the left to 2, and cast the spell four times.
  • After the third change, the integers on the balls become 2, 5, 3, 4, 4, from left to right. Also in this case, at least one modification must be made. One optimal solution is to modify the integer on the third ball from the left to 2, and cast the spell three times.

Sample Input 2

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

Sample Output 2

0
1
2
3

Sample Input 3

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

Sample Output 3

1
0
1
2
2
3
3
3
3
2
D - Game on Tree

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1100

問題文

N 個の頂点 1, 2, ..., N からなる木があります. この木の辺は (x_i, y_i) で表されます.

Alice と Bob は,この木を使ってゲームをします. Alice から始め,Alice と Bob が交互に次の操作を行います.

  • まだ残っている辺を選び,その辺を木から取り除く.また,この操作により木は 2 つに分断されるが,そのうち頂点 1 を含まないほうの木も取り除く.

先に操作を行えなくなったほうが負けです. 2 人が最適に行動するとき,どちらが勝つかを求めてください.

制約

  • 2 \leq N \leq 100000
  • 1 \leq x_i, y_i \leq N
  • 与えられるグラフは木である.

入力

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

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

出力

Alice が勝つなら Alice を,Bob が勝つなら Bob を出力せよ.


入力例 1

5
1 2
2 3
2 4
4 5

出力例 1

Alice

Alice が最初に頂点 1, 2 を結ぶ辺を選んで操作を行うと,頂点 1 のみを含む木になります. このとき,辺は残っていないので,Bob は操作を行うことができず,Alice が勝ちます.


入力例 2

5
1 2
2 3
1 4
4 5

出力例 2

Bob

入力例 3

6
1 2
2 4
5 1
6 3
3 2

出力例 3

Alice

入力例 4

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

出力例 4

Bob

Score : 1100 points

Problem Statement

There is a tree with N vertices numbered 1, 2, ..., N. The edges of the tree are denoted by (x_i, y_i).

On this tree, Alice and Bob play a game against each other. Starting from Alice, they alternately perform the following operation:

  • Select an existing edge and remove it from the tree, disconnecting it into two separate connected components. Then, remove the component that does not contain Vertex 1.

A player loses the game when he/she is unable to perform the operation. Determine the winner of the game assuming that both players play optimally.

Constraints

  • 2 \leq N \leq 100000
  • 1 \leq x_i, y_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

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

Output

Print Alice if Alice wins; print Bob if Bob wins.


Sample Input 1

5
1 2
2 3
2 4
4 5

Sample Output 1

Alice

If Alice first removes the edge connecting Vertices 1 and 2, the tree becomes a single vertex tree containing only Vertex 1. Since there is no edge anymore, Bob cannot perform the operation and Alice wins.


Sample Input 2

5
1 2
2 3
1 4
4 5

Sample Output 2

Bob

Sample Input 3

6
1 2
2 4
5 1
6 3
3 2

Sample Output 3

Alice

Sample Input 4

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

Sample Output 4

Bob
E - Jigsaw

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 1200

問題文

N 個の特殊なジグソーピースがあります.それぞれのピースは,幅が 1 で高さが 1 以上の長方形のパーツを 3 つつなげた形をしています. i 番目のピースは,次のような形をしています:

  • 高さ H のパーツの左側に高さ A_i のパーツを,右側に高さ B_i のパーツをくっつけた形.ただし,左側のパーツの一番下の辺,右側のパーツの一番下の辺は,それぞれ中央のパーツの一番下の辺から C_i, D_i だけ上にある.

すぬけ君は,これらのピースを,一辺が 10^{100} の正方形の形をしたテーブルの上に置こうとしています.この時,次の条件を満たさなければなりません:

  • すべてのピースをテーブルの上に置く.
  • すべてのピースの中央のパーツの一番下の辺全体は,テーブルの手前の辺に接している.
  • 左右のパーツの一番下の辺全体は,テーブルの手前の辺に接しているか,他のピースを構成するあるパーツの上の辺と接している.
  • ピースを回転させたり,反転させたりして用いてはならない.

このような並べ方ができるかどうかを判定してください.

制約

  • 1 \leq N \leq 100000
  • 1 \leq H \leq 200
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq H
  • 0 \leq C_i \leq H - A_i
  • 0 \leq D_i \leq H - B_i
  • 入力はすべて整数

入力

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

N H
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_N B_N C_N D_N

出力

条件を満たすようにピースを並べることが可能なら YES を,不可能なら NO を出力せよ.


入力例 1

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

出力例 1

YES

例えば,下図のように並べればよいです.


入力例 2

4 2
1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1

出力例 2

NO

入力例 3

10 4
1 1 0 3
2 3 2 0
1 2 3 0
2 1 0 0
3 2 0 2
1 1 3 0
3 2 0 0
1 3 2 0
1 1 1 3
2 3 0 0

出力例 3

YES

Score : 1200 points

Problem Statement

We have N irregular jigsaw pieces. Each piece is composed of three rectangular parts of width 1 and various heights joined together. More specifically:

  • The i-th piece is a part of height H, with another part of height A_i joined to the left, and yet another part of height B_i joined to the right, as shown below. Here, the bottom sides of the left and right parts are respectively at C_i and D_i units length above the bottom side of the center part.

Snuke is arranging these pieces on a square table of side 10^{100}. Here, the following conditions must be held:

  • All pieces must be put on the table.
  • The entire bottom side of the center part of each piece must touch the front side of the table.
  • The entire bottom side of the non-center parts of each piece must either touch the front side of the table, or touch the top side of a part of some other piece.
  • The pieces must not be rotated or flipped.

Determine whether such an arrangement is possible.

Constraints

  • 1 \leq N \leq 100000
  • 1 \leq H \leq 200
  • 1 \leq A_i \leq H
  • 1 \leq B_i \leq H
  • 0 \leq C_i \leq H - A_i
  • 0 \leq D_i \leq H - B_i
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_N B_N C_N D_N

Output

If it is possible to arrange the pieces under the conditions, print YES; if it is impossible, print NO.


Sample Input 1

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

Sample Output 1

YES

The figure below shows a possible arrangement.


Sample Input 2

4 2
1 1 0 1
1 1 0 1
1 1 0 1
1 1 0 1

Sample Output 2

NO

Sample Input 3

10 4
1 1 0 3
2 3 2 0
1 2 3 0
2 1 0 0
3 2 0 2
1 1 3 0
3 2 0 0
1 3 2 0
1 1 1 3
2 3 0 0

Sample Output 3

YES
F - Zigzag

Time Limit: 4 sec / Memory Limit: 256 MB

配点 : 1600

問題文

下図のように,1 辺が N 個の点からなる正三角形状に,N(N+1)/2 個の点が並んでいます. 上から i 段目の左から j 番目の点を (i, j) で表すことにします(1 \leq i \leq N, 1 \leq j \leq i). また,(i+1, j)(i, j) のすぐ左下の点,(i+1, j+1)(i, j) のすぐ右下の点と呼ぶことにします.

高橋君は,この点を結んで,M 個の折れ線 1, 2, ..., M を描くことにしました. 各折れ線は,(1, 1) から始めて,「現在いる点のすぐ左下の点かすぐ右下の点を選び,現在いる点と選んだ点を直線で結んだのち,選んだ点へ移動する」 ことを N-1 回繰り返すことで得られます. すなわち,ある X_{i,1}, ..., X_{i,N} が存在して,次が成り立ちます:

  • 折れ線 i は,N 個の点 (1, X_{i,1}), (2, X_{i,2}), ..., (N, X_{i,N}) をこの順で結んでいる.
  • j=1, 2, ..., N-1 に対して,X_{i,j+1} = X_{i,j} または X_{i,j+1} = X_{i,j}+1 が成り立つ.

高橋君は,折れ線 i+1 が折れ線 i の左に来ることはないように折れ線を描きたいです. つまり,j=1, 2, ..., N に対して,X_{1,j} \leq X_{2,j} \leq ... \leq X_{M,j} が成り立ちます.

また,高橋君は,折れ線の曲がり方について K 個の条件を満たすように折れ線を描かなければなりません. i 番目の条件は (A_i, B_i, C_i) で表され,これは次を意味します:

  • C_i=0 のときは,折れ線 A_i を描くとき,B_i 回目の移動においては,その時いる点のすぐ左下の点を選ぶ.
  • C_i=1 のときは,折れ線 A_i を描くとき,B_i 回目の移動においては,その時いる点のすぐ右下の点を選ぶ.

すなわち,X_{A_i, {B_i}+1} = X_{A_i, B_i} + C_i を意味します.

高橋君が M 個の折れ線を描く方法が何通りあるかを mod 1000000007 で求めてください.

注意

提出を行う前に,「コードテスト」を利用して実行時間を計測することを強く推奨する.

制約

  • 1 \leq N \leq 20
  • 1 \leq M \leq 20
  • 0 \leq K \leq (N-1)M
  • 1 \leq A_i \leq M
  • 1 \leq B_i \leq N-1
  • C_i = 0,1
  • (A_i, B_i) として同じ組は複数回与えられない.

入力

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

N M K
A_1 B_1 C_1
A_2 B_2 C_2
:
A_K B_K C_K

出力

高橋君が M 個の折れ線を描く方法は何通りあるかを mod 1000000007 で出力せよ.


入力例 1

3 2 1
1 2 0

出力例 1

6

下図に示すように,6 通りの描き方があります.ただし,赤線は折れ線 1,緑線は折れ線 2 を表します:


入力例 2

3 2 2
1 1 1
2 1 0

出力例 2

0

入力例 3

5 4 2
1 3 1
4 2 0

出力例 3

172

入力例 4

20 20 0

出力例 4

881396682

Score : 1600 points

Problem Statement

There are N(N+1)/2 dots arranged to form an equilateral triangle whose sides consist of N dots, as shown below. The j-th dot from the left in the i-th row from the top is denoted by (i, j) (1 \leq i \leq N, 1 \leq j \leq i). Also, we will call (i+1, j) immediately lower-left to (i, j), and (i+1, j+1) immediately lower-right to (i, j).

Takahashi is drawing M polygonal lines L_1, L_2, ..., L_M by connecting these dots. Each L_i starts at (1, 1), and visits the dot that is immediately lower-left or lower-right to the current dots N-1 times. More formally, there exist X_{i,1}, ..., X_{i,N} such that:

  • L_i connects the N points (1, X_{i,1}), (2, X_{i,2}), ..., (N, X_{i,N}), in this order.
  • For each j=1, 2, ..., N-1, either X_{i,j+1} = X_{i,j} or X_{i,j+1} = X_{i,j}+1 holds.

Takahashi would like to draw these lines so that no part of L_{i+1} is to the left of L_{i}. That is, for each j=1, 2, ..., N, X_{1,j} \leq X_{2,j} \leq ... \leq X_{M,j} must hold.

Additionally, there are K conditions on the shape of the lines that must be followed. The i-th condition is denoted by (A_i, B_i, C_i), which means:

  • If C_i=0, L_{A_i} must visit the immediately lower-left dot for the B_i-th move.
  • If C_i=1, L_{A_i} must visit the immediately lower-right dot for the B_i-th move.

That is, X_{A_i, {B_i}+1} = X_{A_i, B_i} + C_i must hold.

In how many ways can Takahashi draw M polygonal lines? Find the count modulo 1000000007.

Notes

Before submission, it is strongly recommended to measure the execution time of your code using "Custom Test".

Constraints

  • 1 \leq N \leq 20
  • 1 \leq M \leq 20
  • 0 \leq K \leq (N-1)M
  • 1 \leq A_i \leq M
  • 1 \leq B_i \leq N-1
  • C_i = 0 or 1
  • No pair appears more than once as (A_i, B_i).

Input

Input is given from Standard Input in the following format:

N M K
A_1 B_1 C_1
A_2 B_2 C_2
:
A_K B_K C_K

Output

Print the number of ways for Takahashi to draw M polygonal lines, modulo 1000000007.


Sample Input 1

3 2 1
1 2 0

Sample Output 1

6

There are six ways to draw lines, as shown below. Here, red lines represent L_1, and green lines represent L_2.


Sample Input 2

3 2 2
1 1 1
2 1 0

Sample Output 2

0

Sample Input 3

5 4 2
1 3 1
4 2 0

Sample Output 3

172

Sample Input 4

20 20 0

Sample Output 4

881396682