HDU2509 Be the Winner
2016-07-10 21:36
267 查看
Be the Winner
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3062 Accepted Submission(s): 1709
For example "@@@" can be turned into "@@" or "@" or "@ @"(two piles). two people get apples one after another and the one who takes the last is
the loser. Fra wants to know in which situations he can win by playing strategies (that is, no matter what action the rival takes, fra will win).
Input You will be given several cases. Each test case begins with a single number n (1 <= n <= 100), followed by a line with n numbers, the number of apples in each pile. There is a blank line between cases.
Output If a winning strategies can be found, print a single line with "Yes", otherwise print "No".
Sample Input 2 2 2 1 3
Sample Output No Yes
Source ECJTU 2008 Autumn Contest
Recommend lcy
太长不看版:
计算所有堆的异或和。若异或和不为0,那么一定有取法将其转化为0;若异或和为0,因为必须取,那么不得不将其转化成异或和不为0的状态。
最后面对着都取完的状态(0)的人输,按以上规律判定能不能占先机将异或和转化为0即可。
长也要看版:
反尼姆博弈(anti-nim)
该类问题的判定方法:
下面是从知乎找到的分析↓
奇异局势,所有堆的xor和==0.
假定S是非奇异局势,T是奇异局势。
一堆中石子数量>=2,表示充裕堆, =1表示孤单堆。
S0即非奇异局势下,充裕堆为0的状态
S1即非奇异局势下,充裕堆为1的状态
S2即非奇异局势下,充裕堆>=2的状态
T0即奇异局势下,充裕堆为0的状态
T2即奇异局势下,充裕堆>=2的状态
1.奇异局势的定义可知,S能转移到T,能转移到S, T只能转移到S
2.S0必败,T0必胜
3.S1必胜,因为S1只需要转移到S0即可。
4.S2必胜,T2必败。
1)T2只能转移到S1 和 S2
2)若T2转移到S1 则T2败,若T2转移到S2,S2只需要转回到T2即可。所以S2胜,T2败。
所以:
必胜态:T0,S1,S2
必败态:S0,T2
作者:公丕鑫
来源:知乎
另外还有一篇比较详细的证明分析:http://qianmacao.blog.163.com/blog/static/20339718020123555821140/
然后是代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int main(){ int n; while(scanf("%d",&n)!=EOF){ int i,j; int s=0;bool flag=0; for(i=1;i<=n;i++){ scanf("%d",&j); s^=j; if(j>1)flag=1; } if(!flag){//充裕堆为0 if(n%2)//堆数为奇数,必败 printf("No\n"); else printf("Yes\n"); } else{//充裕堆>0 if(s==0)//奇异局势 printf("No\n"); else printf("Yes\n"); } } return 0; }
相关文章推荐
- CAS的小实验
- Spring核心技术(九)——Spring管理的组件和Classpath扫描
- ATOM基础教程一ATOM自定义代码片段(8)
- Android-多线程AsyncTask
- struts2 面试题
- SimpleAdapter的应用
- DEV调试C++项目实例
- Python 函数式编程--匿名函数Lambda
- 开发过程中的注意事项,想到哪写到哪
- C++之静态联编与动态联编及virtual关键字的作用
- 二进制中1的个数(最精简代码)
- Android 那些你所不知道的Bitmap对象详解
- Mobile Service
- iframe session过期跳转到登陆页面
- Spring JDBC 访问数据库
- 比想象中好用的Android单元测试——初探
- Python 函数式编程--返回函数
- Matlab常用的集合运算
- .Net Core开发环境Build
- javascript设计模式学习之十——组合模式