[ARC208 (Div. 2)A]A - Bitwise OR Game题解
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 500 points
Problem Statement
There are N piles of stones, and the i-th pile (1≤i≤N) has Ai 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 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 OR?
The bitwise OR of non-negative integers A and B, A OR B, is defined as follows:
- When A OR B is written in binary, the digit in the 2k place (k≥0) is 1 if at least one of the digits in the 2k place of A and B written in binary is 1, and 0 otherwise.
For example, 3 OR 5=7 (in binary: 011 OR 101=111).
In general, the bitwise OR of k non-negative integers p1,p2,p3,…,pk is defined as (…((p1 OR p2) OR p3) OR … OR pk), and it can be proved that this is independent of the order of p1,p2,p3,…pk.
有道 翻译
按位 OR 是什么?
非负整数 A 和 B , A OR B 的按位 OR 定义如下:
—当以二进制形式写 A OR B 时,如果 A 和 B 的 2k 位中至少有一位为 1 ,则 2k 位( k≥0 )的数字为 1 ,否则为 0 。
例如, 3 OR 5=7 (二进制: 011 OR 101=111 )。
一般来说, k 的非负整数 p1,p2,p3,…,pk 的按位 OR 被定义为 (…((p1 OR p2) OR p3) OR … OR pk) ,并且可以证明这与 p1,p2,p3,…pk 的阶数无关。
有道 翻译
问题陈述
有 N 堆的石头,第 i 第 (1≤i≤N) 堆有 Ai 块石头。
Alice和Bob将使用这些纸牌玩以下游戏:
从爱丽丝开始,两名选手轮流上场。
在每个回合中,玩家选择一个非空的堆,并从堆中移除一个或多个石头。然而,玩家不能做出改变所有堆中按位排列的石头数的移动。
不能先走一步的玩家输了。
当两名玩家都达到最佳状态时,确定哪名玩家获胜。
给你 T 个测试用例;每个都解出来。
按位 OR 是多少?
非负整数 A 和 B A OR B 的按位 OR 定义如下:
—当以二进制形式写 A OR B 时,如果 A 和 B 的 2k 位中至少有一位为 1 ,则 2k 位( k≥0 )的数字为 1 ,否则为 0 。
例如, 3 OR 5=7 (二进制: 011 OR 101=111 )。
一般来说, k 的非负整数 p1,p2,p3,…,pk 的按位 OR 被定义为 (…((p1 OR p2) OR p3) OR … OR pk) ,并且可以证明这与 p1,p2,p3,…pk 的阶数无关。
Constraints
- 1≤T≤105
- 2≤N≤2×105
- The sum of N over all test cases is at most 2×105.
- 1≤Ai≤109
- All input values are integers.
有道 翻译
# #约束
—— 1≤T≤105
—— 2≤N≤2×105
—所有测试用例 N 之和不超过 2×105 。
—— 1≤Ai≤109
—所有输入值均为整数。
Input
The input is given from Standard Input in the following format:
T case1 case2 ⋮ caseT
Each test case is given in the following format:
N A1 A2 … AN
有道 翻译
# #输入
输入来自标准输入,格式如下:
T
case1
case2
⋮
caseT
每个测试用例以以下格式给出:
N
A1 A2 … AN
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.
有道 翻译
# #输出
按顺序输出每个测试用例的答案,以换行符分隔。
对于每个测试用例,如果两个玩家都以最佳状态玩游戏,则输出“Alice”,如果Bob赢了,则输出“Bob”。
Sample Input 1
Copy
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
Copy
Alice Bob Bob Alice
Consider the first test case.
Initially, the bitwise OR of the numbers of stones of all piles is 3 OR 4 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 OR of the numbers of stones of all piles becomes 2 OR 4 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.
有道 翻译
###输出示例
Alice
Bob
Bob
Alice
考虑第一个测试用例。
最初,所有桩的石子数按位 OR 为 3 OR 4 OR 6=7 。
Alice可以做的动作如下:
-从第一堆移走两块石头。
-从第二堆中移出一个或多个石头。
-从第三堆中移出一个或多个石头。
例如,如果Alice从第一堆中取出一颗石头,那么所有堆中石头的位数为 OR ,变为 2 OR 4 OR 6=6 。因此,爱丽丝不能从第一堆中移走一块石头。
如果Alice从第三摞中移出6颗石头,Bob在下一回合中就不能移动。因此,当两个玩家都玩得最优时,赢家是Alice。
思路
找规律。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long t,n,a[2000005],lk=0,lk2=0,lk3=0;
int main(){
cin>>t;
while(t--){
cin>>n;
lk=1;
lk2=1;
lk3=pow(2,32)-1;
for(int i=1;i<=n;i++){
cin>>a[i];
if(i!=1){
lk=lk^a[i];
}
else{
lk=1;
}
if(i!=1){
lk2=lk2|a[i];
}
else{
lk2=1;
}
// lk|=a[i];
// lk2^=(a[i]-a[i-1]);
// lk3&=a[i];
}
sort(a+1,a+n+1);
lk=lk2=0;
for(int i=1;i<=n;i++){
if(i!=0){
lk=lk^a[i];
}
else{
lk=1;
}
if(i!=0){
lk2=lk2|a[i];
}
else{
lk2=1;
}
// lk|=a[i];
// lk2^=(a[i]-a[i-1]);
// lk3&=a[i];
}
if(lk2!=lk){
cout<<"Alice"<<endl;
}
else{
cout<<"Bob"<<endl;
}
}
return 0;
}

浙公网安备 33010602011771号