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.");
        }
    }
  • 相关阅读:
    ubuntu英文环境下使用中文输入法
    Flex 调用添加了SoapHeader的web service
    RoR: Ruby On Rails Web Service 3 分发模式
    C# CRC8实现
    java正则表达式过滤html标签
    静态内部类和非静态内部类的区别
    Java反射机制
    java回调函数简介
    Java之泛型编程
    Java基础知识之系统命令调用、序列化、JDO、匿名内部类
  • 原文地址:https://www.cnblogs.com/lonewanderer/p/5737674.html
Copyright © 2011-2022 走看看