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.");
        }
    }
  • 相关阅读:
    RedHat 7.0及CentOS 7.0禁止Ping的三种方法
    修改WordPress后台默认登陆地址提高网站安全性
    解决WordPress用户名密码都正确但点击登陆就清空密码的问题
    Windows上使用Git托管代码到Coding
    使用Coding Pages托管网站
    Windows上设置Mozilla Thunderbird邮件客户端后台运行
    在VirtualBox中安装BlackArch Linux
    关于XML学习
    软件工程课程设计团队项目总结与项目报告
    详细设计
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5737674.html
Copyright © 2011-2022 走看看