SG function S-nim

Source: Internet
Author: User

http://poj.org/problem?id=2960

S-nim
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3464 Accepted: 1829

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

Source

Svenskt mästerskap i programmering/norgesmesterskapet 2004  test instructions: Given the array s, next gives the M game situation. The game situation is some beads heap, first give the heap number, then the number of beads in each heap. The rules of the game is that two people take turns beads, each time you can choose a heap, from which to remove K beads,k∈s, finally can not take the person loses.    newly learned game, write down, first introduce the Nim game situation 12 heap of Stone, a, b each can take from any heap of any number of stones, and finally do not take the loss of &nbsp, if there are two piles of stone, if the number of stones, the first to go will lose, Because as long as a first take, B can repeat the operation of a above, and a in different heaps to take the same number, two numbers equal when the difference or value equal, and ^ want to wait, and if the number of two stones are different, first take can be the first to take away the number of a lot of that a part of the stone, So that the remaining two piles of the same number of stones 2 n heap of stones, each heap of stones have XI Stone, A and B each time can choose one of the piles to take any number of stones, finally can not take the people lose. Set to win the state is 1, must be in a state after a status of N follow-up state, (the successor State is a pick up, B should take the state of time) if all the subsequent state of one is 0 then this state is 1, and if the successor State is all 1, then this state is 0, One step can go to the must win state, now give a conclusion, will each heap of stone number of different or up, if equal to 0 words is must lose state, if not equal to 0 to win the state, now assume that the number of three piles is a B c, assuming a^b^c=k;(k!=0)   Then the first 1 of the K must be from a B C in one, assuming that from B, then the B^K&LT;B has the first B (B^k) a stone, then, the next state of the XOR value is a^ (b^k) ^c =k^k = 0, in turn recursively go down, The last three piles of stones are 0 of the time must be defeated, so the state of 0 is definitely a must, and the difference or value is not 0 can be obtained by one stone to the required state, so is the winning state.   The following describes the SG function: definition: The smallest non-negative integer that is not present in the collection, S = {1,2,3,4,5}   sg (s) = 0;  s ={0,1,2,3}  sg (s) =4;  situation 1: There is a heap of stones K, only x at a time, x belongs to S = {2,4,5}. procedure, to look for precursors, set K is the K of all precursors of the SG worthy of collectionExample: SG (8) = SG ({SG (6), SG (4), SG (3)}, the procedure, to find the SG (k) can be, if the SG (k) = 0 is a must, SG (1)!=0 for the winning state, the recursive request SG (0)  = 0; Because there is no precursor, SG (1) = 0; s G (2) = sg{0} = 1;SG (3) = SG ({SG (1)}) = 1; SG (4) = SG ({SG (2), SG (0)}) = 2;SG (5) = SG ({SG (3), SG (1), SG (0)} = 2;  Case 2: There are n heap of stones, each pile of Xi stones, each time from the heap I can only take si = {...} In the number of worthy, the last can not take the person to lose the procedure: to find out each pile of SG (xi); Suppose SG (xi) = k; then its rear drive must have k-1,k-2,...... In this case, it is quite a nim game, each heap has a k stone must take, can take any number of stones, so each SG (xi) will be different or up, and then if the result is 0 will fail   hint: The general game problem, the value of the different or up generally can do the   Now give the NIM game with the proof of the SG function, NIM game is equivalent to the number of each acquisition is 123......N, so sg (0) = 0; SG (n) = n;   Below is the code: 
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineN 1056 #defineM 100057 8 intS[n], SN;9 intSg[m];Ten  One voidGETSG (intN) A { -     intMk[m]; -sg[0] =0;//The main is to let the terminating state of the SG is 0 theMemset (MK,-1,sizeof(MK)); -      for(inti =1; i < M; i++)//pre-processing SG functions -     { -          for(intj =0; J < n && S[j] <= i; J + +) +Mk[sg[i-s[j]]]=i;//mark all subsequent SG as I, and then find the smallest positive integer that the subsequent SG did not appear -                              //Optimization: Note that this is marked as I, just beginning to mark 1, so that each time you need to initialize the Mk Memset, and labeled I do not need +         intj =0; A          while(Mk[j] = = i) J + +; atSg[i] =J; -     } - } -  - intMain () - { in      while(~SCANF ("%d", &sn), SN) -     { to          for(inti =0; i < SN; i++) scanf ("%d", &s[i]); +Sort (s, s+sn);//sorting is an optimization, when the SG is used - GETSG (SN); the         intm; *scanf"%d", &m); $         CharAns[n];Panax Notoginseng          for(intc =0; C < m; C++) -         { the             intN, TM; +scanf"%d", &n); A             intres =0; the              for(inti =0; I < n; i++) +             { -scanf"%d", &TM); $Res ^=Sg[tm]; $             } -             if(res = =0) Ans[c] ='L'; -             ElseANS[C] ='W'; the         } -ans[m]=0;Wuyiprintf"%s\n", ans); the     } -     return 0; Wu}

SG function S-nim

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.