hdu1850Being a Good Boy in Spring Festival

来源:互联网 发布:孕妇不能吃薄荷糖 知乎 编辑:程序博客网 时间:2024/05/16 06:11

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1850

题意:中文题。

分析:这题从经典的nim博弈变为了求先手取第一次的方案数,这其实就是考察我们对于nim博弈这个原理的理解,因为我们的异或和xor只有非0的时候才有先手胜的方法,那么我们只要考虑怎么样才能从将异或和变为0,只要简单思考一下我们就能知道只要将xor的最高位二进制减小至剩余的位的数值大小即可,也就是说我们只要找有多少个堆的个数在xor的最高位同样是1就行了。详见代码。

代码:

#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<cstdio>#include<vector>#include<string>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int N=1010;const int MAX=1000000100;const int mod=100000000;const int MOD1=1000000007;const int MOD2=1000000009;const double EPS=0.00000001;typedef long long ll;const ll MOD=1000000007;const int INF=1000000010;typedef double db;typedef long double ldb;typedef unsigned long long ull;int a[105];int main(){    int i,n,xo,ans;    while (scanf("%d", &n)&&n) {        xo=ans=0;        for (i=1;i<=n;i++) {            scanf("%d", &a[i]);xo^=a[i];        }        while (xo-(xo&-xo)) xo-=xo&-xo;        for (i=1;i<=n;i++)        if (a[i]&xo) ans++;        printf("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击