标签:des style blog class code java
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1850
3 5 7 9 0
1
题目意思:
有M堆扑克,每堆有Ni张,两个人轮流拿可以拿一堆中的任意张,最后不能拿的人输。求先拿者获胜的拿法。
解题思路:
Nim博弈。
记n1^n2^n3..^n[m]=x
显然若x=0,则该局势为奇异局势,必输。
如果把ni变成x^ni,则n1^n2^...^n[i-1]^n[i+1]^...^n[m]^x^ni=0,为奇异局势。显然如果ni>x^ni,如果能ni堆中拿走ni-x^ni张牌,则一定会获胜。
所以只用统计ni中比x^ni大于的个数,结果就是先拿者获胜的种数。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 1100000 int hp[Maxn]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int n; while(scanf("%d",&n)&&n) { int ans=0,sum=0; for(int i=1;i<=n;i++) { scanf("%d",&hp[i]); sum^=hp[i]; } for(int i=1;i<=n;i++) if(hp[i]>(sum^hp[i])) //拿走hp[i]-(sum^hp[i])个,是一种赢法 ans++; printf("%d\n",ans); } return 0; }
[Nim博弈]hdu 1850 Being a Good Boy in Spring Festival,布布扣,bubuko.com
[Nim博弈]hdu 1850 Being a Good Boy in Spring Festival
标签:des style blog class code java
原文地址:http://blog.csdn.net/cc_again/article/details/25894399