A - Candy Distribution Again

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 人の子供がいます。 子供たちには 1, 2, ..., N と番号が振られています。

すぬけ君は、x 個のお菓子を子供たちに配ることにしました。 このとき、すぬけ君は x 個のお菓子をすべて配り切らなければなりません。 なお、お菓子を貰わない子供がいても構いません。

i (1 \leq i \leq N) について、子供 i はちょうど a_i 個のお菓子を貰うと喜びます。 すぬけ君は、お菓子を配る方法を工夫し、喜ぶ子供の人数を最大化しようとしています。 喜ぶ子供の人数の最大値を求めてください。

制約

  • 入力はすべて整数である。
  • 2 \leq N \leq 100
  • 1 \leq x \leq 10^9
  • 1 \leq a_i \leq 10^9

入力

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

N x
a_1 a_2 ... a_N

出力

喜ぶ子供の人数の最大値を出力せよ。


入力例 1

3 70
20 30 10

出力例 1

2

例えば、(20, 30, 20) とお菓子を配ればよいです。


入力例 2

3 10
20 30 10

出力例 2

1

(0, 0, 10) とお菓子を配ればよいです。


入力例 3

4 1111
1 10 100 1000

出力例 3

4

(1, 10, 100, 1000) とお菓子を配ればよいです。


入力例 4

2 10
20 20

出力例 4

0

どのようにお菓子を配っても、どの子供も喜びません。

Score : 200 points

Problem Statement

There are N children, numbered 1, 2, ..., N.

Snuke has decided to distribute x sweets among them. He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets. Snuke is trying to maximize the number of happy children by optimally distributing the sweets. Find the maximum possible number of happy children.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 100
  • 1 \leq x \leq 10^9
  • 1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.


Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).


Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).


Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).


Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.

B - Garbage Collector

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

すぬけ君は掃除ロボットを使って部屋を掃除することにしました。

数直線上に N 個のゴミが落ちています。 左から i 番目のゴミは位置 x_i にあります。 これらのゴミを位置 0 にあるゴミ箱に入れたいです。

ゴミの位置に関して、0 < x_1 < x_2 < ... < x_{N} \leq 10^{9} が成立します。

掃除ロボットははじめ位置 0 にいます。ロボットは数直線上を自由に動くことができ、ゴミのある位置にいくとゴミを拾うことができます。 ゴミは同時に何個でも運ぶことでき、ゴミ箱の位置に行くとゴミをゴミ箱に入れることができます。ゴミをゴミ箱以外の場所に置くことは許されません。

ロボットがゴミを拾う、あるいは(1 個以上の)ゴミをゴミ箱に入れるとき X だけエネルギーを消費します。ゴミをゴミ箱に入れるときに消費するエネルギーはゴミの個数にかかわらず X です。 また、ロボットが k 個のゴミを運んでいるとき、距離 1 だけ移動するのに (k+1)^{2} だけエネルギーを消費します。

N 個のゴミを全てゴミ箱に入れるために必要なエネルギーの最小値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^{5}
  • 0 < x_1 < ... < x_N \leq 10^9
  • 1 \leq X \leq 10^9
  • 与えられる入力は全て整数

部分点

  • N \leq 2000 を満たすデータセットに正解した場合、400 点が与えられる。

入力

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

N X
x_1 x_2 ... x_{N}

出力

答えを出力せよ。


入力例 1

2 100
1 10

出力例 1

355
  • 10 のエネルギーを消費して、位置 10 に移動する
  • 100 のエネルギーを消費して、ゴミを取る
  • 36 のエネルギーを消費して、位置 1 に移動する
  • 100 のエネルギーを消費して、ゴミを取る
  • 9 のエネルギーを消費して、位置 0 に移動する
  • 100 のエネルギーを消費して、2 つのゴミをゴミ箱に入れる

このように行動したとき、消費したエネルギーは 10+100+36+100+9+100=355 となります。


入力例 2

5 1
1 999999997 999999998 999999999 1000000000

出力例 2

19999999983

入力例 3

10 8851025
38 87 668 3175 22601 65499 90236 790604 4290609 4894746

出力例 3

150710136

入力例 4

16 10
1 7 12 27 52 75 731 13856 395504 534840 1276551 2356789 9384806 19108104 82684732 535447408

出力例 4

3256017715

Score : 700 points

Problem Statement

Snuke has decided to use a robot to clean his room.

There are N pieces of trash on a number line. The i-th piece from the left is at position x_i. We would like to put all of them in a trash bin at position 0.

For the positions of the pieces of trash, 0 < x_1 < x_2 < ... < x_{N} \leq 10^{9} holds.

