S-Nim (hdu1536 + SG function)

Source: Internet
Author: User

S-Nim (hdu1536 + SG function)
S-Nim

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 5317 Accepted Submission (s): 2288


 

Problem Description Arthur 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.
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 is 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 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

 

 

Enter K to indicate the size of a set, and then enter a set to indicate that only the number of elements in the set can be used for the stones.

Enter m to ask about this set.

Then input n in each row in m rows, indicating that there are n heaps, and each Heap has n1 stones. Ask whether the State indicated by this row is "win" or "lose". If you win, enter W. Otherwise, L

Idea: we can divide n heap stones into n games and then combine n games.

 

Reprinted, please specify the Source: search for & STAR kids

 

Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1536

 

# Include
 
  
# Include
  
   
# Includeusing namespace std; // note that the S array should be sorted in ascending order. The SG function must be initialized to-1. For each set, only one time is required. // n is the size of the Set s. S [I] is defined. array int s [110] of special fetch rules, sg [10010], n; int SG_dfs (int x) {int I; if (sg [x]! =-1) return sg [x]; bool vis [110]; memset (vis, 0, sizeof (vis); for (I = 0; I
   
    
= S [I]) {SG_dfs (x-s [I]); vis [sg [x-s [I] = 1 ;}} int e; for (I = 0; I ++) if (! Vis [I]) {e = I; break;} return sg [x] = e;} int main () {int I, m, t, num; while (scanf (% d, & n) {for (I = 0; I
    
     

 

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.