A - Move and Win

実行時間制限: 1 sec / メモリ制限: 512 MB

配点 : 300

問題文

N 個のマスに区切られた細長い紙切れの上でゲームを行います。マスには 1 から N までの番号が順に付けられています。

アリスの駒はマス A に、ボリスの駒は別のマス B に置かれています。

二人にはターンが交互に訪れます。アリスが先手です。 ターンが回ってきたプレイヤーは、自分の駒を現在のマス X から左隣のマス X-1 か右隣のマス X+1 のどちらかに動かさなければなりません。 ただし、駒を紙切れの外に出したり、相手の駒と同じマスに動かしてはいけません。 また、駒の移動は一ターンに一度だけ行わなければなりません。

駒を動かせなくなった人が負けで、相手の勝ちとなります。

二人とも、勝ちたいと思っています。二人とも最適にプレイするとき、どちらが勝つでしょうか?

制約

  • 2 \leq N \leq 100
  • 1 \leq A < B \leq N
  • 入力値はすべて整数である。

入力

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

N A B

出力

アリスが勝つ場合は Alice、ボリスが勝つ場合は Borys、どちらも勝つことができないなら Draw と出力せよ。


入力例 1

5 2 4

出力例 1

Alice

アリスは駒をマス 3 に動かせます。 すると、ボリスは駒をマス 3 に動かすことができなくなり、マス 5 に動かすほかなくなります。 そして、アリスが駒をマス 4 に動かすと、ボリスは駒を動かせなくなり負けます。


入力例 2

2 1 2

出力例 2

Borys

アリスは最初のターンで駒を動かせず負けます。


入力例 3

58 23 42

出力例 3

Borys

Score : 300 points

Problem Statement

A game is played on a strip consisting of N cells consecutively numbered from 1 to N.

Alice has her token on cell A. Borys has his token on a different cell B.

Players take turns, Alice moves first. The moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X-1, or on the right, cell X+1. Note that it's disallowed to move the token outside the strip or to the cell with the other player's token. In one turn, the token of the moving player must be shifted exactly once.

The player who can't make a move loses, and the other player wins.

Both players want to win. Who wins if they play optimally?

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A < B \leq N
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.


Sample Input 1

5 2 4

Sample Output 1

Alice

Alice can move her token to cell 3. After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5. Then, Alice moves her token to cell 4. Borys can't make a move and loses.


Sample Input 2

2 1 2

Sample Output 2

Borys

Alice can't make the very first move and loses.


Sample Input 3

58 23 42

Sample Output 3

Borys
B - Ice Rink Game

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

配点 : 500

問題文

スケートリンクで、一人の大人の司会と N 人の子供がゲームを行います。 ゲームは K ラウンドからなり、ラウンド i では司会が次のように言います。

  • A_i 人組を作って!

すると、まだ脱落していない子供たちは A_i 人からなるグループをできるだけ多く組みます。 一人につき一つのグループにしか入れません。 グループに入れなかった子供たちは脱落し、その他は次のラウンドに進みます。 ラウンドで誰も脱落しないこともありえます。

最後まで、つまりラウンド K のあとまで残ったのは 2 人で、彼らが勝者となりました。

あなたは A_1, A_2, ..., A_K の値を聞き、N の値は知りませんが、推定してみたくなりました。

ゲームの開始前にいた子供たちの人数として考えられる最小の値と、最大の値を求めてください。もしくは、考えられる N の値は存在しないと判定してください。

制約

  • 1 \leq K \leq 10^5
  • 2 \leq A_i \leq 10^9
  • 入力値はすべて整数である。

入力

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

K
A_1 A_2 ... A_K

Output

考えられる最小の N の値と最大の N の値をそれぞれ表す二つの整数を出力せよ。ただし、問題文で述べた状況が発生しえない場合は、整数 -1 を単独で出力せよ。


入力例 1

4
3 4 3 2

出力例 1

6 8

例えば、ゲームの開始時に子供が 6 人いた場合、以下のように進行します。

  • ラウンド 1 では、6 人の子供たちが 3 人組を 2 つ作り、誰も脱落しません。
  • ラウンド 2 では、6 人の子供たちが 4 人組を 1 つ作り、2 人が脱落します。
  • ラウンド 3 では、4 人の子供たちが 3 人組を 1 つ作り、1 人が脱落します。
  • ラウンド 4 では、3 人の子供たちが 2 人組を 1 つ作り、1 人が脱落します。

