A - Poisonous Cookies

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君は、解毒剤入りの美味しくないクッキーを A 枚、解毒剤入りの美味しいクッキーを B 枚、毒入りの美味しいクッキーを C 枚持っています。

高橋君は、毒入りのクッキーを食べるとお腹を壊し、お腹を壊した状態で毒入りのクッキーを食べると死んでしまいます。 高橋君は死にたくないので、お腹を壊した状態で毒入りのクッキーを食べることはできません。 お腹を壊した状態で解毒剤入りのクッキーを食べると、お腹の調子が治ります。 解毒剤入りのクッキーを食べる以外に、お腹の調子を治す方法はありません。

高橋君が食べることのできる美味しいクッキーの枚数の最大値を求めてください。

制約

  • 0 \leq A,B,C \leq 10^9
  • A,B,C は整数である

入力

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

A B C

出力

高橋君が食べることのできる美味しいクッキーの枚数の最大値を出力せよ。


入力例 1

3 1 4

出力例 1

5

以下のような順番でクッキーを食べることで、美味しいクッキーをすべて食べることができます。

  • 毒入りの美味しいクッキー
  • 解毒剤入りの美味しくないクッキー
  • 毒入りの美味しいクッキー
  • 解毒剤入りの美味しいクッキー
  • 毒入りの美味しいクッキー
  • 解毒剤入りの美味しくないクッキー
  • 毒入りの美味しいクッキー

入力例 2

5 2 9

出力例 2

10

入力例 3

8 8 1

出力例 3

9

Score : 200 points

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death. As he wants to live, he cannot eat one in such a situation. Eating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.

Find the maximum number of tasty cookies that Takahashi can eat.

Constraints

  • 0 \leq A,B,C \leq 10^9
  • A,B and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum number of tasty cookies that Takahashi can eat.


Sample Input 1

3 1 4

Sample Output 1

5

We can eat all tasty cookies, in the following order:

  • A tasty cookie containing poison
  • An untasty cookie containing antidotes
  • A tasty cookie containing poison
  • A tasty cookie containing antidotes
  • A tasty cookie containing poison
  • An untasty cookie containing antidotes
  • A tasty cookie containing poison

Sample Input 2

5 2 9

Sample Output 2

10

Sample Input 3

8 8 1

Sample Output 3

9
B - Tree Burning

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

高橋湖の周長は L です。高橋湖の周上には湖の所有者である高橋君の家があります。 高橋湖の周上の地点には高橋君の家から反時計回りに測った距離を用いて、0 以上 L 未満の実数の座標が定まっています。

高橋湖の周上には木が N 本生えています。i 本目の木は座標 X_i に生えています。高橋君の家のある座標 0 には木は生えていません。

高橋君は、自分の家からはじめて、以下の行動を繰り返します。

  • すべての木を燃やし終えている場合、終了する。
  • 時計回りまたは反時計回りの向きを指定する。
  • 初めてまだ燃やしていない木のある座標に到達するまで、指定した方向に高橋湖の周上を歩き続ける。
  • 木のある座標に到達したら、その木を燃やしてその場に立ち止まり、最初に戻る。

この行動を通じて、高橋君が歩く距離の合計の最大値を求めてください。

制約

  • 2 \leq L \leq 10^9
  • 1 \leq N \leq 2\times 10^5
  • 1 \leq X_1 < ... < X_N \leq L-1
  • 入力はすべて整数である

部分点

この問題には部分点が設定されている。

  • N \leq 2000 を満たす入力に正解すると、300 点が得られる。

入力

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

L N
X_1
:
X_N

出力

高橋君が歩く距離の合計の最大値を出力せよ。


入力例 1

10 3
2
7
9

出力例 1

15

以下のような行動で、高橋君は距離 15 を歩きます。

  • 反時計回りに距離 2 ぶんだけ歩き、座標 2 にある木を燃やして立ち止まる。
  • 反時計回りに距離 5 ぶんだけ歩き、座標 7 にある木を燃やして立ち止まる。
  • 時計回りに距離 8 ぶんだけ歩き、座標 9 にある木を燃やして立ち止まる。

入力例 2

10 6
1
2
3
6
7
9

出力例 2

27

入力例 3

314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265

