A - 1D Matching

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 500

問題文

一次元の世界に N 個のパソコンと N 個の電源があります。 i 番目のパソコンの座標は a_i であり、 i 番目の電源の座標は b_i です。 これらの 2N 個の座標は相異なることが保証されています。

すぬけ君は、それぞれのパソコンをケーブルで電源につなぎたいです。 それぞれの電源は一つのパソコンにのみつなぐことができます。

何通りの方法で、ケーブルの長さの合計を最小化できるでしょうか? 答えを modulo 10^9+7 で求めてください。

制約

  • 1 ≤ N ≤ 10^5
  • 0 ≤ a_i, b_i ≤ 10^9
  • 座標は整数である。
  • 座標は相異なる。

入力

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

N
a_1
:
a_N
b_1
:
b_N

出力

ケーブルの長さの合計を最小化する方法は何通りあるか、 modulo 10^9+7 で出力せよ。


入力例 1

2
0
10
20
30

出力例 1

2

0-20, 10-300-30, 10-20 の 二通りの最適なつなぎ方があります。 どちらの方法でもケーブルの長さの合計は 40 となります。


入力例 2

3
3
10
8
7
12
5

出力例 2

1

Score : 500 points

Problem Statement

There are N computers and N sockets in a one-dimensional world. The coordinate of the i-th computer is a_i, and the coordinate of the i-th socket is b_i. It is guaranteed that these 2N coordinates are pairwise distinct.

Snuke wants to connect each computer to a socket using a cable. Each socket can be connected to only one computer.

In how many ways can he minimize the total length of the cables? Compute the answer modulo 10^9+7.

Constraints

  • 1 ≤ N ≤ 10^5
  • 0 ≤ a_i, b_i ≤ 10^9
  • The coordinates are integers.
  • The coordinates are pairwise distinct.

Input

The input is given from Standard Input in the following format:

N
a_1
:
a_N
b_1
:
b_N

Output

Print the number of ways to minimize the total length of the cables, modulo 10^9+7.


Sample Input 1

2
0
10
20
30

Sample Output 1

2

There are two optimal connections: 0-20, 10-30 and 0-30, 10-20. In both connections the total length of the cables is 40.


Sample Input 2

3
3
10
8
7
12
5

Sample Output 2

1
B - Inscribed Bicycle

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 500

問題文

すぬけ君は、誕生日プレゼントに三角形をもらいました。 三頂点の座標は (x_1, y_1), (x_2, y_2), (x_3, y_3) でした。

すぬけ君は、三角形の内部に半径の等しい二つの円を、重ならないように描きたいです (二円が点で接していてもいいです)。 円の半径の最大値を求めてください。

制約

  • 0 ≤ x_i, y_i ≤ 1000
  • 座標は整数である。
  • 三点は同一直線上に無い。

入力

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

x_1 y_1
x_2 y_2
x_3 y_3

出力

円の半径の最大値を出力せよ。 絶対誤差または相対誤差が 10^{-9} 以下で無ければならない。


入力例 1

0 0
1 1
2 0

出力例 1

0.292893218813

入力例 2

3 1
1 5
4 9

出力例 2

0.889055514217

Score : 500 points

Problem Statement

Snuke received a triangle as a birthday present. The coordinates of the three vertices were (x_1, y_1), (x_2, y_2), and (x_3, y_3).

He wants to draw two circles with the same radius inside the triangle such that the two circles do not overlap (but they may touch). Compute the maximum possible radius of the circles.

Constraints

  • 0 ≤ x_i, y_i ≤ 1000
  • The coordinates are integers.
  • The three points are not on the same line.

Input

The input is given from Standard Input in the following format:

x_1 y_1
x_2 y_2
x_3 y_3

Output

Print the maximum possible radius of the circles. The absolute error or the relative error must be at most 10^{-9}.


Sample Input 1

0 0
1 1
2 0

Sample Output 1

0.292893218813

Sample Input 2

3 1
1 5
4 9

Sample Output 2

0.889055514217
C - Cheating Nim

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 500

問題文

チーターと不正者がニムをすることになりました。 このゲームでは、N 個の石の山を使います。 最初に i 番目の山には a_i 個の石があります。 チーターが先手で、交互にターンを取ります。 それぞれのターンでは、プレイヤーは一つの山を選び、その山から一個以上の石を取り除きます。 ターンが回ってきたときに操作ができなくなったプレイヤーの負けです。

