C - Good Sequence

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

長さ N の正整数の列 a = (a_1, a_2, ..., a_N) が与えられます。 あなたの目標は、a のうちいくつかの要素を取り除き、a良い数列 にすることです。

ここで、数列 b良い数列 であるとは、次の条件が成り立つことです。

  • b の各要素 x について、b に値 x はちょうど x 個含まれる。

例えば、(3, 3, 3), (4, 2, 4, 1, 4, 2, 4), () (空の数列) は良い数列です。 一方、(3, 3, 3, 3), (2, 4, 1, 4, 2) は良い数列ではありません。

a を良い数列にするために取り除くべき要素の個数の最小値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • a_i は整数である。
  • 1 \leq a_i \leq 10^9

入力

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

N
a_1 a_2 ... a_N

出力

a を良い数列にするために取り除くべき要素の個数の最小値を出力せよ。


入力例 1

4
3 3 3 3

出力例 1

1

例えば、要素 31 個取り除くと、(3, 3, 3) は良い数列になります。


入力例 2

5
2 4 1 4 2

出力例 2

2

例えば、要素 42 個取り除くと、(2, 1, 2) は良い数列になります。


入力例 3

6
1 2 2 3 3 3

出力例 3

0

入力例 4

1
1000000000

出力例 4

1

要素 10^91 個取り除くと、() は良い数列になります。


入力例 5

8
2 7 1 8 2 8 1 8

出力例 5

5

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N). Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

  • For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

  • 1 \leq N \leq 10^5
  • a_i is an integer.
  • 1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.


Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.


Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.


Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.


Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5
D - FT Robot

Time Limit: 2 sec / Memory Limit: 512 MB

配点 : 500

問題文

二次元平面の原点にロボットが置かれています。 最初、ロボットは x 軸の正の向きを向いています。

このロボットに命令列 s が与えられます。 s は次の 2 文字のみからなり、先頭から末尾まで順に実行されます。

  • F : 今向いている向きに長さ 1 だけ移動する。
  • T : 時計回りまたは反時計回りの好きな方向に 90 度だけ向きを変える。

ロボットの目標は、命令列をすべて実行し終わった後に座標 (x, y) にいることです。 この目標が達成可能か判定してください。

制約

  • sF, T のみからなる。
  • 1 \leq |s| \leq 8\ 000
  • x, y は整数である。
  • |x|, |y| \leq |s|

入力

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

s
x y

出力

目標が達成可能ならば Yes を出力し、達成不可能ならば No を出力せよ。


入力例 1

FTFFTFFF
4 2

出力例 1

Yes

1 番目の T で反時計周りに 90 度だけ向きを変え、2 番目の T で時計周りに 90 度だけ向きを変えればよいです。


入力例 2

FTFFTFFF
-2 -2

出力例 2

Yes

1 番目の T で時計周りに 90 度だけ向きを変え、2 番目の T で時計周りに 90 度だけ向きを変えればよいです。


入力例 3

FF
1 0

出力例 3

No

入力例 4

TF
1 0

出力例 4

No

入力例 5

FFTTFF
0 0

出力例 5

Yes

例えば、1 番目の T で反時計周りに 90 度だけ向きを変え、2 番目の T で反時計周りに 90 度だけ向きを変えればよいです。


入力例 6

TTTT
1 0

出力例 6

No

Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane. Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s. s consists of the following two kinds of letters, and will be executed in order from front to back.

  • F : Move in the current direction by distance 1.
  • T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed. Determine whether this objective is achievable.

Constraints

  • s consists of F and T.
  • 1 \leq |s| \leq 8 000
  • x and y are integers.
  • |x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.


Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.


Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.


Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.


Sample Input 6

TTTT
1 0

Sample Output 6

No
E - Prefix-free Game

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 700

問題文

文字列 s, t について、st の prefix でなく、ts の prefix でないとき、s, t は prefix-free であると言います。

L を正の整数とします。 文字列集合 S良い文字列集合 であるとは、次の条件が成り立つことです。

  • S の各文字列は、長さ 1 以上 L 以下であり、文字 0, 1 のみからなる。
  • S の相異なる 2 つの文字列のペアはいずれも prefix-free である。

良い文字列集合 S = \{ s_1, s_2, ..., s_N \} があります。 Alice と Bob が次のゲームで勝負します。 二人は交互に次の操作を行います。 Alice が先手です。

  • S に新しい文字列をひとつ追加する。 ただし、追加後の S は良い文字列集合のままでなければならない。

先に操作を行えなくなった方が負けです。 二人が最適に行動するとき、どちらが勝つか判定してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq L \leq 10^{18}
  • s_1, s_2, ..., s_N はすべて相異なる。
  • \{ s_1, s_2, ..., s_N \} は良い文字列集合である。
  • |s_1| + |s_2| + ... + |s_N| \leq 10^5

入力

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

N L
s_1
s_2
:
s_N

出力

Alice が勝つならば Alice を、Bob が勝つならば Bob を出力せよ。


入力例 1

2 2
00
01

出力例 1

Alice

Alice が 1 を追加すると、Bob は新たに文字列を追加できなくなります。


入力例 2

2 2
00
11

出力例 2

Bob

初手で Alice が追加できる文字列は 01, 102 通りです。 初手で Alice が 01 を追加した場合は、Bob が 10 を追加すると、Alice は新たに文字列を追加できなくなります。 初手で Alice が 10 を追加した場合も、Bob が 01 を追加すると、Alice は新たに文字列を追加できなくなります。


入力例 3

3 3
0
10
110

出力例 3

Alice

