题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1404
1 #include<stdio.h> 2 #include<cstring> 3 using namespace std; 4 const int MAXN = 1000000; 5 int sg[MAXN]; 6 int get_lgt(int x){ 7 if(x/100000) return 6; 8 if(x/10000) return 5; 9 if(x/1000) return 4; 10 if(x/100) return 3; 11 if(x/10) return 2; 12 return 1; 13 } 14 void extend(int x){ 15 int lgt = get_lgt(x); 16 for( int i = lgt; i >= 1; --i){ 17 int m = x; 18 int base = 1; 19 for(int j = 1; j < i; ++j) 20 base *= 10; 21 int tmp = ( m % (base*10)) / base; 22 for(int j = tmp; j < 9; ++j){ 23 m += base; 24 sg[m] = 1;//越界了 25 } 26 } 27 //提取每一位的数字,遍历到9 28 if( lgt!= 6 ){ 29 int m = x; 30 int base = 1; 31 for( int i = lgt; i < 6;i++){ 32 m *= 10; 33 for(int j = 0; j < base; ++j) 34 sg[m+j] = 1; 35 base *= 10; 36 } 37 } 38 } 39 // sg[0] = 1;N态 sg[1] = 0;P态 40 // 终态是P态,可以移动到P态的是N态,所有移动都会导致N态的是P态 41 void init(){ 42 memset(sg,0,sizeof(sg)); 43 sg[0] = 1; 44 for(int i = 1; i < MAXN; ++i) 45 if(!sg[i])//N态 46 extend(i); 47 } 48 int main(){ 49 freopen("test.out","r",stdin); 50 freopen("mtest.out","w",stdout); 51 init(); 52 char str[8]; 53 int lgt; 54 int n; 55 while(~scanf("%s",&str)){ 56 n = 0; 57 if( str[0]==‘0‘ ) 58 printf("Yes\n"); 59 else{ 60 lgt = strlen(str); 61 for(int i = 0; i < lgt; ++i){ 62 n *= 10; 63 n += str[i]-‘0‘; 64 }//字符串转化为整数 65 if(sg[n]) 66 printf("Yes\n"); 67 else 68 printf("No\n"); 69 } 70 } 71 }
时间: 2025-01-01 20:54:27