Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=1404

刚开始想采取找规律的方法解题,可以没有发现规律。无奈,只好采用求PN点的方法。

我们假设数字最右边为第一位,且为最低位。那么可以知道当最高位为0时,那么先手的人必定能胜利,这个可以作为一个剪枝条件。

接下来就是两种操作了:1.降低其中任何一位

             2.把其中一个0及这位0后面所有的数字删去

我们只需通过这两个操作,寻找目标数字的后续数字状态的PN状态,即可求得目标数字的PN状态。其实我们只需判断后续状态中有没有P点就行了,如果有P点,那么目标数字的状态为N点。如果没有N点,那么目标数字的状态为P点。通过这方法,就可以求得题目要求数字的PN状态了。

#include <iostream>
#include <cstring>
#include <sstream>
#include <cstdio> using namespace std; const int MAXN = 1000000 + 5;
int sg[MAXN]; stringstream ss;
int length ; inline int Get_digit( int num, int i ) // 求出数字第i位数,最右边为第1位
{
switch (i){
case 1: return num%10;
case 2: return num%100/10;
case 3: return num%1000/100;
case 4: return num%10000/1000;
case 5: return num%100000/10000;
default : return num/100000;
};
} int Get_sg( int num ) // 求sg值
{
if( sg[num]!=-1 )
return sg[num];
int temp = 1;
int orignal = num;
int i;
for( i=0;i<length-1;i++ ) // 缩小第i位的值
{
num = orignal;
if( Get_digit( num, i+1 )==0 )
{ // 消去0即后面所有的数
if( Get_sg( num/( temp*10 ) )==0 ) // 发现必败点
{
sg[ orignal ] = 1; // 则此点为必胜点
return 1;
}
}
num = orignal;
while( Get_digit( num, i+1)!=0 )
{
num = num - temp;
if( Get_sg( num )==0 ) // 发现必败点
{
sg[ orignal ] = 1; // 则此点为必胜点
return 1;
}
}
temp = temp * 10;
} num = orignal;
while( Get_digit(num, length)>1 ) // 最高位单独处理
{ // 避免出现 类似于 123 -> 23 的情况
num = num - temp;
if( Get_sg( num )==0 ) // 发现必败点
{
sg[ orignal ] = 1; // 则此点为必胜点
return 1;
}
}
sg[orignal] = 0; // 没有发现必败点,则此点即为必败点
return 0;
} int main()
{
memset( sg, -1,sizeof(sg) ); // 初始化
char in[10];
int num, ans;
sg[0] = 1;
sg[1] = 0; // 初始化
while( scanf("%s", in)!=EOF )
{
if( in[0]=='0' ) // 最高为为0, 则一定胜利
{
printf( "Yes\n" );
continue;
}
ss.clear();
ss << in;
ss >> num; // char转int
length = strlen(in); // 求该数字的长度
ans = Get_sg( num );
if( ans )
{
printf( "Yes\n" );
}
else
{
printf( "No\n" );
}
}
return 0;
}

Hdu 1404 Digital Deletions的更多相关文章

  1. HDU 1404 Digital Deletions (暴力博弈)

    题意:给定一个数字串,最长是6,然后有两种操作. 第一种是,把该串中的一个数字换成一个比该数字小的数,比如 5 可以换成 0,1,2,3,4.   e.g. 12345 --> 12341 第二 ...

  2. hdoj 1404 Digital Deletions(博弈论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1404 一看就是博弈论的题目,但并没有什么思路,看了题解,才明白 就是求六位数的SG函数,暴力一遍,打表 ...

  3. hdu 1404 找sg ***

    HDU 1404  Digital Deletions 一串由0~9组成的数字,可以进行两个操作:1.把其中一个数变为比它小的数:2.把其中一个数字0及其右边的所以数字删除. 两人轮流进行操作,最后把 ...

  4. Digital Deletions HDU - 1404

    Digital deletions is a two-player game. The rule of the game is as following. Begin by writing down ...

  5. hdu 1404/zoj 2725 Digital Deletions 博弈论

    暴力打表!! 代码如下: #include<iostream> #include<algorithm> #include<cstdio> #include<c ...

  6. HDU 1404 (博弈) Digital Deletions

    首先如果第一个数字是0的话,那么先手必胜. 对于一个已知的先手必败状态,凡是能转移到先手必败的状态一定是必胜状态. 比如1是必败状态,那么2~9可以转移到1,所以是必胜状态. 10,10*,10**, ...

  7. HDU 1013 Digital Roots【字符串,水】

    Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. HDU 1013 Digital Roots(to_string的具体运用)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...

  9. HDU 4394 Digital Square

    Digital Square Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. 基于Visual C++2013拆解世界五百强面试题--题18-程序结果分析2-终结篇

    第二部分程序结果分析,分析流程还是写入代码注释中 分析下面程序的输出: #include <stdio.h> int main() { char *a = "hello" ...

  2. js遍历对象的属性并且动态添加属性

    var person= { name: 'zhangsan', pass: '123' , 'sni.ni' : 'sss', hello:function (){ for(var i=0;i< ...

  3. poj 2155

    题目链接 二维树状数组 给出矩阵左上角和右下角坐标,矩阵里的元素 1变0 ,0 变1,然后给出询问,问某个点是多少. 树状数组下标不能为0 二维的每次更新都是从(1,1)到(x,y) 要更新(x1,y ...

  4. linux下TUN/TAP虚拟网卡的使用

    转载:http://wushank.blog.51cto.com/3489095/1306849 tun/tap 驱动程序实现了虚拟网卡的功能,tun表示虚拟的是点对点设备,tap表示虚拟的是以太网设 ...

  5. eclipse主题插件

    打开eclipse ,选择 Help 选择Install New Software 点击 Add 输入http://eclipse-color-theme.github.com/update,选中Ec ...

  6. Orchard 添加搜索栏

    Orchard 提供索引和搜索的功能. 索引功能需要开启 Indexing 模块, 同时我们要开启Lucene 模块(做实际检索工作的东西). 然后还要开启Search模块(调用Lucene 查询然后 ...

  7. md5 加密 swfit版

    在swift工程中随便建一个objective-c类,会提示你生成一个Bridging-Header,点YES,然后删除刚才建立的objective-c类,只留下[工程名]-Bridging-Head ...

  8. 去掉word2007回车后自动编号

    1. 在使用word自动编号时,回车续写上一编号内容可能会行距很宽,这时候使用shift+回车不会自动下一编号,而且是正常行距 取消回车自动编号: 打开Word文档,单击菜单栏上的[格式]→[自动套用 ...

  9. sdl2-2.04 读取位图并显示

    // sdl2_win32.cpp : Defines the entry point for the console application.//// 假定SDL的库文件和头文件和VC的工程文件在一 ...

  10. 《JavaScript权威指南》拾遗(上)

    一.语言基础         1.javascript中,只有null和undefined是无法拥有方法的值,它们都没有包装对象.typeof null == ‘object' , typeof un ...