标签:异或 return c++ target https ons name == highlight
此题的题意比较容易理解,可以简单的看着 Nim 博弈的变种。但问题在于 Alice 对第 i 堆石子的取法必须根据 bi 确定。所以如果这个问题能够归结到正常的 Nim 博弈(取石子问题),则很容易解决。
考虑特判存在 bi=1 或 bi=2 的情况:
对于只有 bi=0 的情况,Nim 博弈全部 ai 异或即可。
经典的Nim游戏 只要连续取异或XOR就可以判断胜负状态。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], b[N], n;
bool jug()
{
int cnt[3] = {0, 0, 0}, tot = 0;
for(int i=1;i<=n;i++)
{
if(a[i]%2 && b[i] == 2) return false;
if(b[i] == 2) cnt[2]++, cnt[0]++;
if(a[i] > 1 && b[i] == 1) cnt[1]++, cnt[0]++;
}
if(cnt[0]>1) return false;
if(cnt[1] == 1) {
for(int i=1;i<=n;i++)
if(b[i] == 1 && a[i] > 1) tot ^= (a[i]%2?0:1);
else tot ^= a[i];
return !(tot?1:0);
}
else if(cnt[2] == 1) {
for(int i=1;i<=n;i++)
if(b[i] != 2)
tot ^= a[i];
return !(tot?1:0);
}
else {
for(int i=1;i<=n;i++)
tot ^= a[i];
return tot;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
printf("%s\n", jug() ? "Alice" : "Bob");
}
}
ZOJ 3964 Yet Another Game of Stones Nim游戏变种
标签:异或 return c++ target https ons name == highlight
原文地址:http://www.cnblogs.com/heisenberg-/p/6791543.html