しかし、ゲームが始まる前に、不正者はチーターがどのような動きをしても必ず勝つことができるように少し不正をすることにしました。 それぞれの山から、不正者は 0 個または 1 個の石を取り除き、ゲームが始まる前に食べます。 不正者が必ず勝てるようにする方法が複数通りある場合は、食べる石の個数を最小にするようにしたいです。

不正者が食べる石の個数を求めてください。 不正をしても不正者が必ず勝つようにできない場合は、-1 を出力してください。

制約

  • 1 ≤ N ≤ 10^5
  • 2 ≤ a_i ≤ 10^9

入力

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

N
a_1
:
a_N

出力

答えを出力せよ。


入力例 1

3
2
3
4

出力例 1

3

不正者が勝つ唯一の方法は、全ての山から石を取って食べることです。


入力例 2

3
100
100
100

出力例 2

-1

Score : 500 points

Problem Statement

A cheetah and a cheater are going to play the game of Nim. In this game they use N piles of stones. Initially the i-th pile contains a_i stones. The players take turns alternately, and the cheetah plays first. In each turn, the player chooses one of the piles, and takes one or more stones from the pile. The player who can't make a move loses.

However, before the game starts, the cheater wants to cheat a bit to make sure that he can win regardless of the moves by the cheetah. From each pile, the cheater takes zero or one stone and eats it before the game. In case there are multiple ways to guarantee his winning, he wants to minimize the number of stones he eats.

Compute the number of stones the cheater will eat. In case there is no way for the cheater to win the game even with the cheating, print -1 instead.

Constraints

  • 1 ≤ N ≤ 10^5
  • 2 ≤ a_i ≤ 10^9

Input

The input is given from Standard Input in the following format:

N
a_1
:
a_N

Output

Print the answer.


Sample Input 1

3
2
3
4

Sample Output 1

3

The only way for the cheater to win the game is to take stones from all piles and eat them.


Sample Input 2

3
100
100
100

Sample Output 2

-1
D - Dice Game

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 1000

問題文

(六面の) 赤と青のサイコロがあります。 赤いサイコロが振られたとき、i の目が出る確率は p_i パーセントであり、青いサイコロが振られたとき、 j の目が出る確率は q_j パーセントです。

Petr と tourist が次のゲームをしています。 どちらのプレイヤーも二つのサイコロの確率分布を知っています。 最初に、Petr が自分の意思で (tourist に知らせずに) サイコロを一つ選び、それを振り、出た目を tourist に伝えます。 次に、tourist が選ばれたサイコロの色を当てます。 tourist が色を当てることができれば、tourist の勝ちです。そうでなければ Petr の勝ちです。

どちらのプレイヤーも最適に行動したとき、tourist が勝つ確率を求めてください。

制約

  • 0 ≤ p_i, q_i ≤ 100
  • p_1 + ... + p_6 = q_1 + ... + q_6 = 100
  • 入力される値は全て整数である。

入力

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

p_1 p_2 p_3 p_4 p_5 p_6
q_1 q_2 q_3 q_4 q_5 q_6

出力

tourist が勝つ確率を出力せよ。 絶対誤差または相対誤差が 10^{-9} 以下で無ければならない。


入力例 1

25 25 25 25 0 0
0 0 0 0 50 50

出力例 1

1.000000000000

入力例 2

10 20 20 10 20 20
20 20 20 10 10 20

出力例 2

0.550000000000

Score : 1000 points

Problem Statement

There are two (6-sided) dice: a red die and a blue die. When a red die is rolled, it shows i with probability p_i percents, and when a blue die is rolled, it shows j with probability q_j percents.

Petr and tourist are playing the following game. Both players know the probabilistic distributions of the two dice. First, Petr chooses a die in his will (without showing it to tourist), rolls it, and tells tourist the number it shows. Then, tourist guesses the color of the chosen die. If he guesses the color correctly, tourist wins. Otherwise Petr wins.

If both players play optimally, what is the probability that tourist wins the game?

Constraints

  • 0 ≤ p_i, q_i ≤ 100
  • p_1 + ... + p_6 = q_1 + ... + q_6 = 100
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

p_1 p_2 p_3 p_4 p_5 p_6
q_1 q_2 q_3 q_4 q_5 q_6

Output

