A - Fourtune Cookies

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

すぬけ君は美味しさが A, B, C, D であるような 4 枚のクッキーを持っています。 すぬけ君は 1 枚以上のクッキーを選んで食べます。食べるクッキーの美味しさの総和と、残るクッキーの美味しさの総和が等しくなることはありますか?

制約

  • 与えられる入力は全て整数
  • 1 \leq A,B,C,D \leq 10^{8}

入力

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

A B C D

出力

すぬけ君が食べるクッキーの美味しさの総和と、残るクッキーの美味しさの総和が等しくなることがあるなら Yes、そうでなければ No を出力せよ。


入力例 1

1 3 2 4

出力例 1

Yes
  • すぬけ君が美味しさ 1,4 のクッキーを食べるとき、食べるクッキーの美味しさの総和と、残るクッキーの美味しさの総和が等しくなります。

入力例 2

1 2 4 8

出力例 2

No
  • どのようにしても食べるクッキーの美味しさの総和と、残るクッキーの美味しさの総和が等しくなることはありません。

Score : 200 points

Problem Statement

Snuke has four cookies with deliciousness A, B, C, and D. He will choose one or more from those cookies and eat them. Is it possible that the sum of the deliciousness of the eaten cookies is equal to that of the remaining cookies?

Constraints

  • All values in input are integers.
  • 1 \leq A,B,C,D \leq 10^{8}

Input

Input is given from Standard Input in the following format:

A B C D

Output

If it is possible that the sum of the deliciousness of the eaten cookies is equal to that of the remaining cookies, print Yes; otherwise, print No.


Sample Input 1

1 3 2 4

Sample Output 1

Yes
  • If he eats the cookies with deliciousness 1 and 4, the sum of the deliciousness of the eaten cookies will be equal to that of the remaining cookies.

Sample Input 2

1 2 4 8

Sample Output 2

No
  • Whatever choice he makes, the sum of the deliciousness of the eaten cookies will never be equal to that of the remaining cookies.
B - MAX-=min

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

すぬけ君は 1 から N の番号がついた N 枚のカードを持っています。 それぞれのカードには整数が書かれており、カード i には a_i が書かれています。

すぬけ君は以下の手続きを行います。

  1. すぬけ君が持っているカードに書かれた数の最大値を X、最小値を x とする。
  2. X = x なら手続きを終了する。そうでなければ X が書かれたカードを全て X-x が書かれたカードに変え、1 へ戻る。

この問題の制約下で、いずれ手続きが終了することが証明できます。手続き終了後のすぬけ君が持っているカードに書かれた唯一の数を求めてください。

制約

  • 与えられる入力は全て整数
  • 1 \leq N \leq 10^{5}
  • 1 \leq a_i \leq 10^9

入力

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

N
a_1 a_2 \cdots a_N

出力

手続き終了後のすぬけ君が持っているカードに書かれた唯一の数を出力せよ。


入力例 1

3
2 6 6

出力例 1

2
  • 手続き開始時点では、すぬけ君が持っているカードに書かれた数は (2,6,6) です。
    • x=2,X=6 なので、6 と書かれたカードを全て 4 が書かれたカードに書き換えます。
  • すぬけ君が持っているカードに書かれた数は (2,4,4) になっています。
    • x=2,X=4 なので、4 と書かれたカードを全て 2 が書かれたカードに書き換えます。
  • すぬけ君が持っているカードに書かれた数は (2,2,2) になっています。
    • x=2,X=2 なので手続きを終了します。

入力例 2

15
546 3192 1932 630 2100 4116 3906 3234 1302 1806 3528 3780 252 1008 588

出力例 2

42

Score : 300 points

Problem Statement

Snuke had N cards numbered 1 through N. Each card has an integer written on it; written on Card i is a_i.

Snuke did the following procedure:

  1. Let X and x be the maximum and minimum values written on Snuke's cards, respectively.
  2. If X = x, terminate the procedure. Otherwise, replace each card on which X is written with a card on which X-x is written, then go back to step 1.

