省SD2017 A Return of the Nim【威佐夫博弈+NIM】
2017-09-21 17:16
375 查看
Return of the Nim
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 9 Solved: 3
[Submit][Status][Discuss]
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≤ 105 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
山东省第八届ACM程序设计大赛2017.5.7
这题,优秀!
//威佐夫博弈+NIM
给你素数堆石子,你每次可以在一个堆中取任意数量石子,也可以在所有堆中去掉相同数量的石子。
如果只有两个堆,不必说,直接就是威佐夫博弈了。
然后这个素数堆是关键,除去2,剩下的堆不仅是素数也是奇数。
NIM
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 9 Solved: 3
[Submit][Status][Discuss]
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≤ 105 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
山东省第八届ACM程序设计大赛2017.5.7
这题,优秀!
//威佐夫博弈+NIM
给你素数堆石子,你每次可以在一个堆中取任意数量石子,也可以在所有堆中去掉相同数量的石子。
如果只有两个堆,不必说,直接就是威佐夫博弈了。
然后这个素数堆是关键,除去2,剩下的堆不仅是素数也是奇数。
NIM
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int a[1000]; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> 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]) { cout << "Sherlock" << endl; } else cout << "Watson" << endl; } else//NIM { int k = a[0]; for (int i = 1; i < n; i++) { k ^= a[i]; } if (k == 0) cout << "Watson" << endl; else cout << "Sherlock" << endl; } } return 0; }
相关文章推荐
- 2017 省赛 山东 A Return of the Nim 【博弈】【Nim+Wythoff】
- Return of the Nim----Nim博弈+威佐夫博弈 山东省第八届省赛A题
- 【补题】2017山东省赛A题 Return of the Nim
- 山东省第八届省赛 Return of the Nim(博弈)
- 山东省第八届省赛A题 Return of the Nim
- Return of the Nim
- Return of the Nim 山东省赛
- sdut 3893 Return of the Nim(matrix nim)
- 第八节省赛题解-Return of the Nim(尼姆博奕)
- 山东省第八届ACM省赛A.Return of the Nim
- 2017年ACM第八届山东省赛A题:Return of the Nim
- ACM 第八届山东省赛 A题 Return of the Nim
- 山东省第八届acm省赛 Return of the Nim
- About the return value of SetTimer function
- HDU 6138 Fleet of the Eternal Throne(2017 Multi-University Training Contest 8)
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- 2017 ACM/ICPC Asia Regional Qingdao Online C The Dominator of Strings
- Assigning the return value of new by reference is deprecated in xxxx
- ACM-ICPC 2017 Asia Urumqi: K. Sum of the Line(容斥)
- 2016 ICPC 大连 C Game of Taking Stones 【威佐夫博弈+大数+高精度】