HDU4388:Stone Game II(博弈+思维)

时间:2022-03-24 00:03:36

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(博弈+思维)的更多相关文章

  1. HDU 4388 Stone Game II &lbrace;博弈&vert;&vert;找规律&rcub;

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. &lbrack;hdu4388&rsqb;Stone Game II

    不管是否使用技能,发现操作前后所有堆二进制中1的个数之和不变.那么对于一个堆其实可以等价转换为一个k个石子的堆(k为该数二进制的个数),然后就是个nim游戏. 1 #include<bits/s ...

  3. 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 ...

  4. hdu 4388 Stone Game II

    Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...

  5. Codeforces Round &num;573 &lpar;Div&period; 2&rpar; D&period; Tokitsukaze&comma; CSL and Stone Game (博弈,思维)

    D. Tokitsukaze, CSL and Stone Game time limit per test1 second memory limit per test256 megabytes in ...

  6. poj 1740 A New Stone Game(博弈)

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5338   Accepted: 2926 ...

  7. UVALive 3668 A Funny Stone Game(博弈)

    Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2,...,  ...

  8. Forethought Future Cup - Final Round &lpar;Onsite Finalists Only&rpar; C&period; Thanos Nim 题解(博弈&plus;思维)

    题目链接 题目大意 给你n堆石子(n为偶数),两个人玩游戏,每次选取n/2堆不为0的石子,然后从这n/2堆石子中丢掉一些石子(每一堆丢弃的石子数量可以不一样,但不能为0),若这次操作中没有n/2堆不为 ...

  9. HDU 5996:dingyeye loves stone(阶梯博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5996 题意:在一棵树上进行博弈,每次只能将当前的结点的石子放到父节点上,最后不能移动的输. 思路:比赛的时候想的 ...

随机推荐

  1. JSP 和 ASP&period;NET 谁能主宰未来【转】

    随着计算机行业的发展,以后到底谁才是 web 网站开发的主宰者呢? 1. 说说JSP.(本人工作中用的最多的就是JSP) JSP. JavaServer Pages 是Java技术的一部分,可以说是J ...

  2. 随便2--struct pointer

    同为struct,如果struct中没有指针, C 和C++可以用等号赋值,但是一旦里面涉及到指针,就不能用等号,要用memcpy struct A{char v1[20];int v2;} a,b; ...

  3. Java截图笔记

  4. 微信公众平台JSSDK开发

    根据微信开发文档步骤如下: 1.先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”.JS接口安全域名设置 mi.com(前面不用带www/http,域名必须备案过) 2.引入 ...

  5. xml基本操作

    在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...

  6. linq to xml 基本操作

    使用XDocument类来进行简单的xml操作,用于软件参数的设置保存. using System; using System.Collections.Generic; using System.Li ...

  7. iOS第三方(显示视图的宽度高度)- MMPlaceHolder

    github:https://github.com/adad184/MMPlaceHolder#readme appDelegate添加,影响全局 [MMPlaceHolderConfig defau ...

  8. InnoDB的redo日志管理---饶珑辉

    http://raolonghui.com/2015/06/24/innodb%E7%9A%84redo%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86/#comment-11

  9. &&num;39&semi;mysql&&num;39&semi; 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    1.C:\Users\Aiyufei>mysql -h 127.0.0.1 -u root'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件.解决方法: 配置环境变量即可,我的问 ...

  10. 360自带--JS开发工具箱

    360自带–JS开发工具箱 360自带–JS开发工具箱 360自带–JS开发工具箱