zoukankan      html  css  js  c++  java
  • 山东省第八届省赛 A:Return of the Nim(尼姆+威佐夫)

    Problem Description

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

    • There are n piles of stones denoted as ,,...,, 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

    Input

    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 ,,...,.

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

    Output

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

    Sample Input

    2
    3
    2 3 2
    2
    2 1
    

    Sample Output

    Sherlock
    Watson
    

    Hint

    题意:

    题意:

    Sherlock 和 Watson 做游戏,游戏规则如下:
    有n堆石子,第i堆石子的个数为pi, Sherlock  先取,有两种取法,
    1.每次每人可取任意一石堆中的任意个数  (当然所取的个数不能超过石堆中石子的个数)

    2.在没有出现空堆的情况下,每次每人可从所有石堆中取相同个数的石子  (当然所取的个数不能超过石堆中所含最少石子的个数)

    谁先取完谁赢。告诉你石堆的个数及每堆所含石子的个数,要求你输出胜利者的姓名

    题解:

    两堆:威佐夫博弈(不作详细解释)

    大于两堆:尼姆博弈

    代码:

    #include <bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <set>
    #include <bitset>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <iomanip>
    #include <cstdlib>
    using namespace std;
    #define is_lower(c) (c>='a' && c<='z')
    #define is_upper(c) (c>='A' && c<='Z')
    #define is_alpha(c) (is_lower(c) || is_upper(c))
    #define is_digit(c) (c>='0' && c<='9')
    #define min(a,b) ((a)<(b)?(a):(b))
    #define max(a,b) ((a)>(b)?(a):(b))
    #define IO ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    #define For(i,a,b) for(int i = a; i <= b; i++)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    typedef pair<ll,ll> pll;
    typedef vector<int> vi;
    const ll inf=0x3f3f3f3f;
    const double EPS=1e-10;
    const ll inf_ll=(ll)1e18;
    const ll maxn=100005LL;
    const ll mod=1000000007LL;
    const int N = 30+5;
    int ans[N];
    int main()
    { 
        int T;
        cin >> T;
        while(T--)
        {
            int n;
            cin >> n;
            For(i, 1, n)
                cin >> ans[i];
            if(n == 2){
                if(ans[1] > ans[2])
                    swap(ans[1], ans[2]);
                int res = floor((ans[2] - ans[1]) * ((sqrt(5)+1)/2.0));
                if(res == ans[1])
                    cout<< "Watson"<< endl;
                else
                    cout<< "Sherlock" <<endl;
            } else {
                int res = 0;
                For(i, 1, n)
                    res = res^ans[i];
                if(res==0)
                    cout<< "Watson" <<endl;
                else
                    cout<< "Sherlock" <<endl;
            }
        }
        return 0;
    }
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    【新】idea激活码,jetbrains全系列最新2020激活码和破解补丁(亲测可用),可免费使用69年
    分享【高热度网】最火热的10篇文章,每一篇都是精品
    什么是高热度网(gaoredu.com)?高热度网站都有哪些功能?
    【高热度网】2020年最新8款迷你世界手机黑科技辅助软件免费下载
    【我爱科技论坛】关于我爱科技论坛解压密码的说明
    《前端工程师面试宝典》---前端面试必备
    【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)
    【数据结构】队列实现的5种方式及时间复杂度对比分析
    解决Webpack中提示syntax 'classProperties' isn't currently enabled的错误
    如何配置静态MAC地址
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8568484.html
Copyright © 2011-2022 走看看