Problem F: qwb has a lot of Coins
Time Limit: 1 Sec Memory Limit: 128 M[ 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?
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.
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
HINT
题意:有n堆硬币,两个人分别拿,规则是可以拿任意多个(必须拿),拿到最后一个的胜利,问先手胜利的方式有多少种。
分析: 其实就是裸的尼姆博弈,之前有一个cf有写过一个树上尼姆博弈,这个跟那个差不多。
先亦或查看是否为0,不为0的话枚举每一堆需要剩下多少个才能使得亦或为0,如果满足条件,方法数加1.
AC代码:
#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
int a[2000];
int main()
{
int n;
while(scanf("%d",&n)==1)
{
int xorr=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),xorr^=a[i];
if(!xorr)
printf("0\n");
else
{
int ans=0;
for(int i=1;i<=n;i++)
{
int y=xorr^a[i];
if(y<a[i])
ans++;
}
printf("%d\n",ans);
}
}
}