zoukankan      html  css  js  c++  java
  • hdu 5795 A Simple Nim (sg函数)

    A Simple Nim

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 33    Accepted Submission(s): 18


    Problem Description
    Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more interesting,players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
     
    Input
    Intput contains multiple test cases. The first line is an integer 1T100, the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n1], representing heaps with s[0],s[1],...,s[n1] objects respectively.(1n106,1s[i]109)
     
    Output
    For each test case,output a line whick contains either"First player wins."or"Second player wins".
     
    Sample Input
    2
    2
    4 4
    3
    1 2 4
     
    Sample Output
    Second player wins.
    First player wins.
     
    找到规律以后很好做,关键是打表的程序。
     
    打表程序
    #include <bits/stdc++.h>
    using namespace std;
    int g[1010];
    void init() {
        memset(g, -1, sizeof(g));
    }
    int getSG(int x) {
        if(g[x] != -1) return g[x];
        if(x == 0) return 0;
        if(x == 1) return 1;
        if(x == 2) return 2;
        int vis[110];
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i < x; i++) {
            int t = 0;
            int a = getSG(i);
            t ^= a;
            for(int j = 1; j < x - i; j++) {
                int tt = t;
                int b = getSG(j);
                int c = getSG(x - i - j);
                tt ^= b;
                tt ^= c;
                vis[tt] = 1;
                vis[c] = vis[b] = 1;
            }
            vis[a] = 1;
            vis[t] = 1;
        }
        vis[0] = 1;
        for(int i = 0; ; i++) if(!vis[i])
            return i;
    }
    int main() {
        int n;
        init();
        for(int i = 1; i <= 100; i++) {
            g[i] = getSG(i);
            printf("%d %d %d
    ", i, i % 8, g[i]);
        }
    }

    ac代码

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 1e6 + 10;
    int getSG(int x) {
        if(x == 0)
            return 0;
        if(x % 8 == 0)
            return x - 1;
        if(x % 8 == 7)
            return x + 1;
        return x;
    }
    
    int a[maxn];
    
    int main() {
        int t;
        scanf("%d", &t);
        while(t--) {
            int n;
            scanf("%d",&n);
            int ans = 0;
            for(int i = 1; i <= n; i++) {
                scanf("%d", &a[i]);
                ans ^= getSG(a[i]);
            }
            if(!ans)
                puts("Second player wins.");
            else
                puts("First player wins.");
        }
    }
  • 相关阅读:
    记一次proc_open没有开启心得感悟
    Nginx 502 Bad Gateway 的错误的解决方案
    Linux安装redis,启动配置不生效(指定启动加载配置文件)
    设置redis访问密码
    LNMP 多版本PHP同时运行
    ***总结:在linux下连接redis并进行命令行操作(设置redis密码)
    设计模式(一)单例模式:3-静态内部类模式(Holder)
    设计模式(一)单例模式:2-懒汉模式(Lazy)
    设计模式(一)单例模式:1-饿汉模式(Eager)
    设计模式(一)单例模式:概述
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5737674.html
Copyright © 2011-2022 走看看