A - Magic

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 1000 points

Problem Statement

Snuke participated in a magic show.

A magician prepared N identical-looking boxes. He put a treasure in one of the boxes, closed the boxes, shuffled them, and numbered them 1 through N.

Since the boxes are shuffled, now Snuke has no idea which box contains the treasure. Snuke wins the game if he opens a box containing the treasure. You may think that if Snuke opens all boxes at once, he can always win the game. However, there are some tricks:

  • Snuke must open the boxes one by one. After he opens a box and checks the content of the box, he must close the box before opening the next box.
  • He is only allowed to open Box i at most a_i times.
  • The magician may secretly move the treasure from a closed box to another closed box, using some magic trick. For simplicity, assume that the magician never moves the treasure while Snuke is opening some box. However, he can move it at any other time (before Snuke opens the first box, or between he closes some box and opens the next box).
  • The magician can perform the magic trick at most K times.

Can Snuke always win the game, regardless of the initial position of the treasure and the movements of the magician?

Constraints

  • 2 \leq N \leq 50
  • 1 \leq K \leq 50
  • 1 \leq a_i \leq 100

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 \cdots a_N

Output

If the answer is no, print a single -1.

Otherwise, print one possible move of Snuke in the following format:

Q
x_1 x_2 \cdots x_Q

It means that he opens boxes x_1, x_2, \cdots, x_Q in this order.

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


Sample Input 1

2 1
5 5

Sample Output 1

7
1 1 2 1 2 2 1

If Snuke opens the boxes 7 times in the order 1 \rightarrow 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2 \rightarrow 1, he can always find the treasure regardless of its initial position and the movements of the magician.


Sample Input 2

3 50
5 10 15

Sample Output 2

-1

配点 : 1000

問題文

すぬけ君はマジックショーでの勝負に参加しました。

ショーのマジシャンは、見た目が同じ N 個の箱を用意していました。 彼はそのうち一つに財宝を入れてからそれらの箱を閉じ、箱の順番を無作為に入れ替えてから 1 から N までの番号を振りました。

箱の順番を入れ替えられてしまったので、どの箱に財宝が入っているかはすぬけ君には分からなくなりました。 この勝負は、すぬけ君が財宝の入った箱を開けることができれば彼の勝ちです。 それなら、すべての箱を一斉に開ければ必ず勝てるではないかと思われるかもしれません。 しかし、以下のようにいくつか特殊なルールがあります。

  • すぬけ君は、箱を一つずつ開けなければならない。一つの箱を開けてその中身を確認したら、その箱は次の箱を開ける前に閉じなければならない。
  • i は、最大で a_i 回しか開けてはならない。
  • マジシャンは、何らかの手品を用いて閉じた箱から別の閉じた箱に財宝を移すことがある。 簡略化のため、すぬけ君がいずれかの箱を開けている間に財宝が移されることはないとしてよいが、それ以外のあらゆる機会 (すぬけ君が最初に箱を開ける前、またはすぬけ君がいずれかの箱を閉じてから次の箱を開けるまでの間) に財宝が移される可能性がある。
  • マジシャンは、上記の財宝の移動を最大で K 回行う可能性がある。

すぬけ君は、財宝の初期位置やマジシャンがどのように財宝を移動させるかによらず、必ず勝負に勝つことができるでしょうか?

制約

  • 2 \leq N \leq 50
  • 1 \leq K \leq 50
  • 1 \leq a_i \leq 100

入力

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

N K
a_1 a_2 \cdots a_N

出力

問題の答えが No であれば、-1 とのみ出力せよ。

そうでなければ、すぬけ君が取りうる戦略をひとつ以下の形式で出力せよ。

Q
x_1 x_2 \cdots x_Q

これは、箱 x_1, x_2, \cdots, x_Q をこの順に開けることを表す。

複数の解が考えられる場合は、そのうちのどれを出力してもよい。


入力例 1

2 1
5 5

出力例 1

7
1 1 2 1 2 2 1

1 \rightarrow 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2 \rightarrow 1 の順に 7 回箱を開ければ、財宝の初期位置やマジシャンがどのように財宝を移動させるかによらず、必ず財宝を見つけることができます。


入力例 2

3 50
5 10 15

出力例 2

-1
B - Multiple of Nine

Time Limit: 4 sec / Memory Limit: 1024 MB

Score : 1400 points

Problem Statement

Count the number of strings S that satisfy the following constraints, modulo 10^9 + 7.

  • The length of S is exactly N.
  • S consists of digits (0...9).
  • You are given Q intervals. For each i (1 \leq i \leq Q), the integer represented by S[l_i \ldots r_i] (the substring of S between the l_i-th (1-based) character and the r_i-th character, inclusive) must be a multiple of 9.

Here, the string S and its substrings may have leading zeroes. For example, 002019 represents the integer 2019.