Print the probability that tourist wins. The absolute error or the relative error must be at most 10^{-9}.


Sample Input 1

25 25 25 25 0 0
0 0 0 0 50 50

Sample Output 1

1.000000000000

tourist can always win the game: If the number is at most 4, the color is definitely red. Otherwise the color is definitely blue.


Sample Input 2

10 20 20 10 20 20
20 20 20 10 10 20

Sample Output 2

0.550000000000
E - Water Distribution

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 1000

問題文

二次元平面上に N 個の都市があります。 i 番目の都市の座標は (x_i, y_i) です。 最初に、i 番目の都市にある水の量は a_i リットルです。

すぬけ君は、好きな量の水をある都市から他の都市に運ぶことができます。 しかし、水を運んでいる間に水が少し漏れます。 s 番目の都市から t 番目の都市に l リットルの水を運ぶと、目的地に着いたときに残っている水の量は max(l-d_{s,t}, 0) だけです。 ここで、d_{s,t}s 番目の都市と t 番目の都市のユークリッド距離を表します。 すぬけ君は、このタイプの操作を任意の回数繰り返すことができます。

すぬけ君は、N 個の都市にある水の量の最小値を最大化したいです。 全ての都市に少なくとも X リットルの水を配ることができるような最大の X を求めてください。

制約

  • 1 ≤ N ≤ 15
  • 0 ≤ x_i, y_i, a_i ≤ 10^9
  • 入力される全ての値は整数である。
  • どの二つの都市も同じ場所に無い。

入力

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

N
x_1 y_1 a_1
:
x_N y_N a_N

出力

N 個の都市にある水の量の最小値を最大値を出力せよ。 絶対誤差または相対誤差が 10^{-9} 以下で無ければならない。


入力例 1

3
0 0 10
2 0 5
0 5 8

出力例 1

6.500000000000

最適解は、一番目の都市から二番目の都市に 3.5 リットルの水を運ぶことです。 操作の後、一番目と二番目の都市の水の量はどちらも 6.5 リットルとなり、三番目の都市の水の量は 8 リットルとなります。


入力例 2

15
335279264 849598327 822889311
446755913 526239859 548830120
181424399 715477619 342858071
625711486 448565595 480845266
647639160 467825612 449656269
160714711 336869678 545923679
61020590 573085537 816372580
626006012 389312924 135599877
547865075 511429216 605997004
561330066 539239436 921749002
650693494 63219754 786119025
849028504 632532642 655702582
285323416 611583586 211428413
990607689 590857173 393671555
560686330 679513171 501983447

出力例 2

434666178.237122833729

Score : 1000 points

Problem Statement

There are N cities in a two-dimensional plane. The coordinates of the i-th city is (x_i, y_i). Initially, the amount of water stored in the i-th city is a_i liters.

Snuke can carry any amount of water from a city to another city. However, water leaks out a bit while he carries it. If he carries l liters of water from the s-th city to the t-th city, only max(l-d_{s,t}, 0) liters of water remains when he arrives at the destination. Here d_{s,t} denotes the (Euclidean) distance between the s-th city and the t-th city. He can perform arbitrary number of operations of this type.

Snuke wants to maximize the minimum amount of water among the N cities. Find the maximum X such that he can distribute at least X liters of water to each city.

Constraints

  • 1 ≤ N ≤ 15
  • 0 ≤ x_i, y_i, a_i ≤ 10^9
  • All values in the input are integers.
  • No two cities are at the same position.

Input

The input is given from Standard Input in the following format:

N
x_1 y_1 a_1
:
x_N y_N a_N

Output

Print the maximum of the minimum amount of water among the N cities. The absolute error or the relative error must be at most 10^{-9}.


Sample Input 1

3
0 0 10
2 0 5
0 5 8

Sample Output 1

6.500000000000

The optimal solution is to carry 3.5 liters of water from the 1st city to the 2nd city. After the operation, both the 1st and the 2nd cities will have 6.5 liters of water, and the 3rd city will have 8 liters of water.


Sample Input 2

15
335279264 849598327 822889311
446755913 526239859 548830120
181424399 715477619 342858071
625711486 448565595 480845266
647639160 467825612 449656269
160714711 336869678 545923679
61020590 573085537 816372580
626006012 389312924 135599877
547865075 511429216 605997004
561330066 539239436 921749002
650693494 63219754 786119025
849028504 632532642 655702582
285323416 611583586 211428413
990607689 590857173 393671555
560686330 679513171 501983447