Under the constraints in this problem, it can be proved that the procedure will eventually terminate. Find the number written on all of Snuke's cards after the procedure.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 10^{5}
  • 1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \cdots a_N

Output

Print the number written on all of Snuke's cards after the procedure.


Sample Input 1

3
2 6 6

Sample Output 1

2
  • At the beginning of the procedure, the numbers written on Snuke's cards are (2,6,6).
    • Since x=2 and X=6, he replaces each card on which 6 is written with a card on which 4 is written.
  • Now, the numbers written on Snuke's cards are (2,4,4).
    • Since x=2 and X=4, he replaces each card on which 4 is written with a card on which 2 is written.
  • Now, the numbers written on Snuke's cards are (2,2,2).
    • Since x=2 and X=2, he terminates the procedure.

Sample Input 2

15
546 3192 1932 630 2100 4116 3906 3234 1302 1806 3528 3780 252 1008 588

Sample Output 2

42
C - Camels and Bridge

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1 から N の番号がついた N 頭のラクダがいます。

ラクダ i の体重は w_i です。

あなたはラクダたちに隊列を組ませ、M 個のパーツからなる橋を渡らせようとしています。

あなたは橋を渡る前にラクダたちの隊列を決め(番号の昇順となる必要はありません)、ラクダどうしを任意の非負の実数の間隔で並ばせることができます。 ラクダたちはこの決められた間隔を保って橋を渡ります。

橋の i 番目のパーツは長さ l_i で耐荷重は v_i です。 パーツ内部(両端を除く)にいるラクダたちの体重の総和が v_i より大きくなると、橋は崩落してしまいます。

橋が崩落しないようにラクダたちを渡らせることが可能かどうかを判定し、可能ならばそのときの先頭のラクダと末尾のラクダの距離としてありうる値の最小値を求めてください。 これは整数になることが証明できるので、整数で出力してください。

制約

  • 与えられる入力は全て整数
  • 2 \leq N \leq 8
  • 1 \leq M \leq 10^5
  • 1 \leq w_i,l_i,v_i \leq 10^8

入力

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

N M
w_1 w_2 \cdots w_N
l_1 v_1
\vdots
l_M v_M

出力

どのようにしても橋が崩落してしまう場合、-1 を出力せよ。 そうでない場合、橋が崩落しないようにラクダたちを渡らせるときの先頭のラクダと末尾のラクダの距離としてありうる値の最小値を出力せよ。


入力例 1

3 2
1 4 2
10 4
2 6

出力例 1

10
  • 例えば、先頭から順に 1,3,2 の順番に並べ、ラクダどうしの間隔をそれぞれ 0,10 にすることで橋が崩落しないようにラクダたちを渡らせることが可能です。
    • パーツ 1 ではラクダ 1,3 あるいはラクダ 2 のみがパーツの内部にいる状態が起こります。どちらもパーツの内部にいるラクダたちの体重の総和がパーツ 1 の耐荷重である 4 以下のため、橋が崩落することはありません。
    • パーツ 2 ではラクダ 1,3 あるいはラクダ 2 のみがパーツの内部にいる状態が起こります。どちらもパーツの内部にいるラクダたちの体重の総和がパーツ 1 の耐荷重である 6 以下のため、橋が崩落することはありません。
  • ラクダどうしの間隔が 0 でもよいこと、パーツの内部はパーツの両端を含まないことに注意してください。

入力例 2

2 1
12 345
1 1

出力例 2

-1
  • どのようにしても橋が崩落してしまう場合は -1 を出力してください。

入力例 3

8 1
1 1 1 1 1 1 1 1
100000000 1

出力例 3

700000000

入力例 4

8 20
57 806 244 349 608 849 513 857
778 993
939 864
152 984
308 975
46 860
123 956
21 950
850 876
441 899
249 949
387 918
34 965
536 900
875 889
264 886
583 919
88 954
845 869
208 963
511 975