Constraints

  • 1 \leq N \leq 10^9
  • 1 \leq Q \leq 15
  • 1 \leq l_i \leq r_i \leq N

Input

Input is given from Standard Input in the following format:

N
Q
l_1 r_1
:
l_Q r_Q

Output

Print the number of strings that satisfy the conditions, modulo 10^9 + 7.


Sample Input 1

4
2
1 2
2 4

Sample Output 1

136

For example, S = 9072 satisfies the conditions because both S[1 \ldots 2] = 90 and S[2 \ldots 4] = 072 represent multiples of 9.


Sample Input 2

6
3
2 5
3 5
1 3

Sample Output 2

2720

Sample Input 3

20
10
2 15
5 6
1 12
7 9
2 17
5 15
2 4
16 17
2 12
8 17

Sample Output 3

862268030

配点 : 1400

問題文

以下の条件を満たす文字列 S の個数を 10^9 + 7 で割った余りを求めてください。

  • S の長さはちょうど N である。
  • S は数字 (0...9) からなる。
  • Q 個の区間が与えられる。各 i (1 \leq i \leq Q) に対し、S[l_i \ldots r_i] (Sl_i 文字目 (先頭を 1 文字目とする) から r_i 文字目まで (両端含む) の部分文字列) が表す整数は 9 の倍数でなければならない。

ここで、文字列 S やその部分文字列が 0 から始まっても構いません。 例えば、002019 は整数 2019 を表します。

制約

  • 1 \leq N \leq 10^9
  • 1 \leq Q \leq 15
  • 1 \leq l_i \leq r_i \leq N

入力

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

N
Q
l_1 r_1
:
l_Q r_Q

出力

条件を満たす文字列の個数を 10^9 + 7 で割った余りを出力せよ。


入力例 1

4
2
1 2
2 4

出力例 1

136

例えば、S = 9072 は条件を満たします。なぜなら、S[1 \ldots 2] = 90S[2 \ldots 4] = 072 がいずれも 9 の倍数であるためです。


入力例 2

6
3
2 5
3 5
1 3

出力例 2

2720

入力例 3

20
10
2 15
5 6
1 12
7 9
2 17
5 15
2 4
16 17
2 12
8 17

出力例 3

862268030
C1 - Triangular Lamps Easy

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 1000 points

Problem Statement

There is an infinitely large triangular grid, as shown below. Each point with integer coordinates contains a lamp.

Initially, only the lamp at (X, 0) was on, and all other lamps were off. Then, Snuke performed the following operation zero or more times:

  • Choose two integers x and y. Toggle (on to off, off to on) the following three lamps: (x, y), (x, y+1), (x+1, y).

After the operations, N lamps (x_1, y_1), \cdots, (x_N, y_N) are on, and all other lamps are off. Find X.

Constraints

  • 1 \leq N \leq 10^5
  • -10^{17} \leq x_i, y_i \leq 10^{17}
  • (x_i, y_i) are pairwise distinct.
  • The input is consistent with the statement, and you can uniquely determine X.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print X.


Sample Input 1

4
-2 1
-2 2
0 1
1 0

Sample Output 1

-1

The following picture shows one possible sequence of operations:

配点 : 1000

問題文

以下のような、無限に広がる三角グリッドがあります。 座標がともに整数であるような点のそれぞれには、ランプがひとつ設置されています。

はじめ、(X, 0) のランプのみが点灯しており、その他のランプはすべて消灯していました。 この状態から、すぬけ君が次の操作を 0 回以上行いました。

  • 2 つの整数 x, y を選ぶ。 3 つのランプ (x, y), (x, y+1), (x+1, y) の状態を切り替える (点灯していれば消灯させ、消灯していれば点灯させる)。

この操作のあと、N 個のランプ (x_1, y_1), \cdots, (x_N, y_N) が点灯しており、その他のランプはすべて消灯していました。 X を求めてください。

制約

  • 1 \leq N \leq 10^5
  • -10^{17} \leq x_i, y_i \leq 10^{17}
  • (x_i, y_i) は互いに異なる。
  • 入力は問題文と矛盾せず、X は一意に定まる。

入力

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

N
x_1 y_1
:
x_N y_N

出力

X を出力せよ。


入力例 1

4
-2 1
-2 2
0 1
1 0

出力例 1

-1

行われた操作の列として考えられるものをひとつ、以下の画像に示します。

C2 - Triangular Lamps Hard

Time Limit: 4 sec / Memory Limit: 1024 MB

Score : 1000 points

Problem Statement

Red bold fonts show the difference from C1.

There is an infinitely large triangular grid, as shown below. Each point with integer coordinates contains a lamp.

Initially, only the lamp at (X, Y) was on, and all other lamps were off. Then, Snuke performed the following operation zero or more times:

  • Choose two integers x and y. Toggle (on to off, off to on) the following three lamps: (x, y), (x, y+1), (x+1, y).

