Be the Winner
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3383 Accepted Submission(s): 1891
Problem Description
Let's consider m apples divided into n groups. Each group contains no more than 100 apples, arranged in a line. You can take any number of consecutive apples at one time.
For example "@@@" can be turned into "@@" or "@" or "@ @"(two piles). two people get apples one after another and the one who takes the last is
the loser. Fra wants to know in which situations he can win by playing strategies (that is, no matter what action the rival takes, fra will win).
For example "@@@" can be turned into "@@" or "@" or "@ @"(two piles). two people get apples one after another and the one who takes the last is
the loser. Fra wants to know in which situations he can win by playing strategies (that is, no matter what action the rival takes, fra will win).
Input
You will be given several cases. Each test case begins with a single number n (1 <= n <= 100), followed by a line with n numbers, the number of apples in each pile. There is a blank line between cases.
Output
If a winning strategies can be found, print a single line with "Yes", otherwise print "No".
Sample Input
2 2 2 1 3
Sample Output
No Yes
Source
题解:Anti-SG游戏+Multi-SG游戏
题目大意:有n排连在一起的石子,每次可以从任意一排中拿走连续的一段石子,拿走最后一颗石子的人输。
拿走一段后,相当于把一排石子分成了互补影响的两排,这符合Multi-SG游戏
因为是拿走最后一颗的人输,所以我们直接利用Anti-SG游戏的结论:
SJ定理:对于任意一个Anti-SG游戏,我们规定当局面中所有的单一游戏的SG值为0时,游戏结束,则先手必胜当且仅当
(1)游戏的SG函数不为0且游戏中某个单一游戏的SG函数大于1
(2)游戏的SG函数为0且没有单一游戏的SG函数大于1.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 203
using namespace std;
int sg[N],n;
int get(int x)
{
if (x<0) return 0;
if (sg[x]!=-1) return sg[x];
int hash[N];
memset(hash,0,sizeof(hash));
for (int i=1;i<=x;i++)
for (int j=1;i+j-1<=x;j++)
hash[get(i-1)^get(x-(i+j-1))]=1;//cout<<i-1<<" "<<x-(i+j-1)<<endl;
//cout<<"!"<<x<<endl;
for (int i=0;i<=x;i++)
if (!hash[i]) {
sg[x]=i;
return sg[x];
}
}
int main()
{
freopen("a.in","r",stdin);
memset(sg,-1,sizeof(sg));
sg[0]=0;
while (scanf("%d",&n)!=EOF) {
int ans=0; bool pd=false;
for (int i=1;i<=n;i++) {
int x; scanf("%d",&x);
get(x);
ans^=sg[x];
if (sg[x]>1) pd=true;
}
//for (int i=1;i<=3;i++) cout<<sg[i]<<" ";
//cout<<endl;
if (ans&&pd||!ans&&!pd) printf("Yes\n");
else printf("No\n");
}
}