• 山东省第八届省赛 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;
    }
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    Effective_STL 学习笔记(四十) 使仿函数类可适配
    Effective_STL 学习笔记(三十九) 用纯函数做判断式
    PMP考试大纲
    小技巧
    git 常用命令
    java web的返回值对象
    工作任务-SM敏捷核心思维
    树莓派上手
    spring 公用异常处理
    前端现在版本怎么这么乱
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8568484.html
Copyright © 2020-2023  润新知