After the operations, N lamps (x_1, y_1), \cdots, (x_N, y_N) are on, and all other lamps are off. Find X and Y.

Constraints

  • 1 \leq N \leq 10^4
  • -10^{17} \leq x_i, y_i \leq 10^{17}
  • (x_i, y_i) are pairwise distinct.
  • The input is consistent with the statement, and you can uniquely determine X and Y.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print X and Y, separated by a space.


Sample Input 1

4
-2 1
-2 2
0 1
1 0

Sample Output 1

-1 0

The following picture shows one possible sequence of operations:

配点 : 1000

問題文

C1 との相違点を赤字で示します。

以下のような、無限に広がる三角グリッドがあります。 座標がともに整数であるような点のそれぞれには、ランプがひとつ設置されています。

はじめ、(X, Y) のランプのみが点灯しており、その他のランプはすべて消灯していました。 この状態から、すぬけ君が次の操作を 0 回以上行いました。

  • 2 つの整数 x, y を選ぶ。 3 つのランプ (x, y), (x, y+1), (x+1, y) の状態を切り替える (点灯していれば消灯させ、消灯していれば点灯させる)。

この操作のあと、N 個のランプ (x_1, y_1), \cdots, (x_N, y_N) が点灯しており、その他のランプはすべて消灯していました。 XY を求めてください。

制約

  • 1 \leq N \leq 10^4
  • -10^{17} \leq x_i, y_i \leq 10^{17}
  • (x_i, y_i) は互いに異なる。
  • 入力は問題文と矛盾せず、X, Y は一通りに定まる。

入力

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

N
x_1 y_1
:
x_N y_N

出力

XY を空白で区切って出力せよ。


入力例 1

4
-2 1
-2 2
0 1
1 0

出力例 1

-1 0

行われた操作の列として考えられるものをひとつ、以下の画像に示します。

D - Distinct Boxes

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 2000 points

Problem Statement

Snuke has R red balls and B blue balls. He distributes them into K boxes, so that no box is empty and no two boxes are identical. Compute the maximum possible value of K.

Formally speaking, let's number the boxes 1 through K. If Box i contains r_i red balls and b_i blue balls, the following conditions must be satisfied:

  • For each i (1 \leq i \leq K), r_i > 0 or b_i > 0.
  • For each i, j (1 \leq i < j \leq K), r_i \neq r_j or b_i \neq b_j.
  • \sum r_i = R and \sum b_i = B (no balls can be left outside the boxes).

Constraints

  • 1 \leq R, B \leq 10^{9}

Input

Input is given from Standard Input in the following format:

R B

Output

Print the maximum possible value of K.


Sample Input 1

8 3

Sample Output 1

5

The following picture shows one possible way to achieve K = 5:

配点 : 2000

問題文

すぬけ君は R 個の赤いボールと B 個の青いボールを持っています。 彼は、これらのボールを K 個の箱に分けて入れます。このとき、どの箱も空でなく、またどのふたつの箱も中身が一致しないようにします。 K のとりうる最大値を求めてください。

より形式的には、箱に 1 から K までの番号を振り、箱 ir_i 個の赤いボールと b_i 個の青いボールを含むとすると、次の条件が満たされなければなりません。

  • i (1 \leq i \leq K) に対し、r_i > 0 または b_i > 0 である。
  • i, j (1 \leq i < j \leq K) に対し、r_i \neq r_j または b_i \neq b_j である。
  • \sum r_i = R かつ \sum b_i = B である (箱の外にボールが取り残されてはならない)。

制約

  • 1 \leq R, B \leq 10^{9}

入力

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

R B

出力

K のとりうる最大値を出力せよ。


入力例 1

8 3

出力例 1

5

K = 5 を達成する方法として考えられるものをひとつ、以下の画像に示します。

E - e

Time Limit: 4 sec / Memory Limit: 1024 MB

Score : 2718 points

Problem Statement

There is a very long bench. The bench is divided into M sections, where M is a very large integer.

Initially, the bench is vacant. Then, M people come to the bench one by one, and perform the following action:

  • We call a section comfortable if the section is currently unoccupied and is not adjacent to any occupied sections. If there is no comfortable section, the person leaves the bench. Otherwise, the person chooses one of comfortable sections uniformly at random, and sits there. (The choices are independent from each other).

After all M people perform actions, Snuke chooses an interval of N consecutive sections uniformly at random (from M-N+1 possible intervals), and takes a photo. His photo can be described by a string of length N consisting of X and -: the i-th character of the string is X if the i-th section from the left in the interval is occupied, and - otherwise. Note that the photo is directed. For example, -X--X and X--X- are different photos.

What is the probability that the photo matches a given string s? This probability depends on M. You need to compute the limit of this probability when M goes infinity.

