Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
石の山が N 個あり、 i 番目 (1\le i\le N) の山には A_i 個の石が積まれています。
Alice と Bob がこれらの山を使って以下のようなゲームを行います。
- Alice から始めて両者は交互に手番をプレイする。
- 各手番では、プレイヤーは空でない山を一つ選び、その山から 1 個以上好きな個数の石を取り除く。ただし、石を取り除く前後で「全ての山の石の個数をビット単位 \mathrm{OR} 演算することで得られる値」が変わるような行動を取ることはできない。
先に手番をプレイできなくなったプレイヤーの負けです。
両者が最善を尽くしたとき、どちらのプレイヤーが勝つかを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
ビット単位 \mathrm{OR} 演算とは
非負整数 A, B のビット単位 \mathrm{OR}、A\ \mathrm{OR}\ B は以下のように定義されます。
- A\ \mathrm{OR}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも片方が 1 であれば 1、そうでなければ 0 である。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{OR} は (\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k) と定義され、これは p_1, p_2, p_3, \dots p_k の順番によらないことが証明できます。
制約
- 1\le T\le 10^5
- 2\le N \le 2\times 10^5
- 全てのテストケースにおける N の総和は 2\times 10^5 以下
- 1\le A_i \le 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N A_1 A_2 \ldots A_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、両者が最善を尽くしたとき Alice が勝つならば Alice を、Bob が勝つならば Bob を出力せよ。
入力例 1
4 3 3 4 6 3 7 7 7 3 9 3 5 10 1 9 1 3 7 9 10 9 7 3
出力例 1
Alice Bob Bob Alice
1 つ目のテストケースについて考えます。
はじめ、全ての山の石の個数をビット単位 \mathrm{OR} 演算することで得られる値は 3\ \mathrm{OR}\ 4\ \mathrm{OR}\ 6=7 です。
Alice がプレイすることのできる手番は以下の通りです:
- 1 番目の山から 2 個石を取り除く。
- 2 番目の山から 1 個以上好きな個数の石を取り除く。
- 3 番目の山から 1 個以上好きな個数の石を取り除く。
例えば 1 番目の山から 1 個石を取り除いた場合、全ての山の石の個数をビット単位 \mathrm{OR} 演算することで得られる値は 2\ \mathrm{OR}\ 4\ \mathrm{OR}\ 6=6 となります。したがって、 Alice は 1 番目の山から 1 個石を取り除く行動を取ることはできません。
Alice が 3 番目の山から 6 個石を取り除いた場合、 Bob はその次の手番がプレイできなくなります。したがって、両者が最善を尽くしたときの勝者は Alice です。
Score : 500 points
Problem Statement
There are N piles of stones, and the i-th pile (1\le i\le N) has A_i stones.
Alice and Bob will play the following game using these piles:
- Starting with Alice, the two players take turns alternately.
- In each turn, the player chooses one non-empty pile and removes one or more stones from that pile. However, the player cannot make a move that changes the bitwise \mathrm{OR} of the numbers of stones of all piles.
The player who cannot make a move first loses.
Determine which player wins when both players play optimally.
You are given T test cases; solve each of them.
What is bitwise \mathrm{OR}?
The bitwise \mathrm{OR} of non-negative integers A and B, A\ \mathrm{OR}\ B, is defined as follows:
- When A\ \mathrm{OR}\ B is written in binary, the digit in the 2^k place (k \geq 0) is 1 if at least one of the digits in the 2^k place of A and B written in binary is 1, and 0 otherwise.
In general, the bitwise \mathrm{OR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1\ \mathrm{OR}\ p_2)\ \mathrm{OR}\ p_3)\ \mathrm{OR}\ \dots\ \mathrm{OR}\ p_k), and it can be proved that this is independent of the order of p_1, p_2, p_3, \dots p_k.
Constraints
- 1\le T\le 10^5
- 2\le N \le 2\times 10^5
- The sum of N over all test cases is at most 2\times 10^5.
- 1\le A_i \le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N A_1 A_2 \ldots A_N
Output
Output the answers for each test case in order, separated by newlines.
For each test case, print Alice if Alice wins when both players play optimally, and Bob if Bob wins.
Sample Input 1
4 3 3 4 6 3 7 7 7 3 9 3 5 10 1 9 1 3 7 9 10 9 7 3
Sample Output 1
Alice Bob Bob Alice
Consider the first test case.
Initially, the bitwise \mathrm{OR} of the numbers of stones of all piles is 3\ \mathrm{OR}\ 4\ \mathrm{OR}\ 6=7.
The moves Alice can make are as follows:
- Remove two stones from the first pile.
- Remove one or more stones from the second pile.
- Remove one or more stones from the third pile.
For example, if Alice removes one stone from the first pile, the bitwise \mathrm{OR} of the numbers of stones of all piles becomes 2\ \mathrm{OR}\ 4\ \mathrm{OR}\ 6=6. Therefore, Alice cannot make the move of removing one stone from the first pile.
If Alice removes six stones from the third pile, Bob cannot make any move in the next turn. Therefore, the winner when both players play optimally is Alice.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
正整数 N,K が与えられます。
以下の条件を全て満たす長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) のうち、 A_N の値が最小となるものを一つ求めてください。
- A は広義単調増加である。すなわち、 1\le i\le N-1 に対し A_i \le A_{i+1} が成り立つ
- \displaystyle \sum_{i=1}^{N-1} (A_{i+1} \bmod A_i)=K
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T \le 10^5
- 2\le N \le 2\times 10^5
- 全てのテストケースにおける N の総和は 2\times 10^5 以下
- 1\le K\le 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N K
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、条件を全て満たし A_N の値が最小となる A を以下の形式で出力せよ。
A_1 A_2 \ldots A_N
条件を全て満たし A_N の値が最小となる A が複数ある場合、どれを出力しても正答となる。
入力例 1
2 5 3 2 3
出力例 1
1 2 3 4 5 4 7
1 つ目のテストケースについて考えます。
A=(1,2,3,4,5) は広義単調増加であり \displaystyle \sum_{i=1}^{N-1} (A_{i+1} \bmod A_i) = (2\bmod 1) + (3\bmod 2) + (4\bmod 3) + (5\bmod 4)=3=K が成り立つことから条件を全て満たすことが分かります。
A_N の値が 5 より小さく条件を全て満たすような A は存在しないので、A=(1,2,3,4,5) を出力すると正答となります。
この他にも、 A=(2,3,4,5,5) や A=(2,3,3,5,5) などを出力しても正答となります。
Score : 600 points
Problem Statement
You are given positive integers N and K.
Find one length-N sequence of positive integers A=(A_1,A_2,\ldots,A_N) with the minimum value of A_N among the ones that satisfy all of the following conditions.
- A is non-decreasing. That is, A_i \le A_{i+1} for 1\le i\le N-1.
- \displaystyle \sum_{i=1}^{N-1} (A_{i+1} \bmod A_i)=K.
You are given T test cases; solve each of them.
Constraints
- 1\le T \le 10^5
- 2\le N \le 2\times 10^5
- The sum of N over all test cases is at most 2\times 10^5.
- 1\le K\le 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N K
Output
Output the answers for each test case in order, separated by newlines.
For each test case, print A that satisfies all conditions and has the minimum value of A_N in the following format:
A_1 A_2 \ldots A_N
If there are multiple A that satisfy all conditions and have the minimum value of A_N, any of them will be accepted.
Sample Input 1
2 5 3 2 3
Sample Output 1
1 2 3 4 5 4 7
Consider the first test case.
A=(1,2,3,4,5) is non-decreasing and satisfies \displaystyle \sum_{i=1}^{N-1} (A_{i+1} \bmod A_i) = (2\bmod 1) + (3\bmod 2) + (4\bmod 3) + (5\bmod 4)=3=K, so it satisfies all conditions.
There does not exist an A that satisfies all conditions and has a value of A_N less than 5, so printing A=(1,2,3,4,5) will be accepted.
Other than this, printing A=(2,3,4,5,5) or A=(2,3,3,5,5) will also be accepted.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
2^{30} 未満の正整数 C,X が与えられます。
以下の条件を全て満たす正整数 n が存在するか判定し、存在する場合は一つ求めてください。
- 1\le n < 2^{60}
- (n \oplus C) \bmod n=X
ただし、 n \oplus C は n と C のビット単位 \mathrm{XOR} を表します。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
ビット単位 \mathrm{XOR} 演算とは
非負整数 X,Y のビット単位 \mathrm{XOR} 、X \oplus Y は、以下のように定義されます。
- X \oplus Y を二進表記した際の 2^k (k \geq 0) の位の数は、X,Y を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
制約
- 1\le T\le 2\times 10^5
- 1\le C,X < 2^{30}
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
C X
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、条件を全て満たす n が存在しない場合は -1 を出力せよ。
そうでない場合、条件を全て満たす n を出力せよ。
条件を全て満たす n が複数ある場合、どれを出力しても正答となる。
入力例 1
3 10 6 2 3 777 153
出力例 1
7 -1 208
1 つ目のテストケースについて考えます。
n=7 は (n\oplus C) \bmod n = (7 \oplus 10) \bmod 7 = 13\bmod 7 = 6=X より条件を全て満たすことが分かります。したがって、 1 行目には 7 を出力すると正答となります。
この他にも n=12,18,19 などを出力しても正答となります。
Score : 700 points
Problem Statement
You are given positive integers C and X that are less than 2^{30}.
Determine whether there exists a positive integer n that satisfies all of the following conditions, and if it exists, find one such n.
- 1\le n < 2^{60}
- (n \oplus C) \bmod n=X
Here, n \oplus C denotes the bitwise \mathrm{XOR} of n and C.
You are given T test cases, solve each of them.
What is bitwise \mathrm{XOR}?
The bitwise \mathrm{XOR} of non-negative integers X and Y, X \oplus Y, is defined as follows:
- When X \oplus Y is written in binary, the digit in the 2^k place (k \geq 0) is 1 if exactly one of the digits in the 2^k place of X and Y written in binary is 1, and 0 otherwise.
Constraints
- 1\le T\le 2\times 10^5
- 1\le C,X < 2^{30}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
C X
Output
Output the answers for each test case in order, separated by newlines.
For each test case, if there does not exist an n that satisfies all conditions, print -1.
Otherwise, print n that satisfies all conditions.
If there are multiple n that satisfy all conditions, any of them will be accepted.
Sample Input 1
3 10 6 2 3 777 153
Sample Output 1
7 -1 208
Consider the first test case.
n=7 satisfies all conditions because (n\oplus C) \bmod n = (7 \oplus 10) \bmod 7 = 13\bmod 7 = 6=X. Therefore, printing 7 on the first line will be accepted.
Other than this, printing n=12,18,19 etc. will also be accepted.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 700 点
問題文
各要素が 1 以上 N 以下であるような長さ N の整数列 Y=(Y_1,Y_2,\ldots,Y_N) が与えられます。
以下の条件を全て満たす N 行 N 列の整数行列 A=(A_{i,j}) (1\le i , j \le N) が存在するか判定し、存在する場合は一つ求めてください。
- 1\le A_{i,j} \le N (1\le i \le N, 1\le j\le N)
- A_{i,j}=A_{j,i} (1\le i\le N, 1\le j\le N)
- A_{i,j_1}\neq A_{i,j_2} (1\le i\le N, 1\le j_1 < j_2 \le N)
- A_{i,Y_i}=1 (1\le i\le N)
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 5000
- 1\le N \le 500
- 全てのテストケースにおける N^2 の総和は 500^2 以下
- 1\le Y_i\le N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N Y_1 Y_2 \ldots Y_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、条件を全て満たす A が存在しない場合は No を出力せよ。
そうでない場合、条件を全て満たす A を以下の形式で出力せよ。
Yes
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
条件を全て満たす A が複数ある場合、どれを出力しても正答となる。
入力例 1
4 3 1 3 2 3 1 2 3 1 1 5 1 3 2 5 4
出力例 1
Yes 1 2 3 2 3 1 3 1 2 No Yes 1 Yes 1 2 5 4 3 2 3 1 5 4 5 1 4 3 2 4 5 3 2 1 3 4 2 1 5
1 つ目のテストケースについて考えます。
出力例の A は条件を全て満たすことが確認できます。この他にも、例えば以下のような A を出力しても正答となります。
1 3 2 3 2 1 2 1 3
Score : 700 points
Problem Statement
You are given an integer sequence Y=(Y_1,Y_2,\ldots,Y_N) of length N where each element is between 1 and N inclusive.
Determine whether there exists an N \times N integer matrix A=(A_{i,j}) (1\le i , j \le N) that satisfies all of the following conditions, and if it exists, find one such matrix.
- 1\le A_{i,j} \le N (1\le i \le N, 1\le j\le N)
- A_{i,j}=A_{j,i} (1\le i\le N, 1\le j\le N)
- A_{i,j_1}\neq A_{i,j_2} (1\le i\le N, 1\le j_1 < j_2 \le N)
- A_{i,Y_i}=1 (1\le i\le N)
You are given T test cases; solve each of them.
Constraints
- 1\le T\le 5000
- 1\le N \le 500
- The sum of N^2 over all test cases is at most 500^2.
- 1\le Y_i\le N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N Y_1 Y_2 \ldots Y_N
Output
Output the answers for each test case in order, separated by newlines.
For each test case, if there does not exist an A that satisfies all conditions, print No.
Otherwise, print A that satisfies all conditions in the following format:
Yes
A_{1,1} A_{1,2} \ldots A_{1,N}
A_{2,1} A_{2,2} \ldots A_{2,N}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,N}
If there are multiple A that satisfy all conditions, any of them will be accepted.
Sample Input 1
4 3 1 3 2 3 1 2 3 1 1 5 1 3 2 5 4
Sample Output 1
Yes 1 2 3 2 3 1 3 1 2 No Yes 1 Yes 1 2 5 4 3 2 3 1 5 4 5 1 4 3 2 4 5 3 2 1 3 4 2 1 5
Consider the first test case.
It can be verified that the A in the sample output satisfies all conditions. Other than this, for example, the following A will also be accepted:
1 3 2 3 2 1 2 1 3
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 1100 点
問題文
正整数 N,X,Y と長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
石の山が N 個あり、 i 番目 (1\le i\le N) の山には A_i 個の石が積まれています。
Alice と Bob がこれらの山を使って以下のようなゲームを行います。
- Alice から始めて両者は交互に手番をプレイする。
- 各手番では、プレイヤーは空でない山を一つ選び、その山から 1 個か 2 個石を取り除く。ただし、取り除いた後にその山の石の個数が X の倍数または Y の倍数となるような行動を取ることはできない。
先に手番をプレイできなくなったプレイヤーの負けです。
両者が最善を尽くしたとき、どちらのプレイヤーが勝つかを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T \le 2\times 10^5
- 1\le N \le 2\times 10^5
- 全てのテストケースにおける N の総和は 2\times 10^5 以下
- 1\le X < Y\le 10^9
- 1\le A_i \le 10^{18}
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケースは以下の形式で与えられる。
N X Y A_1 A_2 \ldots A_N
出力
各テストケースに対する答えを順に改行区切りで出力せよ。
各テストケースについて、両者が最善を尽くしたとき Alice が勝つならば Alice を、Bob が勝つならば Bob を出力せよ。
入力例 1
3 2 3 5 4 12 5 1 2 1 2 3 4 5 6 530 879 17031 42909 97721 34323 39597 26999
出力例 1
Alice Bob Alice
1 つ目のテストケースについて考えます。
例えばゲームは以下のように進行します。
- Alice が 1 番目の山から石を 2 個取り除く。 1 番目の山の石は残り 2 個となる。
- Bob が 2 番目の山から石を 1 個取り除く。 2 番目の山の石は残り 11 個となる。
- Alice が 1 番目の山から石を 1 個取り除く。 1 番目の山の石は残り 1 個となる。
- Bob は手番をプレイできないので、Bob の負けであり Alice の勝ちとなる。
このテストケースでは両者がどのように手番をプレイしても Alice が必ず勝ちます。したがって、 1 行目には Alice と出力してください。
Score : 1100 points
Problem Statement
You are given positive integers N, X, Y and a length-N integer sequence A=(A_1,A_2,\ldots,A_N).
There are N piles of stones, and the i-th pile (1\le i\le N) has A_i stones.
Alice and Bob will play the following game using these piles:
- Starting with Alice, the two players take turns alternately.
- In each turn, the player chooses one non-empty pile and removes one or two stones from that pile. However, the player cannot make a move such that the number of stones in that pile becomes a multiple of X or a multiple of Y after removing stones.
The player who cannot make a move first loses.
Determine which player wins when both players play optimally.
You are given T test cases, solve each of them.
Constraints
- 1\le T \le 2\times 10^5
- 1\le N \le 2\times 10^5
- The sum of N over all test cases is at most 2\times 10^5.
- 1\le X < Y\le 10^9
- 1\le A_i \le 10^{18}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case is given in the following format:
N X Y A_1 A_2 \ldots A_N
Output
Output the answers for each test case in order, separated by newlines.
For each test case, print Alice if Alice wins when both players play optimally, and Bob if Bob wins.
Sample Input 1
3 2 3 5 4 12 5 1 2 1 2 3 4 5 6 530 879 17031 42909 97721 34323 39597 26999
Sample Output 1
Alice Bob Alice
Consider the first test case.
For example, the game proceeds as follows:
- Alice removes two stones from the first pile. The first pile has two stones remaining.
- Bob removes one stone from the second pile. The second pile has eleven stones remaining.
- Alice removes one stone from the first pile. The first pile has one stone remaining.
- Bob cannot make a move, so Bob loses and Alice wins.
In this test case, Alice always wins no matter how both players make their moves. Therefore, print Alice on the first line.