最後まで残った二人が勝者となります。


入力例 2

5
3 4 100 3 2

出力例 2

-1

このような状況はありえません。 特に、ゲームの開始時の子供たちの人数が 100 人未満の場合は、ラウンド 3 で全員が脱落します。


入力例 3

10
2 2 2 2 2 2 2 2 2 2

出力例 3

2 3

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink. The game consists of K rounds. In the i-th round, the game master announces:

  • Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible. One child may belong to at most one group. Those who are left without a group leave the game. The others proceed to the next round. Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

  • 1 \leq K \leq 10^5
  • 2 \leq A_i \leq 10^9
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively, or a single integer -1 if the described situation is impossible.


Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

  • In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.
  • In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.
  • In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.
  • In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.


Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible. In particular, if the game starts with less than 100 children, everyone leaves after the third round.


Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3
C - Median Sum

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

配点 : 700

問題文

N 個の整数 A_1, A_2, ..., A_N が与えられます。

A のすべての空でない部分列について、それぞれの和を考えます。このような和は 2^N - 1 個存在し、この個数は奇数です。

これらの和を昇順に並べたものを S_1, S_2, ..., S_{2^N - 1} とします。

これらの中央値、S_{2^{N-1}} を求めてください。

制約

  • 1 \leq N \leq 2000
  • 1 \leq A_i \leq 2000
  • 入力値はすべて整数である。

入力

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

N
A_1 A_2 ... A_N

出力

A のすべての空でない部分列の和を書き並べてソートした際の中央値を出力せよ。


入力例 1

3
1 2 1

出力例 1

2

この場合、S = (1, 1, 2, 2, 3, 3, 4) となり、中央値は S_4 = 2 です。


入力例 2

1
58

出力例 2

58

この場合、S = (58) となります。

Score : 700 points

Problem Statement

You are given N integers A_1, A_2, ..., A_N.

Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number.

Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}.

Find the median of this list, S_{2^{N-1}}.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq A_i \leq 2000
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the median of the sorted list of the sums of all non-empty subsequences of A.


Sample Input 1

3
1 2 1

Sample Output 1

2

In this case, S = (1, 1, 2, 2, 3, 3, 4). Its median is S_4 = 2.


Sample Input 2

1
58

Sample Output 2

58

In this case, S = (58).

D - Min Max Repetition

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

配点 : 1100

問題文

AB を正の整数として、f(A, B) を以下の条件を満たす文字列と定めます。

  • f(A, B) の長さは A + B である。
  • f(A, B) はちょうど A 個の A とちょうど B 個の B を含む。
  • f(A, B) の部分文字列であって同じ文字からなるもの(例: AAAAABBBB)のうち最長のものの長さは、上記の二つの条件が満たされるという前提のもとで最小である。
  • f(A, B) は、上記の三つの条件が満たされるという前提のもとで辞書順最小の文字列である。

例えば、f(2, 3) = BABABf(6, 4) = AABAABAABB となります。

次の Q 個のクエリに答えてください。「f(A_i, B_i)C_i 文字目から D_i 文字目までの部分文字列(最初の文字を 1 文字目とする)を求めよ。」

制約

  • 1 \leq Q \leq 10^3
  • 1 \leq A_i, B_i \leq 5 \times 10^8
  • 1 \leq C_i \leq D_i \leq A_i + B_i
  • D_i - C_i + 1 \leq 100
  • 入力値はすべて整数である。

部分点

  • 1 \leq A_i, B_i \leq 10^3 を満たすデータセットに正答すると、500 点が与えられる。

入力

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

Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_Q B_Q C_Q D_Q

出力

入力で与えられた順に、各クエリ i について、f(A_i, B_i)C_i 文字目から D_i 文字目までの部分文字列(最初の文字を 1 文字目とする)を個別の行に出力せよ。


入力例 1

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

出力例 1

BABAB
AABAABAABB
A
BAABA
ABAB

Score : 1100 points