出力例 3

1204124749

Score : 800 points

Problem Statement

Takahashi Lake has a perimeter of L. On the circumference of the lake, there is a residence of the lake's owner, Takahashi. Each point on the circumference of the lake has a coordinate between 0 and L (including 0 but not L), which is the distance from the Takahashi's residence, measured counter-clockwise.

There are N trees around the lake; the coordinate of the i-th tree is X_i. There is no tree at coordinate 0, the location of Takahashi's residence.

Starting at his residence, Takahashi will repeat the following action:

  • If all trees are burnt, terminate the process.
  • Specify a direction: clockwise or counter-clockwise.
  • Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.
  • When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.

Find the longest possible total distance Takahashi walks during the process.

Partial Score

A partial score can be obtained in this problem:

  • 300 points will be awarded for passing the input satisfying N \leq 2000.

Constraints

  • 2 \leq L \leq 10^9
  • 1 \leq N \leq 2\times 10^5
  • 1 \leq X_1 < ... < X_N \leq L-1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

L N
X_1
:
X_N

Output

Print the longest possible total distance Takahashi walks during the process.


Sample Input 1

10 3
2
7
9

Sample Output 1

15

Takahashi walks the distance of 15 if the process goes as follows:

  • Walk a distance of 2 counter-clockwise, burn the tree at the coordinate 2 and stay there.
  • Walk a distance of 5 counter-clockwise, burn the tree at the coordinate 7 and stay there.
  • Walk a distance of 8 clockwise, burn the tree at the coordinate 9 and stay there.

Sample Input 2

10 6
1
2
3
6
7
9

Sample Output 2

27

Sample Input 3

314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265

Sample Output 3

1204124749
C - Coloring Torus

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

n \times n のマス目に対して,上から r+1 行目,左から c+1 列目にあるマスを (r, c) で表します. このマス目の K 色でのよい塗り方とは,次のような塗り方を言います:

  • それぞれのマスは K 色のいずれかで塗られている.
  • K 色のうちすべての色が,いずれかのマスに塗られている.
  • K 色にそれぞれ 1, 2, ..., K の番号をつける.任意の色 i, j (1 \leq i \leq K, 1 \leq j \leq K) に対して,色 i のマスに接している色 j のマスの個数は,色 i のマスの選び方によらず等しい.ここで,マス (r, c) に接しているマスは,((r-1)\; mod\; n, c), ((r+1)\; mod\; n, c), (r, (c-1)\; mod\; n), (r, (c+1)\; mod\; n) とする (これら 4 つの中に同じマスが複数回現れる場合は,そのマスの色は重複している回数だけ数えるものとする).

K が与えられたとき,1 以上 500 以下の n を自由に選んで,n \times n のマス目の K 色でのよい塗り方を構成してください. この問題の制約の下,これは常に可能であることが証明できます.

制約

  • 1 \leq K \leq 1000

入力

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

K

出力

次の形式で出力せよ.

n
c_{0,0} c_{0,1} ... c_{0,n-1}
c_{1,0} c_{1,1} ... c_{1,n-1}
:
c_{n-1,0} c_{n-1,1} ... c_{n-1,n-1}

n はマス目の大きさを表す.1 \leq n \leq 500 でなければならない. c_{r,c} はマス (r, c) をどの色で塗るべきかを表す 1 \leq c_{r,c} \leq K なる整数である.


入力例 1

2

出力例 1

3
1 1 1
1 1 1
2 2 2
  • どの色 1 のマスも,3 個の色 1 のマス,1 個の色 2 のマスと接しています.
  • どの色 2 のマスも,2 個の色 1 のマス,2 個の色 2 のマスと接しています.

次のような出力は不正解となります:

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

入力例 2

9

出力例 2

3
1 2 3
4 5 6
7 8 9

Score : 1000 points

Problem Statement

For an n \times n grid, let (r, c) denote the square at the (r+1)-th row from the top and the (c+1)-th column from the left. A good coloring of this grid using K colors is a coloring that satisfies the following:

  • Each square is painted in one of the K colors.
  • Each of the K colors is used for some squares.
  • Let us number the K colors 1, 2, ..., K. For any colors i and j (1 \leq i \leq K, 1 \leq j \leq K), every square in Color i has the same number of adjacent squares in Color j. Here, the squares adjacent to square (r, c) are ((r-1)\; mod\; n, c), ((r+1)\; mod\; n, c), (r, (c-1)\; mod\; n) and (r, (c+1)\; mod\; n) (if the same square appears multiple times among these four, the square is counted that number of times).

