【补题】2017山东省赛A题 Return of the Nim

来源:互联网 发布:剑三霸刀捏脸数据 编辑:程序博客网 时间:2024/06/10 11:49

题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/3893.html


Return of the Nim

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss

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")

Example Input

232 3 222 1

Example Output

SherlockWatson

Hint

Author

“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

思路


1. 堆数 = 2时为裸的威佐夫博弈,用模板即可;

2. 堆数 > 2时,可以看作在尼姆博奕的推广的基础上多加了威佐夫博弈的一个操作,即各堆均取k个。

    这时讨论均取k个这个操作对平衡状态的影响:

           ①:当目前状态为平衡态时,均取k个。把k转化成二进制表示,先考虑其最后为1的一位,记为第x位。所有堆的数目的二进制表示,减去x位上的1,原来是1的,1 - 1 = 0;原来是0的,向高位借位,该位 0 - 1 = 1。即1变0,0变1。由于共素数堆,且堆数不为2,则堆数也全为奇数。现在变为奇数个数取反后再异或,结果也取反。

           ②:由上可证,通过均取k个这个操作,一定会把平衡态转换成非平衡态。因为最起码有一位的异或值从0变成了1.而根据尼姆博奕的结论,后手又可以通过第一种操作,即单堆取k个,来把非平衡态转化成平衡态。

           ③:故均取k个这个操作,在堆数为奇数的情况下,并没有影响尼姆博弈平衡态和非平衡态的转化过程。

3. 由上可知,堆数 > 2时,仍可使用尼姆博奕求解。


感想及其他

“奇数个数取反后再异或,结果也取反”这个结论同样可证。在平衡态中,由于堆数为奇数个,必有偶数个堆在第x位上为1,奇数个堆在第x位上为0,此时为平衡态,即异或值为0.若有奇数个1,偶数个0,则异或值为1,为非平衡态。

在这种情况下,按位取反,则变为非平衡态状态,故结果取反.

      可得结论,1异或奇数个1的异或值,即1 ^(1……1)(x个) = 0;

                       0异或奇数个1的异或值,即0 ^(1……1)(x个) = 1;

找到与经典模型不同的地方,然后发现它对原来解题方法的影响,对代码进行修改,就可以AC辣~


代码

/***********************author : Meng Xiangcong***********************///#include <bits/stdc++.h>#include <map>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <utility>#include <climits>#include <ctype.h>#include <sstream>#include <iostream>#include <algorithm>#include <functional>using namespace std;#define PI         acos(-1)#define INF        (1<<30)#define EPS        1e-6#define SETDATA    memset(data, 0, sizeof(data))#define SET(a, b)  memset(a, b, sizeof(a))typedef long long LL;int main(){    int t, n;    int x, y;    scanf("%d", &t);        while (t--)    {        scanf("%d", &n);                if (n == 2)       //Wythoff        {            scanf("%d %d", &x, &y);            if (x < y)    //x is the greater one                swap(x, y);            if (floor((sqrt(5.0) + 1.0) / 2.0 * (x - y)) == y)                puts("Watson");            else                puts("Sherlock");        }        else              //Nim        {            x = 0;            while (n--)            {                scanf("%d",&y);                x ^= y;            }            if(x == 0)                puts("Watson");            else                puts("Sherlock");        }    }    return 0;}