zoukankan      html  css  js  c++  java
  • 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
    ");
                else printf("NIE
    ");
            }
            else{
                if(n==16||n==34||n==289)printf("NIE
    ");
                else printf("TAK
    ");
            }
        }
        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代码 记忆化搜索
  • 相关阅读:
    Linux命令:cp (copy)复制文件或目录
    使用 robots.txt 文件阻止或删除网页说明
    ecshop优化修改sitemap.xml到根目录
    我虚拟机上装的CentOS系统显示的ip配置是127.0.0.1,请问如何解决?
    Servlet/JSP vs. ASP.NET MVC
    Ubuntu Linux 上安装Apache的过程
    Ubuntu Linux 上安装Eclipse的过程
    sudo的意义
    Dependency Injection
    Ubuntu Linux 上安装TomCat的过程
  • 原文地址:https://www.cnblogs.com/thmyl/p/7536447.html
Copyright © 2011-2022 走看看