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;
    }
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    我已经发表的文章:2,《上帝的小蚂蚁》(未经作者本人同意,不得转载!)
    【windows环境下】RabbitMq的安装和监控插件安装
    Apache FtpServer扩展【动手实现自己的业务】
    我的博客开始之旅
    curl和wget的区别和使用 wanglf
    ceph集群部署 wanglf
    ansible实现SSH配置免密互信 wanglf
    kubernetes(k8s) helm安装kafka、zookeeper wanglf
    django项目中使用KindEditor富文本编辑器 wanglf
    下一代网页:当HTML5取代Flash
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8568484.html
Copyright © 2011-2022 走看看