HDU4388:Stone Game II(博弈+思维)
Stone Game II
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 609 Accepted Submission(s): 350
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4388
Descripton:
Stone Game II comes. It needs two players to play this game. There are some piles of stones on the desk at the beginning. Two players move the stones in turn. At each step of the game the player should do the following operations.
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.
Input:
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.
Output:
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).
Sample Input:
3 2 1 2 3 1 2 3 4 1 2 3 3
Sample Output:
Case 1: No Case 2: Yes Case 3: No
题意:
给出n堆石子,每堆石子都有一定的个数,然后现在两个人来取石子。假设当前这堆石子个数为n,那么最终他留下了k个石子,必须满足0<k<n且n^k<n。之后再加上一堆数量为n^k的石子。
每个人有一次机会,加上一堆个数为(2*k)^n的石子。最后问是否先手必赢。
题解:
由题意我们可以知道,最终经过一次拆分过后,会有两堆石子,一堆个数为k,另一堆个数为n^k,并且可以进一步推知,如果有一堆石子的个数刚好为2^x,那么此时就是不可再分了。
由于这里是多堆石子不好分析,我们可以先考虑石子只有一堆的情况,通过对拆分后两堆石子个数二进制的分析,我们可以知道,拆分后的两堆石子二进制中1的个数的奇偶性和原来那个堆二进制中1的个数是一致的。假设我们最后拆分为了m堆,已经到达不可再分的状态,此时操作了m-1次,现在假设n中二进制个数为x,那么这里x和m是同奇偶的,如果最后我们要得知谁赢,只需要知道操作次数的奇偶性就行了。而这里m比较难得到,但是k是很容易知道的,所以我们就通过对k-1的奇偶性的判断就可以知道操作次数的奇偶数,这个时候一堆石子的情况就比较显然了。
这里可能会有技能,就是多出来的那一堆的个数为(2*k)^n,但是这里我们其实可以知道这一堆1的奇偶性也并未改变。对于每一位分情况分析一下就行了。所以其实这个技能的存在并不影响我们的分析。
多堆石子的情况下,也是同样的道理,对奇偶性的情况进行分析了,把所有堆操作次数的奇偶性计算出来就行了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = ;
int T,n;
int a[N];
int main(){
ios::sync_with_stdio(false);cin.tie();
cin>>T;
int Case = ;
while(T--){
Case++ ;
cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
int cnt = ;
for(int i=;i<=n;i++){
for(int j=;j>=;j--) if(a[i]&(<<j)) cnt++;
}
cout<<"Case "<<Case<<": ";
if((cnt+n)&) cout<<"Yes"<<'\n';
else cout<<"No"<<'\n';
}
return ;
}
HDU4388:Stone Game II(博弈+思维)的更多相关文章
- HDU 4388 Stone Game II {博弈||找规律}
Stone Game II Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- [hdu4388]Stone Game II
不管是否使用技能,发现操作前后所有堆二进制中1的个数之和不变.那么对于一个堆其实可以等价转换为一个k个石子的堆(k为该数二进制的个数),然后就是个nim游戏. 1 #include<bits/s ...
- 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个物品,从中选择 ...
- Leetcode--Last Stone Weight II
Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...
- Codeforces Round #573 (Div. 2) D. Tokitsukaze, CSL and Stone Game (博弈,思维)
D. Tokitsukaze, CSL and Stone Game time limit per test1 second memory limit per test256 megabytes in ...
- poj 1740 A New Stone Game(博弈)
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5338 Accepted: 2926 ...
- UVALive 3668 A Funny Stone Game(博弈)
Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2,..., ...
- Forethought Future Cup - Final Round (Onsite Finalists Only) C. Thanos Nim 题解(博弈+思维)
题目链接 题目大意 给你n堆石子(n为偶数),两个人玩游戏,每次选取n/2堆不为0的石子,然后从这n/2堆石子中丢掉一些石子(每一堆丢弃的石子数量可以不一样,但不能为0),若这次操作中没有n/2堆不为 ...
随机推荐
- 拉格朗日乘子法与KKT条件 && SVM中为什么要用对偶问题
参考链接: 拉格朗日乘子法和KKT条件 SVM为什么要从原始问题变为对偶问题来求解 为什么要用对偶问题 写在SVM之前——凸优化与对偶问题 1. 拉格朗日乘子法与KKT条件 2. SVM 为什么要从原 ...
- golang笔记1
golang笔记1 go代码是用包来组织的,每个包有一个或多个go文件组成,这些go文件文件放在一个文件夹中 每个源文件开始都用一个package声明,指明本源文件属于哪个包 pakage声明后紧跟这 ...
- hbase Problem binding to node1/192.168.1.13:16020 : 地址已在使用
这是hbase 从0.9.x升级到1.x后HMaster与HRegionServer端口冲突问题 在hbase升级到1.0.0版本后,默认端口做了改动.其中16020端口是hmaster服务和hreg ...
- [python][odlboy]设置字符串打印的颜色
格式:\033[显示方式;前景色;背景色m 说明:前景色 背景色 颜色---------------------------------------30 ...
- scrum立会报告+燃尽图(第三周第四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2286 项目地址:https://coding.net/u/wuyy694 ...
- 冲刺ing-7
第七次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 Leangoo的看板截图,燃尽图 蔺皓雯 编写博客 蔡晨旸 进行测试 曾茜 界面设计 鲁婧楠 界面前后端 杨池宇 界面前后端 项目的发布说 ...
- 1.2Linux下C语言开发基础(学习过程)
===============第二节 Linux下C语言开发基础=========== ********************** 重要知识点总结梳理********************* 一 ...
- our team
今天向大家介绍一下我们的团队,首先我们的团队叫“吉祥三宝”当然我们的三宝不是亲子关系,我们是兄弟关系,对,就是这样 下面来介绍一下我们的团队成团吧: 李奇原: 性格开朗.积极乐观.有责任心,擅长团队协 ...
- 操作系统之实验二Step1-有序顺序表
实验二Step1-有序顺序表 专业:商业软件工程 班级:商软2班 姓名:甘佳萍 学号:201406114207 实验要求:初始化 输入数组元素个数. 输入n个数,排序输出. 存 ...
- Alpha阶段事后诸葛亮分析
事后诸葛亮分析 一.设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件可供各类人群闲暇时间消遣娱乐,锻炼脑力. 定义的很清楚,就是一个定位 ...