The robot is initially at position 0. It can freely move left and right along the number line, pick up a piece of trash when it comes to the position of that piece, carry any number of pieces of trash and put them in the trash bin when it comes to position 0. It is not allowed to put pieces of trash anywhere except in the trash bin.

The robot consumes X points of energy when the robot picks up a piece of trash, or put pieces of trash in the trash bin. (Putting any number of pieces of trash in the trash bin consumes X points of energy.) Also, the robot consumes (k+1)^{2} points of energy to travel by a distance of 1 when the robot is carrying k pieces of trash.

Find the minimum amount of energy required to put all the N pieces of trash in the trash bin.

Constraints

  • 1 \leq N \leq 2 \times 10^{5}
  • 0 < x_1 < ... < x_N \leq 10^9
  • 1 \leq X \leq 10^9
  • All values in input are integers.

Partial Scores

  • 400 points will be awarded for passing the test set satisfying N \leq 2000.

Input

Input is given from Standard Input in the following format:

N X
x_1 x_2 ... x_{N}

Output

Print the answer.


Sample Input 1

2 100
1 10

Sample Output 1

355
  • Travel to position 10 by consuming 10 points of energy.
  • Pick up the piece of trash by consuming 100 points of energy.
  • Travel to position 1 by consuming 36 points of energy.
  • Pick up the piece of trash by consuming 100 points of energy.
  • Travel to position 0 by consuming 9 points of energy.
  • Put the two pieces of trash in the trash bin by consuming 100 points of energy.

This strategy consumes a total of 10+100+36+100+9+100=355 points of energy.


Sample Input 2

5 1
1 999999997 999999998 999999999 1000000000

Sample Output 2

19999999983

Sample Input 3

10 8851025
38 87 668 3175 22601 65499 90236 790604 4290609 4894746

Sample Output 3

150710136

Sample Input 4

16 10
1 7 12 27 52 75 731 13856 395504 534840 1276551 2356789 9384806 19108104 82684732 535447408

Sample Output 4

3256017715
C - ABland Yard

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 900

問題文

N 頂点 M 本の辺からなる無向グラフが与えられます。 頂点には 1 から N の番号が、辺には 1 から M の番号がついています。 頂点には番号以外に AB のラベルがついており、頂点 i には s_i のラベルがついています。

i は頂点 a_ib_i を双方向につなぐ辺です。

怪盗ヌスークは好きな頂点を始点として選び、そこから 0 回以上辺を辿って移動するのが好きです。 今日は移動後に、訪れた頂点についているラベルを始点から訪問した順に並べて文字列を作ることにしました。

例えば、頂点 1 にラベル A が、頂点 2 にラベル B がついているグラフにおいて、ヌスークが 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2 と移動した場合、ABABB が作られます。

怪盗ヌスークが文字 A,B のみからなる任意の文字列が作れるかどうかを判定してください。

制約

  • 2 \leq N \leq 2 \times 10^{5}
  • 1 \leq M \leq 2 \times 10^{5}
  • |s| = N
  • s_iA または B
  • 1 \leq a_i, b_i \leq N
  • 与えられるグラフは単純とも連結とも限らない

入力

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

N M
s
a_1 b_1
:
a_{M} b_{M}

出力

ヌスークが文字 A,B のみからなる任意の文字列が作ることが可能なら Yes を、そうでなければ No を出力せよ。


入力例 1

2 3
AB
1 1
1 2
2 2

出力例 1

Yes
  • ヌスークは頂点 1 と頂点 2 を自由に訪れることができるため、A,B のみからなる任意の文字列が作ることが可能です
77e96cf8e213d606ddd8f3c3f8315d32.png

入力例 2

4 3
ABAB
1 2
2 3
3 1

出力例 2

No
  • 例えば、BB を作ることができません
  • 与えられるグラフは連結とは限りません
1ab1411cb9d6ee023d14ca4e77c4b584.png

入力例 3

13 23
ABAAAABBBBAAB
7 1
10 6
1 11
2 10
2 8
2 11
11 12
8 3
7 12
11 2
13 13
11 9
4 1
9 7
9 6
8 13
8 6
4 10
8 7
4 3
2 1
8 12
6 9

出力例 3

Yes

入力例 4

13 17
BBABBBAABABBA
7 1
7 9
11 12
3 9
11 9
2 1
11 5
12 11
10 8
1 11
1 8
7 7
9 10
8 8
8 12
6 2
13 11

出力例 4

No

Score : 900 points

Problem Statement

You are given an undirected graph consisting of N vertices and M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to M. In addition, each vertex has a label, A or B. The label of Vertex i is s_i. Edge i bidirectionally connects vertex a_i and b_i.

The phantom thief Nusook likes to choose some vertex as the startpoint and traverse an edge zero or more times. Today, he will make a string after traveling as above, by placing the labels of the visited vertices in the order visited, beginning from the startpoint.

For example, in a graph where Vertex 1 has the label A and Vertex 2 has the label B, if Nusook travels along the path 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 2, the resulting string is ABABB.

Determine if Nusook can make all strings consisting of A and B.

Constraints

  • 2 \leq N \leq 2 \times 10^{5}
  • 1 \leq M \leq 2 \times 10^{5}
  • |s| = N
  • s_i is A or B.
  • 1 \leq a_i, b_i \leq N
  • The given graph may NOT be simple or connected.

Input

Input is given from Standard Input in the following format:

N M
s
a_1 b_1
:
a_{M} b_{M}

Output

If Nusook can make all strings consisting of A and B, print Yes; otherwise, print No.


Sample Input 1

2 3
AB
1 1
1 2
2 2

Sample Output 1

Yes
  • Since Nusook can visit Vertex 1 and Vertex 2 in any way he likes, he can make all strings consisting of A and B.
77e96cf8e213d606ddd8f3c3f8315d32.png

Sample Input 2

4 3
ABAB
1 2
2 3
3 1

Sample Output 2

No
  • For example, Nusook cannot make BB.
  • The given graph may not be connected.
1ab1411cb9d6ee023d14ca4e77c4b584.png

Sample Input 3

13 23
ABAAAABBBBAAB
7 1
10 6
1 11
2 10
2 8
2 11
11 12
8 3
7 12
11 2
13 13
11 9
4 1
9 7
9 6
8 13
8 6
4 10
8 7
4 3
2 1
8 12
6 9

Sample Output 3

Yes

Sample Input 4

13 17
BBABBBAABABBA
7 1
7 9
11 12
3 9
11 9
2 1
11 5
12 11
10 8
1 11
1 8
7 7
9 10
8 8
8 12
6 2
13 11

Sample Output 4

No
D - Modulo Matrix

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1100

問題文

整数 N が与えられます。

以下の条件を満たすような N \times N 行列 a をどれか 1 つ構成してください。この問題の制約下で、必ず解が存在することが証明できます。

  • 1 \leq a_{i,j} \leq 10^{15}
  • a_{i,j} は相異なる整数である
  • ある正の整数 m が存在して、上下左右に隣接する 2 つの数 x,y をどこから取り出しても、{\rm max}(x,y){\rm min}(x,y) で割ったあまりは m となる

制約

  • 2 \leq N \leq 500

入力

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

N

出力

答えを以下の形式で出力せよ。

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

入力例 1

2

出力例 1

4 7
23 10
  • どの隣接した 2 つの数についても、大きい方の数を小さい数で割ったあまりが 3 となっています

Score : 1100 points

Problem Statement

You are given an integer N.

Construct any one N-by-N matrix a that satisfies the conditions below. It can be proved that a solution always exists under the constraints of this problem.

  • 1 \leq a_{i,j} \leq 10^{15}
  • a_{i,j} are pairwise distinct integers.
  • There exists a positive integer m such that the following holds: Let x and y be two elements of the matrix that are vertically or horizontally adjacent. Then, {\rm max}(x,y) {\rm mod} {\rm min}(x,y) is always m.

Constraints

  • 2 \leq N \leq 500

Input

Input is given from Standard Input in the following format:

N

Output

Print your solution in the following format:

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Sample Input 1

2

Sample Output 1

4 7
23 10
  • For any two elements x and y that are vertically or horizontally adjacent, {\rm max}(x,y) {\rm mod} {\rm min}(x,y) is always 3.
E - ABBreviate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1300

問題文

ab のみからなる文字列 s があります。 すぬけ君は、次の 2 種類の操作を任意の順序で任意の回数だけ行えます。

  • s 中の部分文字列 aa を一箇所選び、b へ置き換える。
  • s 中の部分文字列 bb を一箇所選び、a へ置き換える。

0 回以上の操作の後、s は何通りありうるでしょうか? 10^9 + 7 で割った余りを求めてください。

制約

  • 1 \leq |s| \leq 10^5
  • sab のみからなる。

入力

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

s

出力

操作後の s は何通りありうるか? 10^9 + 7 で割った余りを出力せよ。


入力例 1

aaaa

出力例 1

6

次の 6 通りです。

  • aaaa
  • aab
  • aba
  • baa
  • bb
  • a

