A - Xor Battle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

2 人の人がおり,01 の番号がついています. また,0 で初期化された変数 x があります. これから 2 人はゲームを行います. ゲームは N ラウンドからなり,i 回目 (1 \leq i \leq N) のラウンドでは,次の操作が行われます.

  • S_i が以下のいずれかの操作をする.
    • xx \oplus A_i で置き換える.ただしここで \oplus はbitごとの排他的論理和を表す.
    • 何もしない.

0 の目標は,最終的に x=0 にすることで,逆に人 1 の目標は,最終的に x \neq 0 にすることです.

2 人が最適に行動する時,最終的に x0 になるかどうかを判定してください.

1 つの入力につき,T 個のテストケースに答えてください.

制約

  • 1 \leq T \leq 100
  • 1 \leq N \leq 200
  • 1 \leq A_i \leq 10^{18}
  • S01 のみからなる長さ N の文字列
  • 入力される数は全て整数である.

入力

入力は以下の形式で標準入力から与えられる. 入力の 1 行目は以下のとおりである.

T

そして,T 個のテストケースが続く. これらはそれぞれ以下の形式で与えられる.

N
A_1 A_2 \cdots A_N
S

出力

各テストケースについて,最終的に x=0 となる場合は 0,そうでない場合は 1 と出力せよ. 各テストケースごとに改行せよ.


入力例 1

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

出力例 1

1
0
0

1 つ目のテストケースでは,人 1x0 \oplus 1=1 で置き換えると,人 0 の操作に依らず,最終的に x \neq 0 になります.

2 つ目のテストケースでは,人 1 がどちらの操作を行っても,人 0 が適切な操作をすれば x=0 にできます.

Score : 400 points

Problem Statement

There are two persons, numbered 0 and 1, and a variable x whose initial value is 0. The two persons now play a game. The game is played in N rounds. The following should be done in the i-th round (1 \leq i \leq N):

  • Person S_i does one of the following:
    • Replace x with x \oplus A_i, where \oplus represents bitwise XOR.
    • Do nothing.

Person 0 aims to have x=0 at the end of the game, while Person 1 aims to have x \neq 0 at the end of the game.

Determine whether x becomes 0 at the end of the game when the two persons play optimally.

Solve T test cases for each input file.

Constraints

  • 1 \leq T \leq 100
  • 1 \leq N \leq 200
  • 1 \leq A_i \leq 10^{18}
  • S is a string of length N consisting of 0 and 1.
  • All numbers in input are integers.

Input

Input is given from Standard Input in the following format. The first line is as follows:

T

Then, T test cases follow. Each test case is given in the following format:

N
A_1 A_2 \cdots A_N
S

Output

For each test case, print a line containing 0 if x becomes 0 at the end of the game, and 1 otherwise.


Sample Input 1

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

Sample Output 1

1
0
0

In the first test case, if Person 1 replaces x with 0 \oplus 1=1, we surely have x \neq 0 at the end of the game, regardless of the choice of Person 0.

In the second test case, regardless of the choice of Person 1, Person 0 can make x=0 with a suitable choice.

B - 01 Unbalanced

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

文字列 S が与えられます. S の各文字は,0,1,? のいずれかです.

S に含まれる全ての ?01 に変えて(? ごとに変換後の文字を選択できます),文字列 S' を作ることを考えます. ここで,S' のアンバランス度を,次のように定義します.

  • S' のアンバランス度 = \max \{ S'l 文字目から r 文字目までに含まれる 0 の個数と 1 の個数の差の絶対値 :\ 1 \leq l \leq r \leq |S|\}

S' のアンバランス度としてありうる最小の値を求めてください.

制約

  • 1 \leq |S| \leq 10^6
  • S の各文字は 0,1,? のいずれかである.

入力

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

S

出力

S' のアンバランス度としてありうる最小の値を出力せよ.


入力例 1

0??

出力例 1

1

S'=010 とすれば,アンバランス度は 1 になり,これが最小です.


入力例 2

0??0

出力例 2

2

入力例 3

??00????0??0????0?0??00??1???11?1?1???1?11?111???1

出力例 3

