您的位置:首页 > 其它

uva 1482 - Playing With Stones(Nim游戏)

2014-08-05 23:27 417 查看
题目链接: uva 1482 - Playing With Stones

题目大意:n堆石子,给定每堆石子的个数,两个人分别从操作,每次可以从一堆中取走至少一个石子,但是不能超过一半。如果不能操作则视为失败。

解题思路:对于每一堆式子来说,可以看作一个Nim游戏,但是SG(x)并不等于x,因为每次取石子不能超过一半,所以对于偶数SG(x)=x/2,对于奇数SG(x)=SG(x/2).

所以去Nim和即可。
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

ll SG (ll x) {
return x&1 ? SG(x>>1) : x>>1;
}

int main () {
int cas, n;

scanf("%d", &cas);
while (cas--) {
ll m, v = 0;
scanf("%d", &n);

for (int i = 0; i < n; i++) {
scanf("%lld", &m);
v ^= SG(m);
}
printf("%s\n", v ? "YES" : "NO");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: