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
",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;
}