您的位置:首页 > 其它

qwb has a lot of Coins 【Nim博弈】

2017-06-03 10:11 127 查看
Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 699 Solved: 220

[Submit][Status][Web Board]

Description

qwb has a lot of coins. One day, he decides to play a game with his friend using these coins. He first puts some of his coins into M piles, each of which is composed of Ni (1<=i<=M) coins. Then, the two players play the coin game in turns. Every step, one can remove one or more coins from only one pile. The winner is the one who removes the last coin.

Then comes the question: How many different ways the first player can do that will ensure him win the game?

Input

Input contains multiple test cases till the end of file. Each test case starts with a number M (1 <= M<= 1000) meaning the number of piles. The next line contains M integers Ni (1 <= Ni <= 1e9, 1 <= i<= M) indicating the number of coins in pile i.

Output

For each case, put the method count in one line.

If the first player can win the game, the method count is the number of different ways that he can do to ensure him win the game, otherwise zero.

Sample Input

3

1 2 3

1

1

Sample Output

0

1

学习了—Nim 博弈

学习了–可以预处理前后缀

题意:问你Nim游戏先手第一步能赢的方案数

能赢就是这个堆的石子个数大于其他堆得石子个数异或和

虽然给了1000,但是这题是可以预处理前后缀异或和达到O(n)的

代码

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long
const int MAXN = 1000+10;
const int MAXM = 1e6+100;
const double PI = acos(-1.0);
const double eps = 1e-8;
inline int read(){
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
while (ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
return x*f;
}
/*--------------------------------------*/
int pre[MAXN],suf[MAXN];
int n;
int arr[MAXN];
void getmap()  // 预处理前后缀
{
pre[0]=suf[n+1]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
pre[i]=pre[i-1]^arr[i];
}

for(int i=n;i;i--)
suf[i]=suf[i+1]^arr[i];
}
int main()
{
while(~scanf("%d",&n))
{
getmap();
int ans=0;
for(int i=1;i<=n;i++)
{
if(arr[i]>(pre[i-1]^suf[i+1])) // 优先级,被坑了。 以后的位运算还是都加上括号比较稳妥
ans++;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: