【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 ...
随机推荐
- 在 Android* 商务应用中实施地图和地理围栏特性
摘要 本案例研究讨论了怎样将地图和地理定位特性构建到 Android* 商务应用中.包含在 Google Maps* 上覆盖商店位置,以及在设备进入商店地理围栏邻近区域时借助地理围栏通知用户. 文件夹 ...
- D3.js中对array的使用
由于D3类库和array密切相关,我们有必要讨论一下D3中的数据绑定以及在数组内部运算的方法. 1.D3中的数组 和其他编程语言一样,D3的数组元素可以是数字或者字符等类型,例如: someData= ...
- leetCode解题报告5道题(十)
题目一:Valid Number Validate if a given string is numeric. Some examples: "0" => true &quo ...
- Xcode6:No architectures to compile for(ONLY_ACTIVE_ARCH=YES...)
1.问题描写叙述 Xcode6真机測试旧project,不能执行,报错例如以下: 2.解决方式 问题非常明显,当前的执行模式设置了ONLY_ACTIVE_ARCH为Yes,当时的project的Val ...
- 28. Spring Boot配置方式
转自:https://blog.csdn.net/webzhuce/article/details/54564019
- 洛谷 P2430 严酷的训练
P2430 严酷的训练 题目背景 Lj的朋友WKY是一名神奇的少年,在同龄人之中有着极高的地位... 题目描述 他的老师老王对他的程序水平赞叹不已,于是下决心培养这名小子. 老王的训练方式很奇怪,他会 ...
- AlertDialog的onCreateDialog与onPrepareDialog用法
场景:在一个Activity中多次使用弹出对话框.而且对话框携带着动态变化的信息数据,这时假设仅仅使用onCreateDialog(int id, Bundle bundle)回调,则会发现第一次以后 ...
- 常用加密算法的Java实现总结(二)
常用加密算法的Java实现总结(二) ——对称加密算法DES.3DES和AES 摘自:http://www.blogjava.net/amigoxie/archive/2014/07/06/41550 ...
- Javascript和jquery事件--双击事件
在js中和jq中对应的命名都为dblclick,ondblclick,但是ondblclick和dom元素的属性相似,可以在行内设置,也可以使用attr设置. 同时,双击事件需要关注一个问题,那就是双 ...
- React-Native_02:语法篇
1.简单介绍 ECMAScript 6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式公布了.它的目标.是使得JavaScript语言能够用来编写复杂的大型应用程 ...