hdu 1404 Digital Deletions (SG函数)

Digital Deletions

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2656 Accepted Submission(s): 951

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

题意:给你一个串,你每次可以选择一个操作:
1.擦除字符0及其右边的字符
2.将一个字符变为比其本身小的字符,例如9可以变为0-8,谁擦完谁赢

思路:因为位数最多有6位,所以说我们可以暴力打表求出每个数sg值,因为不是Nim博弈,所以说sg直接设置为0和1就好了,如果当前点能到达一个必败点,那么此点为必胜点,即为1,同理,必败点为0,枚举每一位可以走的状态即可,注意像01这种的情况,加个特判就好了。

ac代码:

/* ***********************************************
Author       : AnICoo1
Created Time : 2016-08-07-17.35 Sunday
File Name    : D:\MyCode\2016-8\2016-8-6-game.cpp
LANGUAGE     : C++
Copyright  2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int a[20];
int v[MAXN];
int sg[MAXN];
int getSG(int x)
{
    int y=x;int cnt=1;
    while(y)
        a[cnt++]=y%10,y/=10;
    cnt--;
    int flag=0;
    for(int i=1;i<=cnt;i++)
    {
        if(a[i]==0)
        {
            int z=0;
            for(int j=cnt;j>i;j--)
                z=z*10+a[j];
            if(sg[z]==0)
            {
                flag=1;
            }
        }
        else
        {
            for(int j=i==cnt?1:0;j<a[i];j++)
            {
                int z=0;
                for(int k=cnt;k>=1;k--)
                {
                    if(k==i) z=z*10+j;
                    else z=z*10+a[k];
                }
                if(sg[z]==0)
                {
                    flag=1;
                    break;
                }
            }
        }
        if(flag)
            break;
    }
    if(flag) return 1;
    else return 0;
}
int main()
{
    int n;mem(sg,-1);sg[0]=1;sg[1]=0;
    for(int i=3;i<=1000000;i++)
    {
        sg[i]=getSG(i);
    }
    char str[10];
    while(scanf("%s",str)!=EOF)
    {
        int n=0;
        int len=strlen(str);
        if(str[0]=='0')
        {
            printf("Yes\n");
            continue;
        }
        for(int i=0;i<len;i++)
            n=n*10+str[i]-'0';
        if(sg[n]) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值