Given K, choose n between 1 and 500 (inclusive) freely and construct a good coloring of an n \times n grid using K colors. It can be proved that this is always possible under the constraints of this problem,

Constraints

  • 1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K

Output

Output should be in the following format:

n
c_{0,0} c_{0,1} ... c_{0,n-1}
c_{1,0} c_{1,1} ... c_{1,n-1}
:
c_{n-1,0} c_{n-1,1} ... c_{n-1,n-1}

n should represent the size of the grid, and 1 \leq n \leq 500 must hold. c_{r,c} should be an integer such that 1 \leq c_{r,c} \leq K and represent the color for the square (r, c).


Sample Input 1

2

Sample Output 1

3
1 1 1
1 1 1
2 2 2
  • Every square in Color 1 has three adjacent squares in Color 1 and one adjacent square in Color 2.
  • Every square in Color 2 has two adjacent squares in Color 1 and two adjacent squares in Color 2.

Output such as the following will be judged incorrect:

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

Sample Input 2

9

Sample Output 2

3
1 2 3
4 5 6
7 8 9
D - Inversion Sum

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

長さ N の整数列 A_1,A_2,...,A_N が与えられます。Q 回の操作を順に行います。 i 回目の操作は 2 つの整数 X_i,Y_i を用いて表され、以下の 2 つの操作からちょうど片方を選んで行います。

  • A_{X_i}A_{Y_i} の値を入れ替える
  • 何もしない

操作の行い方は 2^Q 通りあります。これらすべての操作の行い方に対する最終的な数列の反転数をすべて足し合わせたものを 10^9+7 で割ったあまりを求めてください。

ただし、数列 P_1,P_2,...,P_M の反転数とは、1\leq i < j\leq M かつ P_i > P_j を満たすような整数の組 (i,j) の個数です。

制約

  • 1 \leq N \leq 3000
  • 0 \leq Q \leq 3000
  • 0 \leq A_i \leq 10^9(1\leq i\leq N)
  • 1 \leq X_i,Y_i \leq N(1\leq i\leq Q)
  • X_i\neq Y_i(1\leq i\leq Q)
  • 入力はすべて整数である

入力

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

N Q
A_1
:
A_N
X_1 Y_1
:
X_Q Y_Q

出力

最終的な数列の反転数の総和を 10^9+7 で割ったあまりを出力せよ。


入力例 1

3 2
1
2
3
1 2
1 3

出力例 1

6

操作の行い方としてありうるものは次の 4 通りです。

  • 1 回目も 2 回目は何もしない。最終的な数列は 1,2,3 であり、反転数は 0 である。
  • 1 回目は何もせず、2 回目は入れ替えを行う。最終的な数列は 3,2,1 であり、反転数は 3 である。
  • 1 回目は入れ替えを行い、2 回目は何もしない。最終的な数列は 2,1,3 であり、反転数は 1 である。
  • 1 回目も 2 回目も入れ替えを行う。最終的な数列は 3,1,2 であり、反転数は 2 である。

これらの反転数の総和である、0+3+1+2=6 を出力します。


入力例 2

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

出力例 2

36

入力例 3

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

出力例 3

425

Score : 1000 points

Problem Statement

You are given an integer sequence of length N: A_1,A_2,...,A_N. Let us perform Q operations in order. The i-th operation is described by two integers X_i and Y_i. In this operation, we will choose one of the following two actions and perform it:

  • Swap the values of A_{X_i} and A_{Y_i}
  • Do nothing

There are 2^Q ways to perform these operations. Find the sum of the inversion numbers of the final sequences for all of these ways to perform operations, modulo 10^9+7.

Here, the inversion number of a sequence P_1,P_2,...,P_M is the number of pairs of integers (i,j) such that 1\leq i < j\leq M and P_i > P_j.