Alice が 111 を追加すると、Bob は新たに文字列を追加できなくなります。


入力例 4

2 1
0
1

出力例 4

Bob

初手で Alice は新たに文字列を追加できません。


入力例 5

1 2
11

出力例 5

Alice

入力例 6

2 3
101
11

出力例 6

Bob

Score : 700 points

Problem Statement

For strings s and t, we will say that s and t are prefix-free when neither is a prefix of the other.

Let L be a positive integer. A set of strings S is a good string set when the following conditions hold true:

  • Each string in S has a length between 1 and L (inclusive) and consists of the characters 0 and 1.
  • Any two distinct strings in S are prefix-free.

We have a good string set S = \{ s_1, s_2, ..., s_N \}. Alice and Bob will play a game against each other. They will alternately perform the following operation, starting from Alice:

  • Add a new string to S. After addition, S must still be a good string set.

The first player who becomes unable to perform the operation loses the game. Determine the winner of the game when both players play optimally.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq L \leq 10^{18}
  • s_1, s_2, ..., s_N are all distinct.
  • { s_1, s_2, ..., s_N } is a good string set.
  • |s_1| + |s_2| + ... + |s_N| \leq 10^5

Input

Input is given from Standard Input in the following format:

N L
s_1
s_2
:
s_N

Output

If Alice will win, print Alice; if Bob will win, print Bob.


Sample Input 1

2 2
00
01

Sample Output 1

Alice

If Alice adds 1, Bob will be unable to add a new string.


Sample Input 2

2 2
00
11

Sample Output 2

Bob

There are two strings that Alice can add on the first turn: 01 and 10. In case she adds 01, if Bob add 10, she will be unable to add a new string. Also, in case she adds 10, if Bob add 01, she will be unable to add a new string.


Sample Input 3

3 3
0
10
110

Sample Output 3

Alice

If Alice adds 111, Bob will be unable to add a new string.


Sample Input 4

2 1
0
1

Sample Output 4

Bob

Alice is unable to add a new string on the first turn.


Sample Input 5

1 2
11

Sample Output 5

Alice

Sample Input 6

2 3
101
11

Sample Output 6

Bob
F - Squirrel Migration

Time Limit: 5 sec / Memory Limit: 512 MB

配点 : 800

問題文

N 頂点の木があります。 頂点には 1 から N まで番号が振られています。 i (1 \leq i \leq N - 1) 番目の辺は頂点 x_iy_i を結んでいます。 頂点 v, w (1 \leq v, w \leq N) について、v, w 間の距離 d(v, w) を「パス v-w に含まれる辺の本数」と定義します。

木の各頂点にはリスが 1 匹ずつ住んでいます。 リスたちは次のようにして引っ越しをすることにしました。 まず、(1, 2, ..., N) の順列 p = (p_1, p_2, ..., p_N) を自由にひとつ選びます。 その後、各 1 \leq i \leq N について、頂点 i に住んでいたリスは頂点 p_i へ引っ越します。

リスたちは移動が好きなので、各リスの移動距離の総和が最大になるように引っ越しをすることにしました。 すなわち、d(1, p_1) + d(2, p_2) + ... + d(N, p_N) が最大になるように p を選ぶことにしました。 このような p の選び方は何通りあるでしょうか? 10^9 + 7 で割った余りを求めてください。

制約

  • 2 \leq N \leq 5,000
  • 1 \leq x_i, y_i \leq N
  • 入力のグラフは木である。

入力

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

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

出力

条件を満たすような p の選び方は何通りあるか? 10^9 + 7 で割った余りを求めよ。


入力例 1

3
1 2
2 3

出力例 1

3

各リスの移動距離の総和の最大値は 4 です。 条件を満たす p は次の 3 通りです。

  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

入力例 2

4
1 2
1 3
1 4

出力例 2

11

各リスの移動距離の総和の最大値は 6 です。 例えば、p = (2, 1, 4, 3) は条件を満たします。


入力例 3

6
1 2
1 3
1 4
2 5
2 6

出力例 3

36

入力例 4

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

出力例 4

396

Score : 800 points

Problem Statement

There is a tree with N vertices. The vertices are numbered 1 through N. The i-th edge (1 \leq i \leq N - 1) connects Vertex x_i and y_i. For vertices v and w (1 \leq v, w \leq N), we will define the distance between v and w d(v, w) as "the number of edges contained in the path v-w".

A squirrel lives in each vertex of the tree. They are planning to move, as follows. First, they will freely choose a permutation of (1, 2, ..., N), p = (p_1, p_2, ..., p_N). Then, for each 1 \leq i \leq N, the squirrel that lived in Vertex i will move to Vertex p_i.

Since they like long travels, they have decided to maximize the total distance they traveled during the process. That is, they will choose p so that d(1, p_1) + d(2, p_2) + ... + d(N, p_N) will be maximized. How many such ways are there to choose p, modulo 10^9 + 7?

Constraints

  • 2 \leq N \leq 5,000
  • 1 \leq x_i, y_i \leq N
  • The input 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 the number of the ways to choose p so that the condition is satisfied, modulo 10^9 + 7.


Sample Input 1

3
1 2
2 3

Sample Output 1

3

The maximum possible distance traveled by squirrels is 4. There are three choices of p that achieve it, as follows:

  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

11

The maximum possible distance traveled by squirrels is 6. For example, p = (2, 1, 4, 3) achieves it.


Sample Input 3

6
1 2
1 3
1 4
2 5
2 6

Sample Output 3

36

Sample Input 4

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

Sample Output 4

396