zcmu 2001: Return of the Nim

2001: Return of the Nim

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 20  Solved: 15
[Submit][Status][Web Board]

Description

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

  •   There are n piles of stones denoted as  piles0 ,   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≤k≤the 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 piles0, piles1,..., pilesn-1.

  • 1≤g≤15
  • 2≤n≤30, where n is a prime.
  • 1≤pilesi≤10^5 where 0≤i≤n−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

 n堆石子,第i堆石子的个数为pi(2<=n<=30且n为质数)S,W两人轮流取石子,有两种取法
1.每次每人可取任意一石堆中的任意个数  (所取的个数不能超过石堆中石子的个数)
2.在没有出现空堆的情况下,每次每人可从所有石堆中取相同个数的石子  (所取的个数不能超过石堆中所含最少石子的个数)

 威佐夫博弈+尼姆博奕

n=2时,为威佐夫博弈

n>=3时,为尼姆博弈

#include<bits/stdc++.h>
#define ll long long
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=10005;
int main()
{
   int t;
   cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n==2){
            int a,b;
            cin>>a>>b;
            if(a>b)swap(a,b);
            int k=(b-a)*(sqrt(5.0)+1)/(2.0);
            if(a==k)cout<<"Watson"<<endl;
            else cout<<"Sherlock"<<endl;
        }else{
            int a[N];
            cin>>a[0];
            int tmp=a[0];
            pre(i,1,n-1){
               cin>>a[i];
                tmp^=a[i];
            }
            if(tmp==0)cout<<"Watson"<<endl;
            else cout<<"Sherlock"<<endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值