S-nim
Time Limit: 2000MS |
|
Memory Limit: 65536KB |
|
64bit IO Format: %i64d &%i64u |
Submit Status
Description
Arthur and his sister Caroll has been playing a game called Nim for some time now. Nim is played as follows:
- The starting position have a number of heaps, all containing some, not necessarily equal, number of beads.
- The players take turns chosing a heap and removing a positive number of beads from it.
- The first player isn't able to make a move, loses.
Arthur and Caroll really enjoyed playing this simple game until they
Recently learned an easy-to-always-be able-to-find the best move:
- Xor the number of beads in the heaps in the current position (i.e. if we have 2, 4 and 7 The xor-sum would be 1 as 2 XOR 4 XOR 7 = 1).
- If The xor-sum is 0, too bad, you'll lose.
- Otherwise, move such that the xor-sum becomes 0. This was always possible.
It's quite easy-to-convince oneself that's this works. Consider these facts:
- The player is takes the last bead wins.
- After the winning player's last move the xor-sum would be 0.
- The xor-sum would change after every move.
Which means that if you do sure that the xor-sum always was 0 when you had made your move, your opponent would never be a Ble to win, and, thus, you'll win.
Understandibly It is no play a game when both players know how to play perfectly (ignorance is bliss). Fourtunately, Arthur and Caroll soon came up with a similar game, S-nim, which seemed to solve this problem. Each player was now only allowed to remove a number of beads in some predefined set S, e.g. if we had s = {2, 5} each play Er is only allowed to remove 2 or 5 beads. Now it isn't always possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it?
Your job is to write a program this determines if a position of S-nim is a losing or a winning position. A position is a winning position if there are at least one move to a losing position. A position is a losing position if there be no moves to a losing position. This means, as expected, which a position with no legal moves is a losing position.
Input
Input consists of a number of test cases.
For each test case:the first line contains a number k (0 < k≤100) describing the size of S, followed by K numbers si (0 < si≤10000) describing S. The second line contains a number m (0 < m≤100) describing the number of positions to evaluate. The next m lines each contain a number L (0 < l≤100) describing the number of heaps and L numbers hi (0≤hi≤10000) Describing the number of beads in the heaps.
The last test case was followed by a 0 on a line of its own.
Output
For each position:if the described position is a winning position print a ' W '. If the described position is a losing position print a ' L '.
Print a newline after all test case.
Sample Input
2 2 532 5 123 2 4 74 2 3 7 125 1 2 3 4 532 5 123 2 4 74 2 3 7 120
Sample Output
Lwwwwl
The classic NIM game title has been given--there is no limit to the number of selections in each heap.
The S-nim game only limits the number of items to be picked from each heap, and is still calculated with the SG function.
The classic NIM game in the SG (x) = x, so the result is the state of each heap directly XOR can, S-nim game first calculate the value of the SG function of each heap, and then the method is still used to determine the XOR.
#include <iostream>#include<cstring>#include<algorithm>using namespacestd;#defineN 102intn,m,l,s[n],sg[10005],ans,a;BOOLvis[10005];voidC () {sg[0]=0; for(intI=1; i<=10000; i++) {memset (Vis,0,sizeof(VIS)); for(intj=0; j<n&&s[j]<=i;j++) Vis[sg[i-s[j]]]=1; for(intx=0; x<=10000; x + +) if(!Vis[x]) {Sg[i]=x; Break; } }}intMain () { while(cin>>n&&N) { for(intI=0; i<n;i++) Cin>>S[i]; Sort (s,s+N); C (); CIN>>m; while(m--) {ans=0; CIN>>l; while(l--) {cin>>A; Ans^=Sg[a]; } if(ans) cout<<"W"; Elsecout<<"L"; } cout<<Endl; } return 0;}
Understanding of the SG function:
#include <iostream>#include<cstring>#include<algorithm>using namespacestd;#defineN 102intn,m,l,s[n],sg[10005],ans,a;BOOLvis[10005];voidC () {sg[0]=0; for(intI=1; i<=10000; i++) {memset (Vis,0,sizeof(VIS)); for(intj=0; j<n&&s[j]<=i;j++) Vis[sg[i-s[j]]]=1;//inside the sg[] is the successor of Sg[i] "is to seek Mex for(intx=0; x<=10000; x + +) if(!Vis[x]) {Sg[i]=x; Break; } }}intMain () { while(cin>>n&&N) { for(intI=0; i<n;i++) Cin>>S[i]; Sort (s,s+N); C (); for(intI=0;i< -; i++) cout<<"I:"<<i<<"Sg[i]:"<<sg[i]<<Endl; /*cin>>m; while (m--) {ans=0; cin>>l; while (l--) {cin>>a; Ans^=sg[a]; } if (ans) cout<< "W"; else cout<< "L"; } cout<<endl;*/ } return 0;}
Let's consider the vertex SG g (x) =k 0<=i<k x y meet g (y) =i
in other words, when a piece SG value is k when we can turn it into a 0 , become 1 , ... , become k-1 , but absolutely cannot keep k not change.
I don't know if you can relate to this. Nim Games, Nim the rules of the game are: each time you select a stack of k of Stones,
can turn it into 0 , become 1 , ... , become k-1 , but absolutely cannot keep k not change. This indicates that if the SG value of the vertex at which the n pieces are located is treated as n
heap The corresponding number of stones, then this Nim each winning strategy of the game corresponds to the original N a winning strategy for pieces!
hdu1536&&poj2960 S-nim (SG function game)