入力例 2

aabb

出力例 2

5

次の 5 通りです。

  • aabb
  • aaa
  • bbb
  • ab
  • ba

入力例 3

ababababa

出力例 3

1

すぬけ君は一度も操作を行えません。


入力例 4

babbabaaba

出力例 4

35

Score : 1300 points

Problem Statement

There is a string s consisting of a and b. Snuke can perform the following two kinds of operation any number of times in any order:

  • Choose an occurrence of aa as a substring, and replace it with b.
  • Choose an occurrence of bb as a substring, and replace it with a.

How many strings s can be obtained by this sequence of operations? Find the count modulo 10^9 + 7.

Constraints

  • 1 \leq |s| \leq 10^5
  • s consists of a and b.

Input

Input is given from Standard Input in the following format:

s

Output

Print the number of strings s that can be obtained, modulo 10^9 + 7.


Sample Input 1

aaaa

Sample Output 1

6

Six strings can be obtained:

  • aaaa
  • aab
  • aba
  • baa
  • bb
  • a

Sample Input 2

aabb

Sample Output 2

5

Five strings can be obtained:

  • aabb
  • aaa
  • bbb
  • ab
  • ba

Sample Input 3

ababababa

Sample Output 3

1

Snuke cannot perform any operation.


Sample Input 4

babbabaaba

Sample Output 4

35
F - Grafting

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 1900

問題文

すぬけ君は頂点に 1 から N の番号がついた N 頂点の木 A,B を見つけました。 Ai 番目の辺は頂点 a_ib_i をつないでいます。 Bj 番目の辺は頂点 c_jd_j をつないでいます。 全ての頂点ははじめ、白で塗られています。

すぬけ君は以下の操作を A0 回以上行い、B と一致するようにしたいです。

  • 白で塗られた1 つ選ぶ(これを v とする)
  • v に接続された辺を取り除き、v と別の頂点をつなぐ新たな辺を追加する
  • v を黒く塗る

AB と一致させることが可能かどうかを判定し、可能ならば必要な操作回数の最小値を求めてください。一致判定において、頂点の色は考慮しません。

T 個のケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \leq T \leq 20
  • 3 \leq N \leq 50
  • 1 \leq a_i,b_i,c_i,d_i \leq N
  • 与えられるグラフはいずれも木

入力

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

T
case_1
:
case_{T}

各ケースは以下の形式で与えられる。

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}

出力

各ケースについて、AB と一致させることが可能ならば必要な操作回数の最小値を、不可能ならば、-1 を出力せよ。


入力例 1

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

出力例 1

1
-1
  • それぞれのケースでは、以下の図のようなグラフが与えられます
  • ケース 1 では頂点 1 を選び、12 をつなぐ辺を取り除いて 13 をつなぐ辺を追加することで AB を一致させることが可能です。頂点 2 は葉ではないため、選ぶことができないことに注意してください
3f020b4a4e883680357cc55adb571fbc.png

入力例 2

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

出力例 2

6
0
7
  • AB が一致していることもあります

Score : 1900 points

Problem Statement

Snuke has found two trees A and B, each with N vertices numbered 1 to N. The i-th edge of A connects Vertex a_i and b_i, and the j-th edge of B connects Vertex c_j and d_j. Also, all vertices of A are initially painted white.

Snuke would like to perform the following operation on A zero or more times so that A coincides with B:

  • Choose a leaf vertex that is painted white. (Let this vertex be v.)
  • Remove the edge incident to v, and add a new edge that connects v to another vertex.
  • Paint v black.

Determine if A can be made to coincide with B, ignoring color. If the answer is yes, find the minimum number of operations required.

You are given T cases of this kind. Find the answer for each of them.

Constraints

  • 1 \leq T \leq 20
  • 3 \leq N \leq 50
  • 1 \leq a_i,b_i,c_i,d_i \leq N
  • All given graphs are trees.

Input

Input is given from Standard Input in the following format:

T
case_1
:
case_{T}

Each case is given in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}

Output

For each case, if A can be made to coincide with B ignoring color, print the minimum number of operations required, and print -1 if it cannot.


Sample Input 1

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

Sample Output 1

1
-1
  • The graph in each case is shown below.
  • In case 1, A can be made to coincide with B by choosing Vertex 1, removing the edge connecting 1 and 2, and adding an edge connecting 1 and 3. Note that Vertex 2 is not a leaf vertex and thus cannot be chosen.
3f020b4a4e883680357cc55adb571fbc.png

Sample Input 2

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

Sample Output 2

6
0
7
  • A may coincide with B from the beginning.