Problem Statement

Let f(A, B), where A and B are positive integers, be the string satisfying the following conditions:

  • f(A, B) has length A + B;
  • f(A, B) contains exactly A letters A and exactly B letters B;
  • The length of the longest substring of f(A, B) consisting of equal letters (ex., AAAAA or BBBB) is as small as possible under the conditions above;
  • f(A, B) is the lexicographically smallest string satisfying the conditions above.

For example, f(2, 3) = BABAB, and f(6, 4) = AABAABAABB.

Answer Q queries: find the substring of f(A_i, B_i) from position C_i to position D_i (1-based).

Constraints

  • 1 \leq Q \leq 10^3
  • 1 \leq A_i, B_i \leq 5 \times 10^8
  • 1 \leq C_i \leq D_i \leq A_i + B_i
  • D_i - C_i + 1 \leq 100
  • All input values are integers.

Partial Score

  • 500 points will be awarded for passing the testset satisfying 1 \leq A_i, B_i \leq 10^3.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_Q B_Q C_Q D_Q

Output

For each query i in order of input, print a line containing the substring of f(A_i, B_i) from position C_i to position D_i (1-based).


Sample Input 1

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

Sample Output 1

BABAB
AABAABAABB
A
BAABA
ABAB
E - Encoding Subsets

実行時間制限: 5 sec / メモリ制限: 512 MB

配点 : 1400

問題文

次のような、01 からなる文字列をエンコードする規則を考えます。

  • 文字列 01 はそれぞれ 01 とエンコードできる。
  • 文字列 AB をそれぞれ PQ とエンコードできる場合、文字列 ABPQ とエンコードできる。
  • 文字列 AP とエンコードできる場合、K \geq 2 を正の整数として、文字列 AA...AAK 個連結したもの)を (PxK) とエンコードできる。

例えば、文字列 00100100100100100100(1(0x2)x2)1(001x3) などとエンコードすることができ、この他のエンコード方法も存在します。

また、次の条件が満たされるとき、文字列 A は文字列 Bサブセット であると呼びます。

  • AB は長さが等しく、どちらも 01 からなる。
  • A_i = 1 であるようなすべての添字 i に対して、B_i = 1 でもある。

01 からなる文字列 S が与えられます。S のすべてのサブセットについて、それぞれをエンコードする方法が何通り存在するか求め、それらの個数の総和を 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq |S| \leq 100
  • S01 からなる。

入力

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

S

出力

S のすべてのサブセットについてのエンコード方法の個数の総和を 998244353 で割ったあまりを出力せよ。


入力例 1

011

出力例 1

9

S のサブセットは 4 個存在し、

  • 0110110(1x2) とエンコードできます。
  • 010010 とエンコードできます。
  • 001001(0x2)1 とエンコードできます。
  • 0000000(0x2)(0x2)0(0x3) とエンコードできます。

したがって、S のすべてのサブセットについてのエンコード方法の個数の総和は 2 + 1 + 2 + 4 = 9 通りです。


入力例 2

0000

出力例 2

10

今回は S のサブセットは 1 個しか存在しませんが、10 通りの方法でエンコードできます。


入力例 3

101110

出力例 3

156

入力例 4

001110111010110001100000100111

出力例 4

363383189

結果を 998244353 で割ったあまりを出力することを忘れずに。

Score : 1400 points

Problem Statement

Consider the following set of rules for encoding strings consisting of 0 and 1:

  • Strings 0 and 1 can be encoded as 0 and 1, respectively.
  • If strings A and B can be encoded as P and Q, respectively, then string AB can be encoded as PQ.
  • If string A can be encoded as P and K \geq 2 is a positive integer, then string AA...A (A repeated K times) can be encoded as (PxK).

For example, string 001001001, among other possibilities, can be encoded as 001001001, 00(1(0x2)x2)1 and (001x3).

Let's call string A a subset of string B if:

  • A and B are equal in length and consist of 0 and 1;
  • for all indices i such that A_i = 1, it's also true that B_i = 1.

You are given string S consisting of 0 and 1. Find the total number of distinct encodings of all subsets of S, modulo 998244353.

