zoukankan      html  css  js  c++  java
  • 2017年ACM第八届山东省赛A题:Return of the Nim

    A题:Return of the Nim

    时间限制: 1 秒  内存限制: 64 MB  |  提交: 33  解决: 16
     

     

    题目描述

    Sherlock and Watson are playing the following modified version of Nim game:

    • There are n piles of stones denoted aspiles0,piles1,...,pilesn-1, and n is a prime number;
    • Sherlock always plays first, and Watson and he move in alternating turns. During each turn, the current player must perform either of the following two kinds of moves:
      1. Choose one pile and remove k(k >0) stones from it;
      2. Remove k stones from all piles, where 1≤kthe size of the smallest pile. This move becomes unavailable if any pile is empty.
    • Each player moves optimally, meaning they will not make a move that causes them to lose if there are still any better or winning moves.
    Giving the initial situation of each game, you are required to figure out who will be the winner

    输入


    The first contains an integer, g, denoting the number of games. The 2×g subsequent lines describe each game over two lines:
    1. The first line contains a prime integer, n, denoting the number of piles.
    2. The second line contains n space-separated integers describing the respective values of piles0,piles1,...,pilesn-1.

    • 1≤g≤15
    • 2≤n≤30, where n is a prime.
    • 1≤pilesi105 where 0≤in−1


    输出

    For each game, print the name of the winner on a new line (i.e., either “Sherlock”or “Watson”)

    样例输入

    2
    3
    2 3 2
    2
    2 1
    

    样例输出

    Sherlock
    Watson
    题意:有 T 组数据
       每组数据 n 堆石子
       每堆石子 num【i】
       sherlock先取 取到最后一颗石子为胜
       n == 2 为 威佐夫博弈
       n > 2 为 NIM博弈
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    using namespace std ; 
    
    #define maxn 100 
    #define LL long long 
    LL num[maxn] ; 
    
    int main(){
        int n ; 
        int t ; 
        scanf("%d" , &t) ; 
        while(t--){
            scanf("%d" , &n) ; 
            for(int i=0 ; i<n ; i++){
                scanf("%d" , &num[i]) ; 
            }
            
            if(n==2){//威佐夫博弈
                if(num[0] < num[1]){
                    swap(num[0],num[1]) ; 
                } 
                if((LL)((num[0]-num[1]) * (1.0 + sqrt(5.0))/2.0) == num[1]){
                    printf("Watson
    ") ; 
                }else printf("Sherlock
    ") ; 
            }
            else {//NIM  游戏
                LL k = num[0] ; 
                for(int i=1 ; i<n ; i++){
                    k = k ^ num[i] ; 
                } 
                if(k==0){
                    printf("Watson
    ") ; 
                }else printf("Sherlock
    ") ; 
            }
            
        }
        return 0 ; 
    }

  • 相关阅读:
    Bzoj 1878: [SDOI2009]HH的项链 莫队
    BZOJ 2140: 稳定婚姻 Tarjan Map
    Bzoj 2190 : [SDOI2008]仪仗队 数论 特判
    bzoj 16801740 : [Usaco2005 Mar]Yogurt factory 贪心 双倍经验
    BZOJ 5387: [Lydsy1806月赛]质数拆分
    BZOJ 1379: [Baltic2001]Postman 水题
    Bzoj : 1823: [JSOI2010]满汉全席
    4952: [Wf2017]Need for Speed 二分
    BZOJ 2301: [HAOI2011]Problem b 2045: 双亲数 同BZOJ 1101 Zap 莫比乌斯反演 3倍经验
    BZOJ 1030: [JSOI2007]文本生成器 AC自动机
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7631399.html
Copyright © 2011-2022 走看看