Sample Output 2

434666178.237122833729
F - Intervals

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 1000

問題文

すぬけ君は N 個の区間を誕生日プレゼントとしてもらいました。 i 番目の区間は [-L_i, R_i] でした。 L_iR_i は正であることが保証されています。 つまり、原点は必ず各区間の内側にあります。

すぬけ君は区間が重なっているのが嫌いなので、いくつかの区間を動かすことにしました。 任意の正整数 d に対し、d ドル払うと一つの区間を選んでそれを距離 d 動かすことができます。 つまり、選んだ区間が [a, b] のとき、それを [a+d, b+d] または [a-d, b-d] にすることができます。

すぬけ君は、このタイプの操作を任意の回数できます。 すべての操作の後、どの二つの区間も交わっていてはいけません (境界が接していることは許されます)。 正確には、任意の二つの区間に対し、その共通部分の長さが 0 になっている必要があります。

目的を達成するのに必要な最小のコストを求めてください。

制約

  • 1 ≤ N ≤ 5000
  • 1 ≤ L_i, R_i ≤ 10^9
  • 入力される値は全て整数である。

入力

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

N
L_1 R_1
:
L_N R_N

出力

目的を達成するのに必要な最小のコストを出力せよ。


入力例 1

4
2 7
2 5
4 1
7 5

出力例 1

22

一つの最適な方法は以下の通りです:

  • 区間 [-2, 7][6, 15]8 ドルで動かす
  • 区間 [-2, 5][-1, 6]1 ドルで動かす
  • 区間 [-4, 1][-6, -1]2 ドルで動かす
  • 区間 [-7, 5][-18, -6]11 ドルで動かす

コストの合計は 8 + 1 + 2 + 11 = 22 ドルとなります。


入力例 2

20
97 2
75 25
82 84
17 56
32 2
28 37
57 39
18 11
79 6
40 68
68 16
40 63
93 49
91 10
55 68
31 80
57 18
34 28
76 55
21 80

出力例 2

7337

Score : 1000 points

Problem Statement

Snuke received N intervals as a birthday present. The i-th interval was [-L_i, R_i]. It is guaranteed that both L_i and R_i are positive. In other words, the origin is strictly inside each interval.

Snuke doesn't like overlapping intervals, so he decided to move some intervals. For any positive integer d, if he pays d dollars, he can choose one of the intervals and move it by the distance of d. That is, if the chosen segment is [a, b], he can change it to either [a+d, b+d] or [a-d, b-d].

He can repeat this type of operation arbitrary number of times. After the operations, the intervals must be pairwise disjoint (however, they may touch at a point). Formally, for any two intervals, the length of the intersection must be zero.

Compute the minimum cost required to achieve his goal.

Constraints

  • 1 ≤ N ≤ 5000
  • 1 ≤ L_i, R_i ≤ 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
L_1 R_1
:
L_N R_N

Output

Print the minimum cost required to achieve his goal.


Sample Input 1

4
2 7
2 5
4 1
7 5

Sample Output 1

22

One optimal solution is as follows:

  • Move the interval [-2, 7] to [6, 15] with 8 dollars
  • Move the interval [-2, 5] to [-1, 6] with 1 dollars
  • Move the interval [-4, 1] to [-6, -1] with 2 dollars
  • Move the interval [-7, 5] to [-18, -6] with 11 dollars

The total cost is 8 + 1 + 2 + 11 = 22 dollars.


Sample Input 2

20
97 2
75 25
82 84
17 56
32 2
28 37
57 39
18 11
79 6
40 68
68 16
40 63
93 49
91 10
55 68
31 80
57 18
34 28
76 55
21 80

Sample Output 2

7337
G - FESTIVAL

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 1000

問題文

CODE FESTIVAL 2016 へようこそ! このコンテストを祝うために、以下の条件を満たす文字列 s を一つ見つけてください:

  • s の長さは 1 以上 5000 以下である。
  • s は英大文字のみからなる。
  • s は文字列 "FESTIVAL" をちょうど K 回部分列として含む。 言い換えると、 0 ≤ i_0 < i_1 < ... < i_7 ≤ |s|-1 かつ s[i_0]='F', s[i_1]='E', ..., s[i_7]='L' を満たすような組 (i_0, i_1, ..., i_7) がちょうど K 組存在する。

