51nod 1831 小C的游戏

小C和小L是好朋友,她们在玩一个游戏。 
一开始有一个大小为n的石子堆,小C先手。 
每次可以对这个石子堆拿走一个或者把这个石子堆分成等量的几份并只取其中一份(不能不变或只剩下一个)。 
如果取走最后一个人的算败,请问这个游戏小C是否能胜。 
Input一行表示数据组数Tcases(Tcases<=1,000)。 
后面Tcases行每行一个n(n<=1,000,000,000)。Output有Tcases行对于先手获胜输出“TAK”,先手狗带输出“NIE”。Sample Input

1
5

Sample Output

NIE
/*
    最简单的做法就是找规律了,直接搜一下就能获得所有的胜负态。 
    仔细观察可以发现质数除了2和17就是败的,合数除了16,34和289都是赢的。 
    感觉这样是不太科学的,那就来讲讲道理。 
    我们发现2,4,8都是赢的,而16的后继状态都是赢的,所以它是败的,而2^n(n>4)都能转化到16。 
    同样的我们能说明17和2^n 17^m。 
    我们考虑一个合数,它的因数肯定有个败态的,它就必胜了。 
    这样也就说明了质数是必败了。
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int temp=n;
        for(int i=2;i*i<=temp;i++)
            while(temp%i==0)temp/=i;
        if(temp==n){
            if(n==2||n==17)printf("TAK\n");
            else printf("NIE\n");
        }
        else{
            if(n==16||n==34||n==289)printf("NIE\n");
            else printf("TAK\n");
        }
    }
    return 0;
}
AC代码 打表找规律
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e7 + 5;
int dp[maxn];//dp[i]表示当有i个石子并且先手先取,先手能否获胜
int dfs(int x){
    if(x<maxn&&dp[x]!=-1)return dp[x];
    for(int i=2;i*i<=x;i++){
        if((x%i==0)&&(!dfs(i)||!dfs(x/i))){
            if(x<maxn)return dp[x]=1;
            else return 1;
        }
    }
    if(!dfs(x-1)){ //这里一定要放在后面
        if(x<maxn)return dp[x]=1;
        else return 1;
    }
    if(x<maxn)return dp[x]=0;
    else return 0;
}
int main(){
    int t,n;
    memset(dp,-1,sizeof(dp));
    dp[0]=0;dp[1]=0;
    dp[2]=1;dp[3]=0;
    cin>>t;
    while(t--){
        scanf("%d",&n);
        if(dfs(n))cout<<"TAK"<<endl;
        else cout<<"NIE"<<endl;
    }
    return 0;
}
AC代码 记忆化搜索
posted @ 2017-09-17 17:49  Echo宝贝儿  阅读(164)  评论(0编辑  收藏  举报