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.");
        }
    }
  • 相关阅读:
    类图的基本知识
    设计模式——单例模式
    解决phpcms V9 推荐位无法排序
    PHPCMS 多站点管理切换问题
    天天团购系统-简单的目录结构
    php的时间输出格式
    天天团购--源码目录结构
    天天团购系统--部分模板语法
    PHP json数据格式化方法
    PHP json_encode中文乱码解决方法
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5737674.html
Copyright © 2011-2022 走看看