出力例 4

3802

Score : 500 points

Problem Statement

There are N camels numbered 1 through N.

The weight of Camel i is w_i.

You will arrange the camels in a line and make them cross a bridge consisting of M parts.

Before they cross the bridge, you can choose their order in the line - it does not have to be Camel 1, 2, \ldots, N from front to back - and specify the distance between each adjacent pair of camels to be any non-negative real number. The camels will keep the specified distances between them while crossing the bridge.

The i-th part of the bridge has length l_i and weight capacity v_i. If the sum of the weights of camels inside a part (excluding the endpoints) exceeds v_i, the bridge will collapse.

Determine whether it is possible to make the camels cross the bridge without it collapsing. If it is possible, find the minimum possible distance between the first and last camels in the line in such a case.

It can be proved that the answer is always an integer, so print an integer.

Constraints

  • All values in input are integers.
  • 2 \leq N \leq 8
  • 1 \leq M \leq 10^5
  • 1 \leq w_i,l_i,v_i \leq 10^8

Input

Input is given from Standard Input in the following format:

N M
w_1 w_2 \cdots w_N
l_1 v_1
\vdots
l_M v_M

Output

If the bridge will unavoidably collapse when the camels cross the bridge, print -1. Otherwise, print the minimum possible distance between the first and last camels in the line when the camels cross the bridge without it collapsing.


Sample Input 1

3 2
1 4 2
10 4
2 6

Sample Output 1

10
  • It is possible to make the camels cross the bridge without it collapsing by, for example, arranging them in the order 1, 3, 2 from front to back, and setting the distances between them to be 0, 10.
    • For Part 1 of the bridge, there are moments when only Camel 1 and 3 are inside the part and moments when only Camel 2 is inside the part. In both cases, the sum of the weights of camels does not exceed 4 - the weight capacity of Part 1 - so there is no collapse.
    • For Part 2 of the bridge, there are moments when only Camel 1 and 3 are inside the part and moments when only Camel 2 is inside the part. In both cases, the sum of the weights of camels does not exceed 6 - the weight capacity of Part 2 - so there is no collapse.
  • Note that the distance between two camels may be 0 and that camels on endpoints of a part are not considered to be inside the part.

Sample Input 2

2 1
12 345
1 1

Sample Output 2

-1
  • Print -1 if the bridge will unavoidably collapse.

Sample Input 3

8 1
1 1 1 1 1 1 1 1
100000000 1

Sample Output 3

700000000

Sample Input 4

8 20
57 806 244 349 608 849 513 857
778 993
939 864
152 984
308 975
46 860
123 956
21 950
850 876
441 899
249 949
387 918
34 965
536 900
875 889
264 886
583 919
88 954
845 869
208 963
511 975

Sample Output 4

3802
D - Let's Play Nim

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

1 から N の番号がついた N 枚の袋と、1 から N の番号がついた N 枚の皿があります。 袋 i には a_i 個のコインが入っています。どの皿もはじめは何も乗っていません。

先手太郎君と後手次郎君が対戦ゲームをします。 先手太郎君と後手次郎君の手番が交互に訪れます。先手太郎君が先手です。 それぞれのプレイヤーは、手番において以下の 2 つの手のどちらかを打つことが可能です。

  1. (コインが入った袋が 1 つ以上存在するとき):コインが入った袋と皿を 1 枚ずつ選び、選んだ袋の中に入った全てのコインを選んだ皿に移す(選ぶ皿にはコインが乗っていてもいなくても構わない)
  2. (コインが入った袋が存在しないとき):コインが乗った皿を 1 枚選び、選んだ皿から 1 枚以上のコインを取り除く

先に手が打てなくなった人の負けです。2 人が最適に行動したときに勝つのはどちらかを判定してください。

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

制約

  • 与えられる入力は全て整数
  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^{5}
  • 1 \leq a_i \leq 10^9
  • 1 つの入力ファイルにおいて、N の総和は 2 \times 10^5 を超えない。

