「POJ2975」Nim
Description
Nim is a 2-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player’s move consists of removing one or more stones from any single pile. Play ends when all the stones have been removed, at which point the last player to have moved is declared the winner. Given a position in Nim, your task is to determine how many winning moves there are in that position.
A position in Nim is called “losing” if the first player to move from that position would lose if both sides played perfectly. A “winning move,” then, is a move that leaves the game in a losing position. There is a famous theorem that classifies all losing positions. Suppose a Nim position contains n piles having k1, k2, …, kn stones respectively; in such a position, there are k1 + k2 + … + kn possible moves. We write each ki in binary (base 2). Then, the Nim position is losing if and only if, among all the ki’s, there are an even number of 1’s in each digit position. In other words, the Nim position is losing if and only if the xorof the ki’s is 0.
Consider the position with three piles given by k1 = 7, k2 = 11, and k3 = 13. In binary, these values are as follows:
1 2 3 |
111 1011 1101 |
There are an odd number of 1’s among the rightmost digits, so this position is not losing. However, suppose k3 were changed to be 12. Then, there would be exactly two 1’s in each digit position, and thus, the Nim position would become losing. Since a winning move is any move that leaves the game in a losing position, it follows that removing one stone from the third pile is a winning move when k1 = 7, k2 = 11, and k3 = 13. In fact, there are exactly three winning moves from this position: namely removing one stone from any of the three piles.
Input
The input test file will contain multiple test cases, each of which begins with a line indicating the number of piles, 1 ≤ n ≤ 1000. On the next line, there are n positive integers, 1 ≤ ki ≤ 1, 000, 000, 000, indicating the number of stones in each pile. The end-of-file is marked by a test case with n = 0 and should not be processed.
Output
For each test case, write a single line with an integer indicating the number of winning moves from the given Nim position.
Sample Input
1 2 3 4 5 |
3 7 11 13 2 1000000000 1000000000 0 |
Sample Output
1 2 |
3 0 |
Source
题解
需要第一步取走石子以后,使得所有子局面异或值为0
所以只要一个子局面的值大于其他所有子局面的异或值
第一步就可以取走这堆石子中的一部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include<cstdio> #include<iostream> using namespace std; int n,a[1001]; int main() { while(scanf("%d",&n)) { if(!n)break; int ans=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { int s=0; for(int j=1;j<=n;j++) if(i!=j)s^=a[j]; if(a[i]>s)ans++; } printf("%d\n",ans); } return 0; } |
因为异或的逆运算也是异或,所以这样可以降低复杂度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<cstdio> #include<iostream> using namespace std; int n,a[1001]; int main() { while(scanf("%d",&n)) { if(!n)break; int ans=0,tot=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); ans^=a[i]; } for(int i=1;i<=n;i++) if(a[i]>(ans^a[i]))tot++; printf("%d\n",tot); } return 0; } |