传送门:HDU 1404
Problem Description
Digital deletions is a two-player game. The rule of the game is as following.
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.
Input
The 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.
Output
Output 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
题意:
给一个长度不超过6字符的数字字符串。两种操作二选一:
1、把任意一位变成比他本身小的数字。比如205,可以把5变成0,1,2,3,4,成了200,201…。
2、把任意一个0后及他本身去掉。比如205,去掉2和他后面的数字变成了2。
问最后去掉数字的算赢。问先手有木有必胜策略。
题解:
我们可以用sg函数把0~1e6的所有数的状态表示出来。
容易得到sg[0]=1,sg[1]=0.
我们可以通过已知的先手必败状态推出他的所有先手必胜状态。只要是可以一步到达sg=0的状态的状态都是必胜状态。
那么怎么找必胜点?
1.把比输状态的每一位+1,直到加到9,这些状态都是必胜状态。(通过操作一得)
2.如果位数小于6,可以在末尾+0,再加上各个数,这些数都是必胜状态。(通过操作二得)
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;
const int N=1000000;
int sg[N];
char s[10];
int getw(int x)//求位数
{
if(x/100000) return 6;
if(x/10000) return 5;
if(x/1000) return 4;
if(x/100) return 3;
if(x/10) return 2;
return 1;
}
void SG(int x)
{
int len=getw(x);
for(int i=len;i>=1;i--)
{
int m=x;
int base=1;
for(int j=1;j<i;j++) base*=10;
int temp=(m%(base*10))/base;
for(int j=temp;j<9;j++)//当前数的上一个到9都是必胜状态
{ //如1666为必输状态,那么1766,1866,1966,1676,1686,1696,1667...都为必胜状态
m+=base;
sg[m]=1;
}
}
if(len!=6)//没有6位在后面加0
{
int m=x;
int base=1;
for(int i=len;i<6;i++)
{
m*=10;
for(int j=0;j<base;j++)
{
sg[m+j]=1;
}
base*=10;
}
}
}
//sg[n]=0表示先手必输,那么凡是可以一步到达这个状态的都是必赢的状态即sg[m]=1
void f()//预处理
{
memset(sg,0,sizeof(sg));
sg[0]=1;
for(int i=1;i<N;i++)
{
if(!sg[i]) SG(i);
}
return;
}
int main()
{
f();
while(scanf("%s",s)!=EOF)
{
int len=strlen(s);
if(s[0]=='0')//如果首个字符为0,那么先手直接获胜
{
printf("Yes\n");
continue;
}
int m=0;
for(int i=0;i<len;i++)
{
m*=10;
m+=s[i]-'0';
}
if(sg[m]) printf("Yes\n");
else printf("No\n");
}
return 0;
}