4

Score : 800 points

Problem Statement

Given is a string S, where each character is 0, 1, or ?.

Consider making a string S' by replacing each occurrence of ? with 0 or 1 (we can choose the character for each ? independently). Let us define the unbalancedness of S' as follows:

  • (The unbalancedness of S') = \max \{ The absolute difference between the number of occurrences of 0 and 1 between the l-th and r-th character of S (inclusive) :\ 1 \leq l \leq r \leq |S|\}

Find the minimum possible unbalancedness of S'.

Constraints

  • 1 \leq |S| \leq 10^6
  • Each character of S is 0, 1, or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible unbalancedness of S'.


Sample Input 1

0??

Sample Output 1

1

We can make S'= 010 and achieve the minimum possible unbalancedness of 1.


Sample Input 2

0??0

Sample Output 2

2

Sample Input 3

??00????0??0????0?0??00??1???11?1?1???1?11?111???1

Sample Output 3

4
C - Range Set

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 800

問題文

すぬけくんは長さ N の文字列 x を持っています. 最初,x のすべての文字は 0 です.

すぬけくんは,以下の 2 種類の操作を好きな順序で好きな回数行うことができます.

  • x の連続する A 文字を選んで,それらをすべて 0 にする.
  • x の連続する B 文字を選んで,それらをすべて 1 にする.

すぬけくんが操作を終えたあとの x としてありうるものが何通りあるかを求めてください. ただし答えは非常に大きくなることがあるので.10^9+7 で割ったあまりを求めてください.

制約

  • 1 \leq N \leq 5000
  • 1 \leq A,B \leq N
  • 入力される値はすべて整数である.

入力

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

N A B

出力

すぬけくんが操作を終えたあとの x としてありうるものが何通りあるかを 10^9+7 で割ったあまりを出力せよ.


入力例 1

4 2 3

出力例 1

11

例えば,x=0011,1111 などはありえますが,x=0110 はありえません.


入力例 2

10 7 2

出力例 2

533

入力例 3

1000 100 10

出力例 3

828178524

Score : 800 points

Problem Statement

Snuke has a string x of length N. Initially, every character in x is 0.

Snuke can do the following two operations any number of times in any order:

  • Choose A consecutive characters in x and replace each of them with 0.
  • Choose B consecutive characters in x and replace each of them with 1.

Find the number of different strings that x can be after Snuke finishes doing operations. This count can be enormous, so compute it modulo (10^9+7).

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq A,B \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of different strings that x can be after Snuke finishes doing operations, modulo (10^9+7).


Sample Input 1

4 2 3

Sample Output 1

11

For example, x can be 0011 or 1111 in the end, but cannot be 0110.


Sample Input 2

10 7 2

Sample Output 2

533

Sample Input 3

1000 100 10

Sample Output 3

828178524
D - Lamps and Buttons

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 1200

問題文

1 から N までの番号のついた N 個のランプと,1 から N までの番号のついた N 個のボタンがあります. 最初,ランプ 1,2,\cdots,A は点灯しており,それ以外のランプは点灯していません.

すぬけくんとりんごさんは,次のようなゲームを行うことにしました.

  • まず,りんごさんが (1,2,\cdots,N) の順列 (p_1,p_2,\cdots,p_N) を生成する. この順列は N! 通りの中から一様ランダムに選ばれる. すぬけくんは,この順列を知らされない.

  • 次に,すぬけくんは,以下の操作を好きなだけ繰り返す.

    • 現在点灯しているランプを 1 つ選ぶ(そのようなランプがない場合は操作を行えない). 選んだランプの番号を i とする. そして,ボタン i を押す. すると,ランプ p_i の状態が反転する.つまり,ランプ p_i がもともと点灯しているなら消灯し,もともと点灯していないなら点灯する.

すぬけくんは,常に,どのランプが点灯しているかを知ることができます. すぬけくんの勝利条件は,すべてのランプを点灯した状態にすることです. これが不可能と判明した時点ですぬけくんは負けを認めます. すぬけくんが最適に行動するとき,勝率はいくらでしょうか?

すぬけくんの勝率を w とした時,w \times N! は整数になります. w \times N!10^9+7 で割ったあまりを求めてください.

制約

  • 2 \leq N \leq 10^7
  • 1 \leq A \leq \min(N-1,5000)

入力

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

N A

出力

すぬけくんの勝率を w とした時,w \times N!10^9+7 で割ったあまりを出力せよ.


入力例 1

3 1

出力例 1

2

すぬけくんはまず,ボタン 1 を押します. ランプ 1 が消灯した場合はすぬけくんの負けです. そうでないときは,新しく点灯したランプに対応するボタンを押します. ここで残っていたランプが点灯すれば,すぬけくんの勝ちです. 逆に,ランプ 1 が消灯した場合,すぬけくんの負けです. このゲームの勝率は 1/3 なので,(1/3)\times 3!=2 を出力します.


入力例 2

3 2

出力例 2

3

入力例 3

8 4

出力例 3

16776

入力例 4

9999999 4999

出力例 4

90395416

Score : 1200 points

Problem Statement

We have N lamps numbered 1 to N, and N buttons numbered 1 to N. Initially, Lamp 1, 2, \cdots, A are on, and the other lamps are off.

Snuke and Ringo will play the following game.

  • First, Ringo generates a permutation (p_1,p_2,\cdots,p_N) of (1,2,\cdots,N). The permutation is chosen from all N! possible permutations with equal probability, without being informed to Snuke.

  • Then, Snuke does the following operation any number of times he likes:

    • Choose a lamp that is on at the moment. (The operation cannot be done if there is no such lamp.) Let Lamp i be the chosen lamp. Press Button i, which switches the state of Lamp p_i. That is, Lamp p_i will be turned off if it is on, and vice versa.

At every moment, Snuke knows which lamps are on. Snuke wins if all the lamps are on, and he will surrender when it turns out that he cannot win. What is the probability of winning when Snuke plays optimally?

Let w be the probability of winning. Then, w \times N! will be an integer. Compute w \times N! modulo (10^9+7).

Constraints

  • 2 \leq N \leq 10^7
  • 1 \leq A \leq \min(N-1,5000)

Input

Input is given from Standard Input in the following format:

N A

Output

Print w \times N! modulo (10^9+7), where w is the probability of Snuke's winning.


Sample Input 1

3 1

Sample Output 1

2

First, Snuke will press Button 1. If Lamp 1 turns off, he loses. Otherwise, he will press the button that he can now press. If the remaining lamp turns on, he wins; if Lamp 1 turns off, he loses. The probability of winning in this game is 1/3, so we should print (1/3)\times 3!=2.


Sample Input 2

3 2

Sample Output 2

3

Sample Input 3

8 4

Sample Output 3

16776

Sample Input 4

9999999 4999

Sample Output 4

90395416
E - Fragile Balls

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1200

問題文

1 から N までの番号の付いた N 個の箱があります. また,1 から M までの番号の付いた M 個のボールがあります. 現在,ボール i は箱 A_i に入っています.

あなたは,以下の操作を行えます.

  • 現在ボールが 2 個以上入っている箱を 1 つ選ぶ. そして,その箱からボールを 1 つ選んで取り出し,別の箱に入れる.

ただし,ボールは非常に壊れやすいため,ボール i は合計で C_i 回より多く移動させることはできません. 逆に,ボールが壊れない限り,何度でもボールの移動は行なえます.

あなたの目標は,すべての i (1 \leq i \leq M)について,ボール i が箱 B_i に入っているようにすることです. この目的が達成可能かどうか判定してください. また可能な場合は,目標を達成するのに必要な操作回数の最小値を求めてください.

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i,B_i \leq N
  • 1 \leq C_i \leq 10^5
  • 目標とする状態において,どの箱にも 1 つ以上のボールが入っている. つまり,すべての i (1 \leq i \leq N) について,B_j=i を満たす j が存在する.

入力

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

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

出力

目標が達成不可能な場合は -1 を,達成可能な場合は必要な操作回数の最小値を出力せよ.


入力例 1

3 3
1 2 1
2 1 1
1 3 2

出力例 1

3

以下のように 3 回の操作を行えば良いです.

  • 1 からボール 1 を取り出し,箱 2 に入れる.
  • 2 からボール 2 を取り出し,箱 1 に入れる.
  • 1 からボール 3 を取り出し,箱 3 に入れる.

入力例 2

2 2
1 2 1
2 1 1

出力例 2

-1

入力例 3

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

出力例 3

6

入力例 4

1 1
1 1 1

出力例 4

0

Score : 1200 points

Problem Statement

We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.

You can do the following operation:

  • Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.

Since the balls are very easy to break, you cannot move Ball i more than C_i times in total. Within this limit, you can do the operation any number of times.

Your objective is to have Ball i in Box B_i for every i (1 \leq i \leq M). Determine whether this objective is achievable. If it is, also find the minimum number of operations required to achieve it.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq A_i,B_i \leq N
  • 1 \leq C_i \leq 10^5
  • In the situation where the objective is achieved, every box contains one or more balls. That is, for every i (1 \leq i \leq N), there exists j such that B_j=i.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

Output

If the objective is unachievable, print -1; if it is achievable, print the minimum number of operations required to achieve it.


Sample Input 1

3 3
1 2 1
2 1 1
1 3 2

Sample Output 1

3

We can achieve the objective in three operations, as follows:

  • Pick up Ball 1 from Box 1 and put it into Box 2.
  • Pick up Ball 2 from Box 2 and put it into Box 1.
  • Pick up Ball 3 from Box 1 and put it into Box 3.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

-1

Sample Input 3

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

Sample Output 3

6

Sample Input 4

1 1
1 1 1

Sample Output 4

0
F - Division into Multiples

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1800

問題文

すぬけ君は X+Y 個のボールを持っています. このうち X 個には整数 A が,残りの Y 個には整数 B が書かれています.

すぬけ君は,これらのボールをいくつかのグループに分けます. このとき,どのボールもちょうど 1 つのグループに含まれ,またどのグループも 1 つ以上のボールを含むようにします.

ここで,あるグループがよいグループであるとは,グループ内のボールに書かれている整数の総和が整数 C の整数倍であることを意味します. よいグループの個数の最大値を求めてください.

1 つの入力につき,T 個のテストケースに答えてください.

制約

  • 1 \leq T \leq 2 \times 10^4
  • 1 \leq A,X,B,Y,C \leq 10^9
  • A \neq B

入力

入力は以下の形式で標準入力から与えられる. 入力の 1 行目は以下のとおりである.

T

そして,T 個のテストケースが続く. これらはそれぞれ以下の形式で与えられる.

A X B Y C

出力

各テストケースについて,よいグループの個数の最大値を出力せよ. 各テストケースごとに改行せよ.


入力例 1

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

出力例 1

2
2
0

1 つ目のテストケースでは,\{3,3,4\},\{3,4,4,4\} とグループ分けすれば,よいグループの個数が 2 になります.

2 つ目のテストケースでは,\{2,1\},\{1,1,1\},\{1\} とグループ分けすれば,よいグループの個数が 2 になります.

Score : 1800 points

Problem Statement

Snuke has X+Y balls. X of them have an integer A written on them, and the other Y of them have an integer B written on them.

Snuke will divide these balls into some number of groups. Here, every ball should be contained in exactly one group, and every group should contain one or more balls.

A group is said to be good when the sum of the integers written on the balls in that group is a multiple of an integer C. Find the maximum possible number of good groups.

Solve T test cases for each input file.

Constraints

  • 1 \leq T \leq 2 \times 10^4
  • 1 \leq A,X,B,Y,C \leq 10^9
  • A \neq B

Input

Input is given from Standard Input in the following format. The first line is as follows:

T

Then, T test cases follow. Each test case is given in the following format:

A X B Y C

Output

For each test case, print a line containing the maximum possible number of good groups.


Sample Input 1

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

Sample Output 1

2
2
0

In the first test case, we can have two good groups by making the following groups: \{3,3,4\} and \{3,4,4,4\}.

In the second test case, we can have two good groups by making the following groups: \{2,1\}, \{1,1,1\}, and \{1\}.