HDU2176 取(m堆)石子游戏
来源:互联网 发布:python 判断加载完成 编辑:程序博客网 时间:2024/05/29 03:28
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2176
题意:nim游戏,先手必败输出no,先手必胜输出yes和第一步的每一种取法。
思路:基础nim,顺序输出取法就行。
这是篇nim博弈入门的经典帖子http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<cmath>#include<set>using namespace std;int a[200005];int main(){ int m; while (scanf("%d", &m), m){ int c = 0; for (int i = 0; i < m; i++){ scanf("%d", &a[i]); c ^= a[i]; } if (c == 0){ printf("No\n"); continue; } printf("Yes\n"); for (int i = 0; i < m; i++){ if ((a[i] ^ c) < a[i]) printf("%d %d\n", a[i], a[i] ^ c); } } return 0;}
因为nim博弈的性质不熟悉超时一发,不需要每个数都判断是否含有c的最高位,只要含有c的最高位,与c亦或一定小于原数。
超时代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<cmath>#include<set>using namespace std;int a[200005];int main(){ int m; while (scanf("%d", &m), m){ int c = 0; for (int i = 0; i < m; i++){ scanf("%d", &a[i]); c ^= a[i]; } if (c == 0){ printf("No\n"); continue; } printf("Yes\n"); int cc = 1; while ((cc << 1) <= c)cc <<= 1; int t = cc << 1; for (int i = 0; i < m; i++){ if (a[i] % t >= cc) printf("%d %d\n", a[i], a[i] ^ c); } } return 0;}
0 0
- HDU2176 取(m堆)石子游戏
- hdu2176 取(m堆)石子游戏
- HDU2176:取(m堆)石子游戏(Nim)
- HDU2176 取(m堆)石子游戏
- hdu2176 取(m堆)石子游戏
- hdu2176 取(m堆)石子游戏
- hdu2176取(m堆)石子游戏
- HDU2176 取(m堆)石子游戏
- HDU2176取(m堆)石子游戏
- hdu2176 取(m堆)石子游戏 nim博弈
- nim博弈 hdu2176 取(m堆)石子游戏
- hdu2176 取(m堆)石子游戏(nim博弈)
- hdu2176取(m堆)石子游戏 ——Nim 博弈
- hdu2176取(m堆)石子游戏 组合游戏初步——NIM
- ACM-尼姆博弈之取(m堆)石子游戏——hdu2176
- hdu2176取(m堆)石子游戏 nim hdu1848Fibonacci again and again sg
- hdu 2176 取(m堆)石子游戏
- hdu 2176 取(m堆)石子游戏
- HDU 1257 最少拦截系统(LIS,DP,附宇神讲课笔记)
- 3000门徒内部训练绝密视频(泄密版)第3课:Scala中函数式编程彻底精通及Spark源码阅读
- 扩展ThreadPoolExecutor打印线程错误堆栈
- HDU 1558 判断两个线段相交附带并查集
- 使用Pull 解析XML文件
- HDU2176 取(m堆)石子游戏
- 《入门经典》2
- JDBC PreparedStatement接口的应用
- android面试(基础篇 9)
- activity的双向传值
- Ubuntu 14.04 ssh的安装和配置
- UVALive 7261 Xiongnu's Land(二分)
- hdu4707Pet最短路(spfa)
- [2482]二叉排序树