Constraints

  • 1 \leq |S| \leq 100
  • S consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the total number of distinct encodings of all subsets of S modulo 998244353.


Sample Input 1

011

Sample Output 1

9

There are four subsets of S:

  • 011 can be encoded as 011 and 0(1x2);
  • 010 can be encoded as 010;
  • 001 can be encoded as 001 and (0x2)1;
  • 000 can be encoded as 000, 0(0x2), (0x2)0 and (0x3).

Thus, the total number of encodings of all subsets of S is 2 + 1 + 2 + 4 = 9.


Sample Input 2

0000

Sample Output 2

10

This time S has only one subset, but it can be encoded in 10 different ways.


Sample Input 3

101110

Sample Output 3

156

Sample Input 4

001110111010110001100000100111

Sample Output 4

363383189

Don't forget to take the result modulo 998244353.

F - Arcs on a Circle

実行時間制限: 5 sec / メモリ制限: 512 MB

配点 : 2100

問題文

長さ C の円周があり、この上に N 本の円弧を配置します。円弧 i の長さは L_i です。

それぞれの円弧 i は、円周上の一様ランダムな位置に配置されます。 すなわち、円周上のランダムな点が選ばれ、その点を中心とした長さ L_i の円弧が出現します。

これらの円弧は、それぞれ独立に配置されます。例えば、円弧が交差したり、ある円弧が別の円弧を含むことがあります。

円周上のすべての点が少なくとも一本の円弧で覆われる確率はいくらでしょうか? 円弧はその両端も覆うものとします。

制約

  • 2 \leq N \leq 6
  • 2 \leq C \leq 50
  • 1 \leq L_i < C
  • 入力値はすべて整数である。

入力

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

N C
L_1 L_2 ... L_N

出力

円周上のすべての点が少なくとも一本の円弧で覆われる確率を出力せよ。 解答は、絶対誤差が 10^{-11} 以下であれば正答とみなされる。


入力例 1

2 3
2 2

出力例 1

0.3333333333333333

二本の円弧の中心間の距離が 1 以上でなければなりません。長さ 3 の円周上でそのようになる確率は 1 / 3 です。


入力例 2

4 10
1 2 3 4

出力例 2

0.0000000000000000

円弧の長さの合計がちょうど C であり、円周上のすべての点が少なくとも一本の円弧に覆われることはありえますが、この事象の発生確率は 0 です。


入力例 3

4 2
1 1 1 1

出力例 3

0.5000000000000000

入力例 4

3 5
2 2 4

出力例 4

0.4000000000000000

入力例 5

4 6
4 1 3 2

出力例 5

0.3148148148148148

入力例 6

6 49
22 13 27 8 2 19

出力例 6

0.2832340720702695

Score : 2100 points

Problem Statement

You have a circle of length C, and you are placing N arcs on it. Arc i has length L_i.

Every arc i is placed on the circle uniformly at random: a random real point on the circle is chosen, then an arc of length L_i centered at this point appears.

Note that the arcs are placed independently. For example, they may intersect or contain each other.

What is the probability that every real point of the circle will be covered by at least one arc? Assume that an arc covers its ends.

Constraints

  • 2 \leq N \leq 6
  • 2 \leq C \leq 50
  • 1 \leq L_i < C
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
L_1 L_2 ... L_N

Output

Print the probability that every real point of the circle will be covered by at least one arc. Your answer will be considered correct if its absolute error doesn't exceed 10^{-11}.


Sample Input 1

2 3
2 2

Sample Output 1

0.3333333333333333

The centers of the two arcs must be at distance at least 1. The probability of this on a circle of length 3 is 1 / 3.


Sample Input 2

4 10
1 2 3 4

Sample Output 2

0.0000000000000000

Even though the total length of the arcs is exactly C and it's possible that every real point of the circle is covered by at least one arc, the probability of this event is 0.


Sample Input 3

4 2
1 1 1 1

Sample Output 3

0.5000000000000000

Sample Input 4

3 5
2 2 4

Sample Output 4

0.4000000000000000

Sample Input 5

4 6
4 1 3 2

Sample Output 5

0.3148148148148148

Sample Input 6

6 49
22 13 27 8 2 19

Sample Output 6

0.2832340720702695