Begin by writing down a string of digits (numbers) that's as long or as short as you like. The digits can be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and appear in any combinations that you like. You don't have to use them all. Here is an example:
On a turn a player may either:
Change any one of the digits to a value less than the number that it is. (No negative numbers are allowed.) For example, you could change a 5 into a 4, 3, 2, 1, or 0.
Erase a zero and all the digits to the right of it.
The player who removes the last digit wins.
The game that begins with the string of numbers above could proceed like this:
Now, given a initial string, try to determine can the first player win if the two players play optimally both.
InputThe input consists of several test cases. For each case, there is a string in one line.
The length of string will be in the range of [1,6]. The string contains only digit characters.
Proceed to the end of file.
OutputOutput Yes in a line if the first player can win the game, otherwise output No.
Sample Input
0
00
1
20
Sample Output
Yes
Yes
No
No 思路:必败态可以转移到必胜态,sg搜一下。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAX=;
int sg[MAX];
int getlength(int n){
if(n/) return ;
if(n/) return ;
if(n/) return ;
if(n/) return ;
if(n/) return ;
return ;
}
void extend(int n){
int len=getlength(n);
for(int i=len;i>;i--){
int m=n;
int x=;
for(int j=;j<i;j++) x*=;
int index=(n%(x*))/x;
for(int j=index;j<;j++){
m+=x;
sg[m]=;
}
}
if(len<){
int m=n;
int x=;
for(int i=len;i<;i++)
{
m*=;
for(int i=;i<x;i++)
sg[m+i]=;
x*=;
}
}
}
void fun(){
memset(sg,,sizeof(sg));
sg[]=;
for(int i=;i<MAX;i++)
if(!sg[i])
extend(i);
}
int main(){
char str[];
int n;
fun();
while(scanf("%s",&str)!=EOF)
{
if(str[]=='') //第一个数字是0,则前者必胜
{
printf("Yes\n");
continue;
}
int len=strlen(str);//第一个数字非0,再转化成整型数
n=;
for(int i=;i<len;i++)
{
n*=;
n+=str[i]-'';
}
if(sg[n]) printf("Yes\n");
else printf("No\n");
}
return ;
}