【hdu 3537】Daizhenyang's Coin
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 677 Accepted Submission(s): 321
Problem Description
We know that Daizhenyang is chasing a girlfriend. As we all know, whenever you chase a beautiful girl, there’ll always be an opponent, or a rival. In order to take one step ahead in this chasing process, Daizhenyang decided to prove to the girl that he’s better and more intelligent than any other chaser. So he arranged a simple game: Coin Flip Game. He invited the girl to be the judge.
In this game, n coins are set in a row, where n is smaller than 10^8. They took turns to flip coins, to flip one coin from head-up to tail-up or the other way around. Each turn, one can choose 1, 2 or 3 coins to flip, but the rightmost selected must be head-up before flipping operation. If one cannot make such a flip, he lost.
As we all know, Daizhenyang is a very smart guy (He’s famous for his 26 problems and Graph Theory Unified Theory-Network Flow does it all ). So he will always choose the optimal strategy to win the game. And it’s a very very bad news for all the competitors.
But the girl did not want to see that happen so easily, because she’s not sure about her feelings towards him. So she wants to make Daizhenyang lose this game. She knows Daizhenyang will be the first to play the game. Your task is to help her determine whether her arrangement is a losable situation for Daizhenyang.
For simplicity, you are only told the position of head-up coins. And due to the girl’s complicated emotions, the same coin may be described twice or more times. The other coins are tail-up, of course.
Coins are numbered from left to right, beginning with 0.
Input
Multiple test cases, for each test case, the first line contains only one integer n (0<=n<=100), representing the number of head-up coins. The second line has n integers a1, a2 … an (0<=ak<10^8) indicating the An-th coin is head up.
Output
Output a line for each test case, if it’s a losable situation for Daizhenyang can, print “Yes”, otherwise output “No” instead.
Sample Input
0
1
0
4
0 1 2 3
Sample Output
Yes
No
Yes
【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=3537
【题解】
首先要明白一些事情,不然没法做;
有这样的结论:局面的SG 值为局面中每个正面朝上的棋子单一
存在时的SG 值的异或和。即一个有k个硬币朝上,朝上硬币位置
分布在的翻硬币游戏中,SG值是等于k个独立的开始时只有一个硬
币朝上的翻硬币游戏的SG值异或和。比如THHTTH这个游戏中,2号、
3号、6号位是朝上的,它等价于TH、TTH、TTTTTH三个游戏和,
即sg[THHTTH]=sg[TH]^sg[TTH]^sg[TTTTTH].
我们的重点就可以放在单个硬币朝上时的SG值的求法。
所以现在只考虑单个硬币朝上的(最后一个硬币正面朝上,其他都是反面朝上);
下面为方便起见用0代表反面朝上,1代表正面朝上
N=0
先手输->sg[0]=0;
N=1
只有一个硬币的时候
即
1
则先手赢;
sg[1]=1;
N=2
即
01
这里
01可以通过一次操作变成{00,10}
对于00其对应N=0的sg,即sg[0]=0;
对于10其对应N=1的sg,即sg[1]=1;
(不要忘了上面整个游戏可以分解成若干个小游戏的思想);
则sg[2]=mex(sg[0],sg[1]);(mex是不包括集合里面的数的最小整数);
sg[2]=2;
N=3
即
001
这里001可以通过一次操作变成{000,010,100,110}
000对应N=0,sg[0]=0;
010对应N=2,sg[2]=2;
100对应N=1,sg[1]=1;
110分解为N=1和N=2的情况,sg=sg[1]^sg[2]=3;
所以
sg[3]=mex{sg[0],sg[2],sg[1],3}=4
N=4
即
0001
0001可以通过一次操作变成{0000,0010,0100,1000,0110,1010,1100}
①0000->sg[0]=0
②0010->sg[3]=4
③0100->sg[2]=2
④1000->sg[1]=1
⑤0110->sg=sg[2]^sg[3]=6
⑥1010->sg=sg[1]^sg[3]=5
⑦1100->sg=sg[1]^sg[2]=3
所以sg[4] = mex{0,4,2,1,6,5,3}=7
到了这里我们可以依据这个规则写出打表程序(在题解下方)
得到如下数据
sg[0]=0
sg[1]=1
sg[2]=2
sg[3]=4
sg[4]=7
sg[5]=8
sg[6]=11
sg[7]=13
sg[8]=14
sg[9]=16
sg[10]=19
sg[11]=21
sg[12]=22
sg[13]=25
sg[14]=26
sg[15]=28
sg[16]=31
sg[17]=32
sg[18]=35
sg[19]=37
sg[20]=38
可以看到N=x的时候
sg[x]要么等于2*(x-1)+1要么等于2*(x-1)
直接给出结论了
当x-1的二进制形式里面有奇数个1的时候,sg[x]=2*(x-1);
有偶数个1的时候sg[x]=2*(x-1)+1;
且sg[0]=0;
但是这里题目给的下标是从0开始的;
所以上面得到的sg下标要移位一下
sg[0]=1
sg[1]=2
sg[2]=4
sg[3]=7 ->下标为3代表的是N=4的情况;
…
这样就更方便了:
直接看所给的下标x的二进制形式里面有多少个1;
为奇数则sg[x]=2*x;否则sg[x]=2*x+1
最后看看所有的sg函数的异或值是多少;
为0则先手败,否则先手赢;
注意在这个题目里,先手败是输出Yes!
**不要忘了硬币的位置可能重复,最毒XX心:(
【打表程序】↓↓↓
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int sg[200];
bool flag[200];
string s;
int now;
int main()
{
freopen("F:\\rush.txt","r",stdin);
freopen("F:\\rush_out.txt","w",stdout);
sg[0] = 0;sg[1] = 1;
s = "01";
now = 2;
while (now <= 50)
{
memset(flag,false,sizeof flag);
string ts = s;
int len = ts.length();
ts[len-1] = '0';
flag[0] = true;
rep1(i,0,len-2)
{
ts[i]='1';
flag[sg[i+1]] = true;
ts[i]='0';
}
rep1(i,0,len-3)
{
ts[i]='1';
rep1(j,i+1,len-2)
{
ts[j]='1';
flag[sg[i+1]^sg[j+1]] = true;
ts[j]='0';
}
ts[i]='0';
}
rep1(j,0,200)
if (!flag[j])
{
sg[now] = j;
break;
}
s='0'+s;
now++;
}
rep1(i,0,20)
printf("sg[%d]=%d\n",i,sg[i]);
fclose(stdin);
fclose(stdout);
return 0;
}
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int n;
map <LL,bool> dic;
bool judge(LL x)
{
int cnt = 0;
while (x > 0)
{
if (x&1)
cnt++;
x>>=1;
}
return cnt&1;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
while (~scanf("%d",&n))
{
dic.clear();
LL ans = 0;
rep1(i,1,n)
{
LL x;
rel(x);
if (dic[x]) continue;
dic[x] = true;
if (judge(x))
ans = ans ^ (2*x);
else
ans = ans^(2*x+1);
}
if (ans==0)
puts("Yes");
else
puts("No");
}
return 0;
}
【hdu 3537】Daizhenyang's Coin的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 3951】Coin Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
随机推荐
- 《开源公开课分享》:Java开源框架案例分享
缺乏高端技术人才?缺乏开发标准? 代码复用性低?技术风险难于把控? 招聘成本高?培训成本高? 假设想法不够雄伟,那么就会局限于细节:假设一開始就铺很大的摊子,将会失去控制: ...
- Android中的帧动画与补间动画的使用
前言 在日常开发中,我们有时候须要一些好看的动画效果,这时能够充分利用Android提供的这几种动画来实现. Android提供了3种类型的动画: 补间动画:补间动画能够应用于View,让你能够定义一 ...
- win10系统64位安装git后右键运行git bash here生成一个mintty.exe.stackdump文件后闪退解决方案
在其他win10电脑上复制了一个null.sys文件,替换C:\Windows\System32\drivers\null.sys,搞定.
- 国内计算机类期刊 SCI收录:
国内计算机类期刊 SCI收录: JOURNAL OF COMPUTER SCIENCE AND TECHNOLOGY,计算机科学与技术,英文,双月刊, SCIE 国内计算机类期刊 EI收录: 核心类 ...
- Java Web学习总结(8)——使用Cookie进行会话管理
一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...
- 关于C++中用两个迭代器方式初始化string的知识
string(iter1, iter2); 第一点:两个迭代器必须指向同一个容器. 第二点:iter2必须>=iter1. 第三点:假设iter1等于iter2,那么结果为空[] 另外一个比較特 ...
- UML学习总结(2)——StartUML 各种类图的例子
1.UML分为: 1)静态建模:系统基础和系统固定框架结构,这些图形往往是"静态"的. 类图(Class Diagram):常用来分析业务概念 用例图(Use Case Diagr ...
- Android java取得实时上周的时间
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Te ...
- Asp.NETCore让FromServices回来
起因 这两天,我忽然有点怀念 Asp.NET MVC 5 之前的时代,原因是我看到项目里面有这么一段代码(其实不止一段,几乎每个 Controller 都是) [Route("home&qu ...
- android权限详细
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...