入力

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

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

N
a_1 a_2 \cdots a_N

出力

T 行出力せよ。i 行目には i 番目のテストケースの勝者が先手太郎君ならば First、後手次郎君ならば Second を出力せよ。


入力例 1

3
1
10
2
1 2
21
476523737 103976339 266993 706803678 802362985 892644371 953855359 196462821 817301757 409460796 773943961 488763959 405483423 616934516 710762957 239829390 55474813 818352359 312280585 185800870 255245162

出力例 1

Second
First
Second
  • テストケース 1 では後手次郎君が勝利します。以下はそのような 2 人の行動の例です。
    • 先手太郎君の手番では、袋 1 を選んで皿 1 にコインを移すしかできません。
    • 後手次郎君の手番で皿 1 を選んで全てのコインを取り除くことで、先手太郎君は手番で手を打つことができず敗北します。
  • コインが入った袋が存在するとき、コインの入った袋を選んで皿に移す手しか打てないことに注意してください。
  • 同様に、コインが入った袋が存在しないときは皿を選んでコインを 1 つ以上取り除く手しか打てないことに注意してください。

Score : 600 points

Problem Statement

We have N bags numbered 1 through N and N dishes numbered 1 through N. Bag i contains a_i coins, and each dish has nothing on it initially.

Taro the first and Jiro the second will play a game against each other. They will alternately take turns, with Taro the first going first. In each player's turn, the player can make one of the following two moves:

  1. When one or more bags contain coin(s): Choose one bag that contains coin(s) and one dish, then move all coins in the chosen bag onto the chosen dish. (The chosen dish may already have coins on it, or not.)
  2. When no bag contains coins: Choose one dish with coin(s) on it, then remove one or more coins from the chosen dish.

The player who first becomes unable to make a move loses. Determine the winner of the game when the two players play optimally.

You are given T test cases. Solve each of them.

Constraints

  • All values in input are integers.
  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 10^{5}
  • 1 \leq a_i \leq 10^9
  • In one input file, the sum of N does not exceed 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is in the following format:

N
a_1 a_2 \cdots a_N

Output

Print T lines. The i-th line should contain First if Taro the first wins in the i-th test case, and Second if Jiro the second wins in the test case.


Sample Input 1

3
1
10
2
1 2
21
476523737 103976339 266993 706803678 802362985 892644371 953855359 196462821 817301757 409460796 773943961 488763959 405483423 616934516 710762957 239829390 55474813 818352359 312280585 185800870 255245162

Sample Output 1

Second
First
Second
  • In test case 1, Jiro the second wins. Below is one sequence of moves that results in Jiro's win:
    • In Taro the first's turn, he can only choose Bag 1 and move the coins onto Dish 1.
    • In Jiro the second's turn, he can choose Dish 1 and remove all coins from it, making Taro fail to make a move and lose.
  • Note that when there is a bag that contains coin(s), a player can only make a move in which he chooses a bag that contains coin(s) and moves the coin(s) onto a dish.
  • Similarly, note that when there is no bag that contains coin(s), a player can only make a move in which he chooses a dish and removes one or more coins.
E - Keep Graph Disconnected

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700

問題文

1 から N の番号がついた N 個の頂点と、1 から M の番号がついた M 本の辺からなる無向グラフ G が与えられます。 辺 i は頂点 a_i と頂点 b_i を双方向につないでいます。

G が以下の条件の両方を満たすとき、Gよいグラフ であるといいます。はじめ、G はよいグラフであることが保証されます。

  • 頂点 1N が非連結
  • 自己ループや多重辺が存在しない

先手太郎君と後手次郎君が対戦ゲームをします。 先手太郎君と後手次郎君の手番が交互に訪れます。先手太郎君が先手です。 それぞれのプレイヤーは、手番において以下の操作が可能です。

操作:頂点 u,v を選んで uv を双方向につなぐ辺を G に追加する。