与えられた制約の元では、必ず解が存在することが証明できます。 複数通りの解が考えられる場合は、どれを出力してもかまいません。

制約

  • 1 ≤ K ≤ 10^{18}

入力

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

K

出力

条件を満たす文字列を一つ出力せよ。


入力例 1

7

出力例 1

FESSSSSSSTIVAL

入力例 2

256

出力例 2

FFEESSTTIIVVAALL

Score : 1000 points

Problem Statement

Welcome to CODE FESTIVAL 2016! In order to celebrate this contest, find a string s that satisfies the following conditions:

  • The length of s is between 1 and 5000, inclusive.
  • s consists of uppercase letters.
  • s contains exactly K occurrences of the string "FESTIVAL" as a subsequence. In other words, there are exactly K tuples of integers (i_0, i_1, ..., i_7) such that 0 ≤ i_0 < i_1 < ... < i_7 ≤ |s|-1 and s[i_0]='F', s[i_1]='E', ..., s[i_7]='L'.

It can be proved that under the given constraints, the solution always exists. In case there are multiple possible solutions, you can output any.

Constraints

  • 1 ≤ K ≤ 10^{18}

Input

The input is given from Standard Input in the following format:

K

Output

Print a string that satisfies the conditions.


Sample Input 1

7

Sample Output 1

FESSSSSSSTIVAL

Sample Input 2

256

Sample Output 2

FFEESSTTIIVVAALL
H - AB=C Problem

実行時間制限: 3 sec / メモリ制限: 256 MB

配点 : 1500

問題文

すぬけ君は誕生日プレゼントとして二つの行列 AB をもらいました。 それぞれの行列は 01 のみからなる NN 列の行列です。

すぬけ君は、行列の積 C = AB を計算しました。 全ての計算を modulo 2 で行ったので、 C01 のみからなる NN 列の行列です。 1 ≤ i, j ≤ N について、行列 C(i, j) 成分 c_{i, j} が与えられます。

しかし、すぬけ君は間違って行列 AB を食べてしまったので、C のみを知っています。 (順序付きの) 行列の組 (A, B) が何通り考えられるか、modulo 10^9+7 で求めてください。

制約

  • 1 ≤ N ≤ 300
  • c_{i, j}0 または 1 である

入力

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

N
c_{1, 1} ... c_{1, N}
:
c_{N, 1} ... c_{N, N}

出力

(順序付きの) 行列の組 (A, B) が何通り考えられるか、modulo 10^9+7 で出力せよ。


入力例 1

2
0 1
1 0

出力例 1

6

入力例 2

10
1 0 0 1 1 1 0 0 1 0
0 0 0 1 1 0 0 0 1 0
0 0 1 1 1 1 1 1 1 1
0 1 0 1 0 0 0 1 1 0
0 0 1 0 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 1 0 1 0 0 0 0 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 1 0 0 0 0
1 0 1 0 0 1 1 1 1 1

出力例 2

741992411

Score : 1500 points

Problem Statement

Snuke received two matrices A and B as birthday presents. Each of the matrices is an N by N matrix that consists of only 0 and 1.

Then he computed the product of the two matrices, C = AB. Since he performed all computations in modulo two, C was also an N by N matrix that consists of only 0 and 1. For each 1 ≤ i, j ≤ N, you are given c_{i, j}, the (i, j)-element of the matrix C.

However, Snuke accidentally ate the two matrices A and B, and now he only knows C. Compute the number of possible (ordered) pairs of the two matrices A and B, modulo 10^9+7.

Constraints

  • 1 ≤ N ≤ 300
  • c_{i, j} is either 0 or 1.

Input

The input is given from Standard Input in the following format:

N
c_{1, 1} ... c_{1, N}
:
c_{N, 1} ... c_{N, N}

Output

Print the number of possible (ordered) pairs of two matrices A and B (modulo 10^9+7).


Sample Input 1

2
0 1
1 0

Sample Output 1

6

Sample Input 2

10
1 0 0 1 1 1 0 0 1 0
0 0 0 1 1 0 0 0 1 0
0 0 1 1 1 1 1 1 1 1
0 1 0 1 0 0 0 1 1 0
0 0 1 0 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 1 0 1 0 0 0 0 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 1 0 0 0 0
1 0 1 0 0 1 1 1 1 1

