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堆不为 ...
随机推荐
- 会声会影2018提示dll文件丢失怎么办?
一些会声会影2018用户,在安装.使用软件的过程中,会出现dll缺失的提示,导致软件无法打开,那么,出现这一问题要怎么解决.接下来小编为大家具体介绍下两种解决方法. 图1:dll丢失提示 打开会声会影 ...
- hdu - 6282,2018CCPC湖南全国邀请赛G题,字符串,规律
HDU – 6282 http://acm.hdu.edu.cn/showproblem.php?pid=6282 by Hzu_Tested 题意:给出两个字符串S和T,只由a,b,c三种字符组成( ...
- Tensorflow - Implement for a Convolutional Neural Network on MNIST.
Coding according to TensorFlow 官方文档中文版 中文注释源于:tf.truncated_normal与tf.random_normal TF-卷积函数 tf.nn.con ...
- Blockchain For Dummies(IBM Limited Edition
Blockchain For Dummies(IBM Limited Edition)笔记 该系列内容主要介绍用于商业的区块链,有人说区块链之于贸易,犹如因特网之于信息.在商业领域区块链可以用于交易任 ...
- emmmmmm
211606342杨艺勇 211606379王熙航 单元测试 对每一个代码块进行测试,返回测试结果并和预期结果进行比对 对源代码进行相应的重构,以适应测试代码的调用,且不影响源代码的正常运行 通过与构 ...
- C++:构造函数1——普通构造函数
前言:构造函数是C+中很重要的一个概念,这里对其知识进行一个简单的总结 一.构造函数的定义 1.类中的构造函数名与类名必须相同 2.构造函数没有函数的返回类值型说明符 [特别注意]: a.构造函数的返 ...
- 福大软工1816:Beta(4/7)
Beta 冲刺 (1/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 准备四六级 展示GitHub当日代码/ ...
- 201621123037 《Java程序设计》第7周学习总结
作业06-接口.内部类 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 答: 思维导图: 其他-笔记: 2. 书面作业 1. ArrayList代码分析 1.1 解释Arr ...
- ubuntu 手动apache记录
1.下载apache tar -xvzf httpd.xx 解压 2.下载安装pcre Download PCRE from PCRE.org 解压,进入文件夹中 ./configure --pre ...
- virtio是啥子
这个山头今天好像要攻占下来了 guest os中的一些特权操作会被hypervhisor给接收,这里一个很重要的认识是:hypervisor是os的os,既然要访问资源,那么就需要经过整机资源的管理者 ...