山东省第八届省赛 Return of the Nim(博弈)

来源:互联网 发布:蝙蝠侠 超人 知乎 编辑:程序博客网 时间:2024/05/20 10:14

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

Sherlock

Watson

博弈题,在n<3的时候是威佐夫博弈,n>=3的时候是尼姆博弈,分类讨论完美解决。

博弈胜利的条件是拿到最后的石子,所以只要判断谁先能让石子达到奇异状态谁就会获胜。
因为任何自然数在威佐夫博弈中都包含于唯一一个奇异状态中,所以只要两堆石子初始状态下处于非奇异状态,那么,先手胜,反之,后手胜。所以两堆是只要判断是否初始奇异即可。
三堆或者三堆以上时,是尼姆博弈,如果我们面对的是一个非奇异局势(a,b,c),要如何变为奇异局势呢?假设 a < b< c,我们只要将 c 变为 a^b,即可,因为有如下的运算结果: a^b^(a^b)=(a^a)^(b^b)=0^0=0。要将c 变为a^b,只从 c中减去 c-(a^b)。
而判断是否为奇异状态,只要用异或运算便可求得。
#include   #includeusing namespace std;  int a[1000];  int main(){      int t;      scanf("%d",&t);      while(t--){          int n;          scanf("%d",&n);          for(int i=0;i
原创粉丝点击