Constraints

  • 1 \leq N \leq 3000
  • 0 \leq Q \leq 3000
  • 0 \leq A_i \leq 10^9(1\leq i\leq N)
  • 1 \leq X_i,Y_i \leq N(1\leq i\leq Q)
  • X_i\neq Y_i(1\leq i\leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1
:
A_N
X_1 Y_1
:
X_Q Y_Q

Output

Print the sum of the inversion numbers of the final sequences, modulo 10^9+7.


Sample Input 1

3 2
1
2
3
1 2
1 3

Sample Output 1

6

There are four ways to perform operations, as follows:

  • Do nothing, both in the first and second operations. The final sequence would be 1,2,3, with the inversion number of 0.
  • Do nothing in the first operation, then perform the swap in the second. The final sequence would be 3,2,1, with the inversion number of 3.
  • Perform the swap in the first operation, then do nothing in the second. The final sequence would be 2,1,3, with the inversion number of 1.
  • Perform the swap, both in the first and second operations. The final sequence would be 3,1,2, with the inversion number of 2.

The sum of these inversion numbers, 0+3+1+2=6, should be printed.


Sample Input 2

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

Sample Output 2

36

Sample Input 3

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

Sample Output 3

425
E - Less than 3

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1400

問題文

長さ N の文字列 s および t が与えられます。 s および t01 からなります。 また、s および t において、同一の文字が 3 個以上連続する箇所はありません。

あなたは次の操作を繰り返し行い、s を書き換えていくことができます。

  • 添字 i (1 \leq i \leq N) を任意に選び、si 文字目を反転する (すなわち、01 へ、10 へ書き換える)。 ただし、操作後の s において、同一の文字が 3 個以上連続する箇所があってはならない。

あなたの目標は st に一致させることです。 st に一致させるために必要な操作回数の最小値を求めてください。

制約

  • 1 \leq N \leq 5000
  • s および t の長さは N である。
  • s および t01 からなる。
  • s および t において、同一の文字が 3 個以上連続する箇所はない。

入力

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

N
s
t

出力

st に一致させるために必要な操作回数の最小値を出力せよ。なお、有限回の操作で目的は必ず達成可能であることが証明できる。


入力例 1

4
0011
0101

出力例 1

4

例えば、00111011100111010101 と操作を行えばよいです。


入力例 2

1
0
0

出力例 2

0

入力例 3

8
00110011
10101010

出力例 3

10

Score : 1400 points

Problem Statement

You are given strings s and t, both of length N. s and t consist of 0 and 1. Additionally, in these strings, the same character never occurs three or more times in a row.

You can modify s by repeatedly performing the following operation:

  • Choose an index i (1 \leq i \leq N) freely and invert the i-th character in s (that is, replace 0 with 1, and 1 with 0), under the condition that the same character would not occur three or more times in a row in s after the operation.

Your objective is to make s equal to t. Find the minimum number of operations required.

Constraints

  • 1 \leq N \leq 5000
  • The lengths of s and t are both N.
  • s and t consists of 0 and 1.
  • In s and t, the same character never occurs three or more times in a row.

Input

Input is given from Standard Input in the following format:

N
s
t

Output

Find the minimum number of operations required to make s equal to t. It can be proved that the objective is always achievable in a finite number of operations.


Sample Input 1

4
0011
0101

Sample Output 1

4

One possible solution is 00111011100111010101.


Sample Input 2

1
0
0

Sample Output 2

0

Sample Input 3

8
00110011
10101010

Sample Output 3

10
F - Permutation and Minimum

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1600

問題文

長さ 2N の数列 A_1, A_2, ..., A_{2N} があります. 各 A_i-1 か,あるいは 1 以上 2N 以下の整数です. -1 以外のどの整数も,{A_i} の中には 1 回までしか現れません.

すぬけ君は,A_i = -1 であるような i それぞれに対して,A_i1 以上 2N の整数で置き換えて,{A_i}1, 2, ..., 2N の並び替えになるようにします. そして,長さ N の数列 B_1, B_2, ..., B_N を,B_i = min(A_{2i-1}, A_{2i}) として求めます.

数列 B_1, B_2, ..., B_N として考えられる異なるものの個数を 10^9 + 7 で割ったあまりを求めてください.

制約

  • 1 \leq N \leq 300
  • A_i = -1 あるいは 1 \leq A_i \leq 2N
  • A_i \neq -1, A_j \neq -1 ならば A_i \neq A_j (i \neq j)

入力

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

N
A_1 A_2 ... A_{2N}

出力

数列 B_1, B_2, ..., B_N として考えられる異なるものの個数を 10^9 + 7 で割ったあまりを出力せよ.


入力例 1

3
1 -1 -1 3 6 -1

出力例 1

5

{A_i}1, 2, ..., 2N の並び替えにする方法は 6 通りありますが,そのそれぞれに対して {B_i} は次のようになります:

  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 2, 4, 3, 6, 5): (B_1, B_2, B_3) = (1, 3, 5)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 2, 5, 3, 6, 4): (B_1, B_2, B_3) = (1, 3, 4)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 3, 6, 5): (B_1, B_2, B_3) = (1, 2, 5)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 5, 3, 6, 2): (B_1, B_2, B_3) = (1, 3, 2)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 5, 2, 3, 6, 4): (B_1, B_2, B_3) = (1, 2, 4)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 5, 4, 3, 6, 2): (B_1, B_2, B_3) = (1, 3, 2)