Here, we can prove that the limit can be uniquely written in the following format using three rational numbers p, q, r and e = 2.718 \ldots (the base of natural logarithm):

p + \frac{q}{e} + \frac{r}{e^2}

Your task is to compute these three rational numbers, and print them modulo 10^9 + 7, as described in Notes section.

Notes

When you print a rational number, first write it as a fraction \frac{y}{x}, where x, y are integers and x is not divisible by 10^9 + 7 (under the constraints of the problem, such representation is always possible). Then, you need to print the only integer z between 0 and 10^9 + 6, inclusive, that satisfies xz \equiv y \pmod{10^9 + 7}.

Constraints

  • 1 \leq N \leq 1000
  • |s| = N
  • s consists of X and -.

Input

Input is given from Standard Input in the following format:

N
s

Output

Print three rational numbers p, q, r, separated by spaces.


Sample Input 1

1
X

Sample Output 1

500000004 0 500000003

The probability that a randomly chosen section is occupied converge to \frac{1}{2} - \frac{1}{2e^2}.


Sample Input 2

3
---

Sample Output 2

0 0 0

After the actions, no three consecutive unoccupied sections can be left.


Sample Input 3

5
X--X-

Sample Output 3

0 0 1

The limit is \frac{1}{e^2}.


Sample Input 4

5
X-X-X

Sample Output 4

500000004 0 833333337

The limit is \frac{1}{2} - \frac{13}{6e^2}.


Sample Input 5

20
-X--X--X-X--X--X-X-X

Sample Output 5

0 0 183703705

The limit is \frac{7}{675e^2}.


Sample Input 6

100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-

Sample Output 6

0 0 435664291

配点 : 2718

問題文

非常に細長いベンチがあります。 このベンチは M 個の区画に分かれています。ここで、M は非常に大きい整数です。

はじめ、ベンチには誰も座っていません。 このベンチに M 人の人たちが一人ずつ訪れ、以下の行動を行います。

  • まだ人が座っておらず、人が座っている区画と隣接もしていないような区画を 快適 であると呼ぶことにする。 快適な区画が存在しなければ、ベンチから立ち去る。 そうでなければ、快適な区画の一つを一様ランダムに選んでそこに座る (人々の座る区画の選択は互いに独立である)。

M 人全員が上記の行動を取ったあと、すぬけ君は N 個の連続する区画からなる区間を (M-N+1 個の候補から) 一様ランダムに選び、その区間の写真を撮ります。 この写真は、X- からなる長さ N の次のような文字列により表現できます: i 文字目は、区間の左から i 番目の区画に人が座っていれば X、そうでなければ - であるような文字列。 なお、写真の左右は区別されます。 例えば、-X--XX--X- は異なる写真です。

撮った写真が与えられる文字列 s に一致する確率はいくつでしょうか? この確率は M に依存しますが、M が限りなく大きくなるときのこの確率の極限を求めてください。

ここで、この極限は 3 つの有理p, q, re = 2.718 \ldots (自然対数の底) を用いて以下の形式で一意に表せることが証明できます。

p + \frac{q}{e} + \frac{r}{e^2}

これら 3 つの有理数を求め、それらを注記で述べるように mod 10^9 + 7 で出力してください。

注記

有理数を出力する際は、まずその有理数を分数 \frac{y}{x} として表してください。ここで、x, y は整数であり、x10^9 + 7 で割り切れてはなりません (この問題の制約下で、そのような表現は必ず可能です)。そして、xz \equiv y \pmod{10^9 + 7} を満たすような 0 以上 10^9 + 6 以下の唯一の整数 z を出力してください。

制約

  • 1 \leq N \leq 1000
  • |s| = N
  • sX- からなる。

入力

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

N
s

出力

3 つの有理数 p, q, r を空白で区切って出力せよ。


入力例 1

1
X

出力例 1

500000004 0 500000003

ランダムに選ばれた区画に人が座っている確率は \frac{1}{2} - \frac{1}{2e^2} に収束します。


入力例 2

3
---

出力例 2

0 0 0

人々の行動のあと、人が座っていない区画が 3 つ連続して残ることはありません。


入力例 3

5
X--X-

出力例 3

0 0 1

極限は \frac{1}{e^2} です。


入力例 4

5
X-X-X

出力例 4

500000004 0 833333337

極限は \frac{1}{2} - \frac{13}{6e^2} です。


入力例 5

20
-X--X--X-X--X--X-X-X

出力例 5

0 0 183703705

極限は \frac{7}{675e^2} です。


入力例 6

100
X-X-X-X-X-X-X-X-X-X--X-X-X-X-X-X-X-X-X-X-X-X-X-X-X--X--X-X-X-X--X--X-X-X--X-X-X--X-X--X--X-X--X-X-X-

出力例 6

0 0 435664291