zoukankan      html  css  js  c++  java
  • hdu 1849 nim博弈

    http://acm.hdu.edu.cn/showproblem.php?pid=1849

    Nim博弈

    算法分析:

    Nim游戏模型:有三堆石子,分别含有abc个石子。两人轮流从某一堆中取任意多的石子,规定每次至少取一个,多者不限。最后取光者得胜。

    定理1Nim游戏的一个状态(abc)是P状态,当且仅当a^b^c =0

     

    对应此题就是:共有m个棋子就是m堆石子,把每个位置的标号等价于该堆石子的数目,取走最后一颗石子的人获胜,就是最后一个回到0位置的人获胜。即Nim博弈问题

    View Code
    // I'm lanjiangzhou
    //C
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    #include <time.h>
    //C++
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <cctype>
    #include <stack>
    #include <string>
    #include <list>
    #include <queue>
    #include <map>
    #include <vector>
    #include <deque>
    #include <set>
    using namespace std;
    
    //*************************OUTPUT*************************
    #ifdef WIN32
    #define INT64 "%I64d"
    #define UINT64 "%I64u"
    #else
    #define INT64 "%lld"
    #define UINT64 "%llu"
    #endif
    
    //**************************CONSTANT***********************
    #define INF 0x3f3f3f3f
    
    // aply for the memory of the stack
    //#pragma comment (linker, "/STACK:1024000000,1024000000")
    //end
    
    const int maxn = 1010;
    int a[maxn];
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            if(n==0) break;
            int t=0;
            for(int i=0;i<n;i++){
                scanf("%d",&a[i]);
                t=(t^a[i]);
            }
            if(t) printf("Rabbit Win!\n");
            else printf("Grass Win!\n");
        }
        return 0;
    }
  • 相关阅读:
    洛谷 P3808 【模板】AC自动机(简单版) 题解
    O3优化模板
    洛谷 P3909 异或之积 题解
    洛谷 P3870 [TJOI2009]开关 题解
    洛谷 P1891 疯狂LCM 题解
    洛谷 P5221 Product 题解
    洛谷 P2568 GCD 题解
    洛谷 P5639 【CSGRound2】守序者的尊严 题解
    扩展kmp板子
    [JZOJ3167] 【GDOI2013模拟3】查税
  • 原文地址:https://www.cnblogs.com/lanjiangzhou/p/3020102.html
Copyright © 2011-2022 走看看