よって,異なる B_1, B_2, B_35 個あります.


入力例 2

4
7 1 8 3 5 2 6 4

出力例 2

1

入力例 3

10
7 -1 -1 -1 -1 -1 -1 6 14 12 13 -1 15 -1 -1 -1 -1 20 -1 -1

出力例 3

9540576

入力例 4

20
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 -1 -1 -1 -1 -1 7 -1 -1 -1 -1 -1 -1 -1 -1 -1 34 -1 -1 -1 -1 31 -1 -1 -1 -1 -1 -1 -1 -1

出力例 4

374984201

Score : 1600 points

Problem Statement

There is a sequence of length 2N: A_1, A_2, ..., A_{2N}. Each A_i is either -1 or an integer between 1 and 2N (inclusive). Any integer other than -1 appears at most once in {A_i}.

For each i such that A_i = -1, Snuke replaces A_i with an integer between 1 and 2N (inclusive), so that {A_i} will be a permutation of 1, 2, ..., 2N. Then, he finds a sequence of length N, B_1, B_2, ..., B_N, as B_i = min(A_{2i-1}, A_{2i}).

Find the number of different sequences that B_1, B_2, ..., B_N can be, modulo 10^9 + 7.

Constraints

  • 1 \leq N \leq 300
  • A_i = -1 or 1 \leq A_i \leq 2N.
  • If A_i \neq -1, A_j \neq -1, then A_i \neq A_j. (i \neq j)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{2N}

Output

Print the number of different sequences that B_1, B_2, ..., B_N can be, modulo 10^9 + 7.


Sample Input 1

3
1 -1 -1 3 6 -1

Sample Output 1

5

There are six ways to make {A_i} a permutation of 1, 2, ..., 2N; for each of them, {B_i} would be as follows:

  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 2, 4, 3, 6, 5): (B_1, B_2, B_3) = (1, 3, 5)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 2, 5, 3, 6, 4): (B_1, B_2, B_3) = (1, 3, 4)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 2, 3, 6, 5): (B_1, B_2, B_3) = (1, 2, 5)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 4, 5, 3, 6, 2): (B_1, B_2, B_3) = (1, 3, 2)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 5, 2, 3, 6, 4): (B_1, B_2, B_3) = (1, 2, 4)
  • (A_1, A_2, A_3, A_4, A_5, A_6) = (1, 5, 4, 3, 6, 2): (B_1, B_2, B_3) = (1, 3, 2)

Thus, there are five different sequences that B_1, B_2, B_3 can be.


Sample Input 2

4
7 1 8 3 5 2 6 4

Sample Output 2

1

Sample Input 3

10
7 -1 -1 -1 -1 -1 -1 6 14 12 13 -1 15 -1 -1 -1 -1 20 -1 -1

Sample Output 3

9540576

Sample Input 4

20
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 -1 -1 -1 -1 -1 7 -1 -1 -1 -1 -1 -1 -1 -1 -1 34 -1 -1 -1 -1 31 -1 -1 -1 -1 -1 -1 -1 -1

Sample Output 4

374984201