Sample Output 2

741992411
I - 90 and 270

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 1500

問題文

以下の条件を満たす N 角形を一つ構成してください:

  • 多角形は単純である。 (ノートを見てください)
  • 多角形の各辺は座標軸に平行である。
  • 多角形の頂点の座標は 0 以上 10^9 以下の整数である。
  • 多角形の頂点は反時計回りに 1 から N まで番号がつけられている。
  • i 番目の頂点の内角は a_i 度である。

解が複数通り考えられる場合は、どれを出力してもかまいません。

ノート

全ての辺が正の長さを持ち、どの二つの辺も (隣接する辺が頂点で接するのを除いて) 共通部分を持たないとき、多角形は単純であるという。

制約

  • 3 ≤ N ≤ 1000
  • a_i90 または 270

入力

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

N
a_1
:
a_N

出力

解が存在する場合は、以下の形式で出力せよ:

x_1 y_1
:
x_N y_N

ここで (x_i, y_i)i 番目の頂点の座標である。

解が存在しない場合は、 -1 と出力せよ。


入力例 1

8
90
90
270
90
90
90
270
90

出力例 1

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

入力例 2

3
90
90
90

出力例 2

-1

Score : 1500 points

Problem Statement

Construct an N-gon that satisfies the following conditions:

  • The polygon is simple (see notes for the definition).
  • Each edge of the polygon is parallel to one of the coordinate axes.
  • Each coordinate is an integer between 0 and 10^9, inclusive.
  • The vertices are numbered 1 through N in counter-clockwise order.
  • The internal angle at the i-th vertex is exactly a_i degrees.

In case there are multiple possible answers, you can output any.

Notes

A polygon is called simple if each edge has a positive length, and no two edges have a common point (except for adjacent edges touching at a vertex).

Constraints

  • 3 ≤ N ≤ 1000
  • a_i is either 90 or 270.

Input

The input is given from Standard Input in the following format:

N
a_1
:
a_N

Output

In case the answer exists, print the answer in the following format:

x_1 y_1
:
x_N y_N

Here (x_i, y_i) are the coordinates of the i-th vertex.

In case the answer doesn't exist, print a single -1.


Sample Input 1

8
90
90
270
90
90
90
270
90

Sample Output 1

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

Sample Input 2

3
90
90
90

Sample Output 2

-1
J - 123 Pairs

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 1500

問題文

1 以上 2N 以下の整数を考えます。 すぬけ君は、これらの整数を以下の条件を満たすように N 組のペアに分けたいです:

  • 1 以上 2N 以下の整数はそれぞれちょうど一つのペアに含まれる。
  • 差が 1 であるようなペアがちょうど A 組ある。
  • 差が 2 であるようなペアがちょうど B 組ある。
  • 差が 3 であるようなペアがちょうど C 組ある。

制約により N = A + B + C であることが保証されているので、差が 4 以上のペアは存在しません。

このようにペアに分ける方法が何通りあるか、modulo 10^9+7 で求めてください。

制約

  • 1 ≤ N ≤ 5000
  • 0 ≤ A, B, C
  • A + B + C = N

入力

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

N A B C

出力

答えを出力せよ。


入力例 1

3 1 2 0

出力例 1

2

1-2, 3-5, 4-61-3, 2-4, 5-6 の二通りの方法があります。


入力例 2

600 100 200 300

出力例 2

522158867

Score : 1500 points

Problem Statement

Consider all integers between 1 and 2N, inclusive. Snuke wants to divide these integers into N pairs such that:

  • Each integer between 1 and 2N is contained in exactly one of the pairs.
  • In exactly A pairs, the difference between the two integers is 1.
  • In exactly B pairs, the difference between the two integers is 2.
  • In exactly C pairs, the difference between the two integers is 3.

Note that the constraints guarantee that N = A + B + C, thus no pair can have the difference of 4 or more.

Compute the number of ways to do this, modulo 10^9+7.

Constraints

  • 1 ≤ N ≤ 5000
  • 0 ≤ A, B, C
  • A + B + C = N

Input

The input is given from Standard Input in the following format:

N A B C

Output

Print the answer.


Sample Input 1

3 1 2 0

Sample Output 1

2

There are two possibilities: 1-2, 3-5, 4-6 or 1-3, 2-4, 5-6.


Sample Input 2

600 100 200 300

Sample Output 2

522158867