HDU 4388 Stone Game II {博弈||找规律}
Stone Game II
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 531 Accepted Submission(s): 300
First, choose a pile of stones. (We assume that the number of stones in this pile is n)
Second, take some stones from this pile. Assume the number of stones
left in this pile is k. The player must ensure that 0 < k < n and
(k XOR n) < n, otherwise he loses.
At last, add a new pile of
size (k XOR n). Now the player can add a pile of size ((2*k) XOR n)
instead of (k XOR n) (However, there is only one opportunity for each
player in each game).
The first player who can't do these operations
loses. Suppose two players will do their best in the game, you are asked
to write a program to determine who will win the game.
The
first line contains the number T of test cases (T<=150). The first
line of each test cases contains an integer number n (n<=50),
denoting the number of piles. The following n integers describe the
number of stones in each pile at the beginning of the game.
You can assume that all the number of stones in each pile will not exceed 100,000.
For each test case, print the case number and the answer. if the first player will win the game print "Yes"(quotes for clarity) in a single line, otherwise print "No"(quotes for clarity).
给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则:
1. 任意选择一个堆,假设该堆有x个物品,从中选择k个,要保证0<k<x且0<(x^k)<x。
2. 再增加一个大小为x^k的堆(也就相当于将一个x个物品的堆变成一个k个物品的堆和一个x^k个物品的堆),另外有一个技能,可以将这个大小为x^k的堆变成(2*k)^x的堆,但是这个技能每个人只有一次机会可以使用。
现在问两人轮流操作,都采取最优策略,最后不能操作的人输,问谁会赢。
解题思路
把每堆原来a个物品分成两堆,k和k^a,证明得,这样分堆,不会影响二进制中1的总数的奇偶性,(后面会给出证明)为什么要考虑二进制呢?
注意题目中的条件,x^k<x,所以当x的二进制表示中只有一个1时就不能再分了。所以终止条件也有了。
对于操作,因为是乘二,所以并不影响奇偶性结果。
设一堆数目中二进制表示1的个数是m,则所有要操作的步骤就是所有的(m-1)加起来。讨论这个总数的奇偶性就行了。
为何分堆不影响二进制中1的总数的奇偶性?
考虑x的某一位p,分四种情况:
1. 如果x的第p位为1且k的第p位也为1,那么(x^k)的第p位就是0.
2. 如果x的第p位为1且k的第p位也为0,那么(x^k)的第p位就是1.
3. 如果x的第p位为0且k的第p位也为1,那么(x^k)的第p位就是1.
4. 如果x的第p位为0且k的第p位也为0,那么(x^k)的第p位就是0.
综上,此题可以解决。
如何统计二进制中1的个数?
可以对2不断取模进行,也可以使用n&(n-1)
后面的证明看博客https://blog.csdn.net/u013243347/article/details/52220551
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━━━━━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 1000000000
#define maxn 1005
#define maxm 100005
#define eps 1e-10
#define for0(i,maxn) for(int i=1;i<=(maxn);++i)
#define for1(i,maxn) for(int i=1;i<=(maxn);++i)
#define for2(i,x,y) for(int i=(x);i<=(y);++i)
#define for3(i,x,y) for(int i=(x);i>=(y);--i)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>'') {if(ch=='-') f=-;ch=getchar();}
while(ch>=''&&ch<='') {x=*x+ch-'';ch=getchar();}
return x*f;
}
int getsg(int n)
{
int count=;
while(n)
{
n&=(n-);
count++;
}
return count;
}
int main()
{
int T,n;
T=read();
int index=;
while(T--)
{
printf("Case %d: ",++index);
n=read();
int ans=;
for(int i=;i<=n;++i) ans+=(getsg(read())-);
if(ans&) puts("Yes");
else puts("No");
}
}
HDU 4388 Stone Game II {博弈||找规律}的更多相关文章
- HDU 4388 Stone Game II 博弈论 找规律
http://acm.hdu.edu.cn/showproblem.php?pid=4388 http://blog.csdn.net/y1196645376/article/details/5214 ...
- hdu 4388 Stone Game II sg函数 博弈
Stone Game II comes. It needs two players to play this game. There are some piles of stones on the d ...
- hdu 4388 Stone Game II
Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...
- HDU 1079 Calendar Game(博弈找规律)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题目大意:给你一个日期(包含年月日),这里我表示成year,month,day,两人轮流操作,每 ...
- hdu 2865 Polya计数+(矩阵 or 找规律 求C)
Birthday Toy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 题解报告:hdu 1564 Play a game(找规律博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1564 Problem Description New Year is Coming! ailyanlu ...
- 51nod_1831: 小C的游戏(Bash博弈 找规律)
题目链接 此类博弈不需要考虑sg函数,只需要确定必胜态和必败态,解题思路一般为打败先打表找规律,而后找规律给出统一的公式.打表方式:给定初始条件(此题中为ok[0]=ok[1]=0),然后从低到高枚举 ...
- Gym - 101775L SOS 博弈 找规律
题目:https://cn.vjudge.net/problem/Gym-101775L PS:训练赛中被这道题折磨的不轻,和队友反复推必胜态与必败态试图推导出公式或者规律,然后推的心态逐渐失控,,, ...
- HDU 4349 Xiao Ming's Hope 找规律
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- 三、Linux 系统目录结构
Linux 系统目录结构 登录系统后,在当前命令窗口下输入命令: ls / 你会看到如下图所示: 树状目录结构: 以下是对这些目录的解释: /bin:bin是Binary的缩写, 这个目录存放着最 ...
- jenkins+svn+pipeline+kubernetes部署java应用(三)
将jar包.Dockerfile.kubernetes部署yaml文件上传至svn自定义目录 一.生成流水线脚本 二.配置jenkins pipeline构建语句 三.点击构建java工程
- 15.VUE学习之-表单中使用key唯一令牌解决表单值混乱问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- char* 与char[]区别
[转] 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区(认为无区别),导致有时出现“难以理解”的错误.一时也不能说得很明白,网上也 ...
- Python中函数参数类型和参数绑定
参数类型 Python函数的参数类型一共有五种,分别是: POSITIONAL_OR_KEYWORD(位置参数或关键字参数) VAR_POSITIONAL(可变参数) KEYWORD_ONLY(关键字 ...
- Linux之ubuntu系统操作学习笔记
1,swp分区:当内存不够时用swp分区顶替内存 2,语言环境检查 locale –a:可以明白系统支持什么语言 3,安装软件: apt-cache search(软件):搜索软件 apt-cach ...
- Webapp和后端交互检查测试
除了功能,我们可以使用下面方法,查看交互过程,页面不能发现的问题: 什么是json 什么是json,json是什么,json如何使用 JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能 ...
- [python][django学习篇][10]再次修改博客首页模板
目前我们看到的只是模板中预先填充的一些数据,我们得让它显示从数据库中获取的文章数据.下面来稍微改造一下模板: 删除所有article标签,然后添加以下内容,将从数据库读取到的内容填充到模板变量{{ p ...
- excel模板解析前后设计变化,以及我对此的看法和感受
前言:近期也在做Excel模板的解析工作,目前来说,应该是第三版了.我最开始做的,就是垒鸡窝,虽然考虑了1.0提出的关于excel解析的一些建议和问题(单个模板),但是真的毫无设计可言.就几个工具类, ...
- koa2 + webpack 热更新
网上有很多express+webpack的热更新,但是koa2的很少,这两天研究了一下子,写一个简单的教程. 1.需要的包 webpack:用于构建项目 webpack-dev-middleware: ...