S-nim
Time limit:5000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 7262 Accepted Submission (s): 3074
Problem Descriptionarthur 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 a easy-to-always be-able to fin D 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 Playe 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.
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 was 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 a ' L '. Print a newline after all test case.
Sample Input2 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 OUTPUTLWWWWL
Source Norgesmesterskapet 2004 Test instructions: M heap stone players can remove Si stone from a heap each time cannot take the puzzle: Preliminary learning sg function Sg[i] is the successor of I the SG value does not appear in the non-negative minimum value. SG XOR value is 0
1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^ ^ ^5 o o6 ******************************/7#include <bits/stdc++.h>8#include <map>9#include <Set>Ten#include <cmath> One#include <queue> A#include <bitset> -#include <math.h> -#include <vector> the#include <string> -#include <stdio.h> -#include <cstring> -#include <iostream> +#include <algorithm> - #pragmaComment (linker, "/stack:102400000,102400000") + using namespacestd; A #defineA First at #defineB Second - Const intMod=1000000007; - Const intmod1=1000000007; - Const intMod2=1000000009; - Const Doubleeps=0.00000001; - typedef __int64 LL; in Constll mod=1000000007; - Const intinf=1000000010; to Constll max=1ll<< -; + Const Doubleeps=1e-8; - Const Doubleinf=~0u>>1; the Const DoublePi=acos (-1.0); *typedefDoubledb; $typedef unsignedint UINT;Panax Notoginsengtypedef unsignedLong Longull; - intK; the intsg[10005]; + inta[ the]; A intflag[ the]; the intQ,M,EXM; + voidInit () - { $sg[0]=0; $ for(intI=1; i<=10000; i++) - { -memset (Flag,0,sizeof(flag)); the for(intj=1; j<=k;j++) - {Wuyi if(i-a[j]>=0) the { -flag[sg[i-a[j]]]=1; Wu } - } About for(intj=0;; J + +) $ { - if(flag[j]==0){ -sg[i]=J; - Break; A } + } the } - } $ intMain () the { the while(SCANF ("%d", &k)! =EOF) the { the if(k==0) - Break; in for(intI=1; i<=k;i++) thescanf"%d",&a[i]); the init (); Aboutscanf"%d",&q); the for(intI=1; i<=q;i++) the { thescanf"%d",&m); + intans=0; - for(intj=1; j<=m;j++) the {Bayiscanf"%d",&EXM); theans^=SG[EXM]; the } - if(ans==0) -printf"L"); the Else theprintf"W"); the } theprintf"\ n"); - } the return 0; the}
HDU 1536 SG function