S-NimTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 4091 Accepted Submission (s): 1760
Problem DescriptionArthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows:
The starting position has 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 not able to make a move, loses.
Arthur and Caroll really enjoyed playing this simple game until they recently learned an easy way 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 will be 1 as 2 xor 4 xor 7 = 1 ).
If the xor-sum is 0, too bad, you will lose.
Otherwise, move such that the xor-sum becomes 0. This is always possible.
It is quite easy to convince oneself that this works. Consider these facts:
The player that takes the last bead wins.
After the winning player's last move the xor-sum will be 0.
The xor-sum will change after every move.
Which means that if you make sure that the xor-sum always is 0 when you have made your move, your opponent will never be able to win, and, thus, you will win.
Understandibly it is no fun to 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, that seemed to solve this problem. each player is now only allowed to remove a number of beads in some predefined set S, e.g. if we have S = (2, 5) each player is only allowed to remove 2 or 5 beads. now it is not alway S possible to make the xor-sum 0 and, thus, the strategy above is useless. Or is it?
Your job is to write a program that determines if a position of S-Nim is a losing or a winning position. A position is a winning position if there is at least one move to a losing position. A position is a losing position if there are no moves to a losing position. this means, as expected, that a position with no legal moves is a losing position. inputInput 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 is followed by a 0 on a line of its own. outputFor each position: If the described position is a winning position print a 'W '. if the described position is a losing position print an 'L '. print a newline after each 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
SourceNorgesmesterskapet 2004 question: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1536
This is also the question of a classic SG function. There is a solution to the SG function. You can stamp this, which is very detailed → explain. (If K is 0, it ends.) The number followed by k indicates that the number of items in the number set is obtained (that is, only the number of items can be retrieved at a time) and then the number will be M, M inquiries. Then, in the next M rows, each line first has an N value, indicating the number of heap items. N followed by N numbers, indicating the number of items per heap.
Because, in the OJ background operations, the input and output are separated (in fact, your program's answer is saved as a TXT file and then compared with the standard answer TXT file in binary format, each N directly outputs 'L' or 'W'. When the M line ends, the line breaks and no array is used to store the answer. PS: scanf is faster than cin by 80 MS
/*************************************** **************************************** * ******************* Author: tree ** From: http://blog.csdn.net/lttree ** Title: S-Nim ** Source: hdu 1536 ** Hint: SG *************************************** **************************************** * *******************/# include
# Include
# Include using namespace std; # define N 10001int f [N], sg [N]; bool mex [N]; void get_sg (int t, int n) {int I, j; memset (sg, 0, sizeof (sg); for (I = 1; I <= n; I ++) {memset (mex, 0, sizeof (mex); // set 1 for (j = 1; j <= t & f [j] <= I; j ++) mex [sg [I-f [j] = 1; // find the minimum number that does not belong to the set for (j = 0; j <= n; j ++) if (! Mex [j]) break; sg [I] = j ;}} int main () {int k, m, n, I, t, temp; while (scanf ("% d", & k) {for (I = 1; I <= k; ++ I) scanf ("% d ", & f [I]); sort (f + 1, f + k + 1); get_sg (k, N); scanf ("% d", & m ); while (m --) {temp = 0; scanf ("% d", & n); for (I = 0; I