辺を追加した結果、G がよいグラフでなくなった人の負けです。2 人が最適に行動したときに勝つのはどちらかを判定してください。

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

制約

  • 与えられる入力は全て整数
  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 10^{5}
  • 0 \leq M \leq \min(N(N-1)/2,10^{5})
  • 1 \leq a_i,b_i \leq N
  • 与えられるグラフはよいグラフ
  • 1 つの入力ファイルにおいて、N の総和、M の総和はどちらも 2 \times 10^5 を超えない。

入力

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

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

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

N M
a_1 b_1
\vdots
a_M b_M

出力

T 行出力せよ。i 行目には i 番目のテストケースの勝者が先手太郎君ならば First、後手次郎君ならば Second を出力せよ。


入力例 1

3
3 0
6 2
1 2
2 3
15 10
12 14
8 3
10 1
14 6
12 6
1 9
13 1
2 5
3 9
7 2

出力例 1

First
Second
First
  • テストケース 1 では先手太郎君が勝利します。以下はそのような 2 人の行動の例です。
    • 先手太郎君の手番で頂点 1,2 をつなぐ辺を追加する。辺を追加したあとのグラフもよいグラフです。
    • 後手次郎君はどの 2 つの頂点の間に辺を追加したとしても、グラフがよいグラフではなくなってしまいます。
    • よって、勝者は先手太郎君です。

Score : 700 points

Problem Statement

Given is an undirected graph G consisting of N vertices numbered 1 through N and M edges numbered 1 through M. Edge i connects Vertex a_i and Vertex b_i bidirectionally.

G is said to be a good graph when both of the conditions below are satisfied. It is guaranteed that G is initially a good graph.

  • Vertex 1 and Vertex N are not connected.
  • There are no self-loops and no multi-edges.

Taro the first and Jiro the second will play a game against each other. They will alternately take turns, with Taro the first going first. In each player's turn, the player can do the following operation:

  • Operation: Choose vertices u and v, then add to G an edge connecting u and v bidirectionally.

The player whose addition of an edge results in G being no longer a good graph loses. Determine the winner of the game when the two players play optimally.

You are given T test cases. Solve each of them.

Constraints

  • All values in input are integers.
  • 1 \leq T \leq 10^5
  • 2 \leq N \leq 10^{5}
  • 0 \leq M \leq \min(N(N-1)/2,10^{5})
  • 1 \leq a_i,b_i \leq N
  • The given graph is a good graph.
  • In one input file, the sum of N and that of M do not exceed 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

T
\mathrm{case}_1
\vdots
\mathrm{case}_T

Each case is in the following format:

N M
a_1 b_1
\vdots
a_M b_M

Output

Print T lines. The i-th line should contain First if Taro the first wins in the i-th test case, and Second if Jiro the second wins in the test case.


Sample Input 1

3
3 0
6 2
1 2
2 3
15 10
12 14
8 3
10 1
14 6
12 6
1 9
13 1
2 5
3 9
7 2

Sample Output 1

First
Second
First
  • In test case 1, Taro the first wins. Below is one sequence of moves that results in Taro's win:
    • In Taro the first's turn, he adds an edge connecting Vertex 1 and 2, after which the graph is still good.
    • Then, whichever two vertices Jiro the second would choose to connect with an edge, the graph would no longer be good.
    • Thus, Taro wins.
F - Lights Out on Connected Graph

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 900

問題文

1 から N の番号がついた N 個の頂点と、1 から M の番号がついた M 本の辺からなる無向グラフ G が与えられます。G は連結で、自己ループや多重辺が存在しないことが保証されます。 辺 i は頂点 a_i と頂点 b_i を双方向につなぐ辺です。 それぞれの辺は赤か青のどちらかの色で塗ることができます。はじめ、全ての辺は赤で塗られています。

G から 0 本以上の辺を取り除き新しいグラフ G^{\prime} を作ることを考えます。 G^{\prime} としてありうるグラフは 2^M 通りありますが、これらのうちよいグラフ(後述)であるようなものの個数を 998244353 で割ったあまりを求めてください。

