第八节省赛题解-Return of the Nim(尼姆博奕)

Return of Nim

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

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

题意:
Sherlock 和 Watson 在玩游戏,一共有 n 堆石子( n 是素数),总是Sherlock 先取石子,取石子有两种取法:第一种取法,选择 n 堆石子中的一堆,从这堆石子中取 k 个石子;第二种取法,从毎堆石子中都取走 k 个石子( k 小于等于石子数量最少的数量);谁最后把石子取完谁获胜。
解题思路:
如果只有两堆石子,就可以把这场游戏看成是威佐夫博弈,所以当 n ==2时,当成威佐夫博弈写就行;
如果石子堆超过两堆,这场游戏就可以当做是尼姆博奕;尼姆博奕是:有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品中取部分或全部物品,最少取一件,取到最后一件物品的人获胜,所有石子数量异或为零,则后手必定能去完最后的石子。一共还有n堆物品虽然看起来游戏规则和尼姆博奕不太一样,但是,当 n 堆石子异或为零是先手去完石子后,后手总能通过取走一部分石子,创造去平衡状态。

某不知名的大神这样说:

假如说现在有三堆,个数分别是15,6,9,然后我们可以发现这已经在Nim的平衡状态了,我们写成二进制更好看一些
1 1 1 1
0 1 1 0
1 0 0 1
我们随便取一个k,假如说就取2,那么已经在Nim平衡状态的这个状态肯定会被破坏,就是不平衡了,单看倒数第二位,前两个数的倒数第二位为0,最后一个数的倒数第二位为1 ,只看倒数第二位就知道三个数的异或和一定不为0,所以不在平衡状态,又因为题目保证了素数(其实这里挺坑的,奇数堆就可以),所以如果已经在平衡状态的话,减k操作和普通的Nim操作一定会破坏平衡状态,如果不在平衡状态的话,普通的Nim操作或者是减k操作一定可以达到一种平衡状态(普通的Nim操作就可以,减k有的时候也可以,比如7,6,5所有堆同时减去4就达到了平衡状态,单把最后一堆减4个也是平衡状态),然后发现减k操作好像对于Nim博弈来说…并没有什么影响,所以这个题的题解就是如果只有两堆,就是裸地威佐夫博弈,如果是大于两堆,就是裸地Nim。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
using namespace std;

const int N=1e9+7;
int a[50];
int main()
{
    int T,n;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d",&n);
        for (int i=0; i<n; i++)
        {
            scanf("%d",a+i);
        }
        if (n==2)
        {
            int x=a[0],y=a[1];
            if (x<y)
                swap(x,y);
            int temp=floor ( (x-y)*(1.0+sqrt(5.0))/2.0 );
            if (temp==y)
                printf("Watson\n");
            else
                printf("Sherlock\n");
        }
        else
        {
            int temp=a[0];
            for (int i=1; i<n; i++)
                temp^=a[i];
            if (temp==0)
                printf("Watson\n");
            else
                printf("Sherlock\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值