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:
- Choose one pile and remove k(k >0) stones from it;
- 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≤ 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
Hint
Source
题目的意思是说有两个人进行博弈,现有两种操作,一种是全部堆取k个,但是k不可超过最小堆中的石子个数,另一种是可以从任意一堆中取任意个数的石子,题目保证输入的堆数是素数。
我们可以很惊讶的发现,如果去掉第一种操作就是个裸地Nim,如果去掉第二种操作就是裸地威佐夫,所以这个题的题解就是Nim+威佐夫,威佐夫就不用讲了,只有堆数为2的时候才是威佐夫,直接上板子就可以了,然后我们考虑Nim,也就是n>=3的时候,我们就不讲第二种操作了,就是裸地Nim
code:
#include <bits/stdc++.h> using namespace std; int a[1000]; int main(){ int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); for(int i = 0; i < n; i++){ scanf("%d",&a[i]); } if(n == 2){ if(a[0] < a[1]) swap(a[0],a[1]); if(floor((a[0] - a[1]) * ((sqrt(5.0)+1.0) / 2.0)) != a[1]){ printf("Sherlock\n"); } else{ printf("Watson\n"); } } else{ int k = a[0]; for(int i = 1; i < n; i++){ k ^= a[i]; } if(k == 0) printf("Watson\n"); else printf("Sherlock\n"); } } return 0; }