G^{\prime} が以下の条件の両方を満たすとき、G^{\prime}よいグラフ であるといいます。

  • G^{\prime} は連結
  • 以下の操作を 0 回以上繰り返すことで、全ての辺の色を青色にできる
    • 頂点を 1 つ選び、その頂点に接続する全ての辺の色を変化させる。すなわち、辺の色が赤ならば青へ、青ならば赤へ変化させる。

制約

  • 与えられる入力は全て整数
  • 1 \leq N \leq 17
  • N-1 \leq M \leq N(N-1)/2
  • G は連結で、自己ループや多重辺が存在しない

入力

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

N M
a_1 b_1
\vdots
a_M b_M

出力

G^{\prime} としてありうるグラフのうち、よいグラフであるものの個数を 998244353 で割ったあまりを出力せよ。


入力例 1

3 2
1 2
2 3

出力例 1

1
  • 1、辺 2 のどちらも取り除かない場合のみ条件を満たします。
    • 例えば、頂点 2 に対して操作を行うことで、全ての辺を青色にすることが可能です。
  • それ以外の場合はグラフが非連結になるため、条件を満たしません。

入力例 2

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

出力例 2

19

入力例 3

17 50
16 17
10 9
16 10
5 17
6 15
5 9
15 11
16 1
8 13
6 17
15 3
16 15
11 3
7 6
1 4
11 13
10 6
10 12
3 16
7 3
16 5
13 3
12 13
7 11
3 12
13 10
1 12
9 15
11 14
4 6
13 2
6 1
15 2
1 14
15 17
2 11
14 13
16 9
16 8
8 17
17 12
1 11
6 12
17 2
8 1
14 6
9 7
11 10
5 14
17 7

出力例 3

90625632
  • 998244353 で割ったあまりを求めるのを忘れずに。

Score : 900 points

Problem Statement

Given is an undirected graph G consisting of N vertices numbered 1 through N and M edges numbered 1 through M. It is guaranteed that G is connected and contains no self-loops and no multi-edges. Edge i connects Vertex a_i and Vertex b_i bidirectionally. Each edge can be painted red or blue. Initially, every edge is painted red.

Consider making a new graph G^{\prime} by removing zero or more edges from G. There are 2^M graphs that G^{\prime} can be. Among them, find the number of good graphs defined below, modulo 998244353.

G^{\prime} is said to be a good graph when both of the following conditions are satisfied:

  • G^{\prime} is connected.
  • It is possible to make all edges blue by doing the following operation zero or more times.
    • Choose one vertex and change the color of every edge incident to that vertex, that is, red to blue and vice versa.

Constraints

  • All values in input are integers.
  • 1 \leq N \leq 17
  • N-1 \leq M \leq N(N-1)/2
  • G is connected and has no self-loops and no multi-edges.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
\vdots
a_M b_M

Output

Print the number of good graphs among the ones that G^{\prime} can be, modulo 998244353.


Sample Input 1

3 2
1 2
2 3

Sample Output 1

1
  • The conditions are satisfied only if neither Edge 1 nor Edge 2 is removed.
    • In this case, we can make all edges blue by, for example, doing the operation for Vertex 2.
  • Otherwise, the graph gets disconnected and thus does not satisfy the condition.

Sample Input 2

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

Sample Output 2

19

Sample Input 3

17 50
16 17
10 9
16 10
5 17
6 15
5 9
15 11
16 1
8 13
6 17
15 3
16 15
11 3
7 6
1 4
11 13
10 6
10 12
3 16
7 3
16 5
13 3
12 13
7 11
3 12
13 10
1 12
9 15
11 14
4 6
13 2
6 1
15 2
1 14
15 17
2 11
14 13
16 9
16 8
8 17
17 12
1 11
6 12
17 2
8 1
14 6
9 7
11 10
5 14
17 7

Sample Output 3

90625632
  • Be sure to find the count modulo 998244353.