(SG) poj 2960

S-Nim

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3356   Accepted: 1769

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 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 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

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 5
3
2 5 12
3 2 4 7
4 2 3 7 12
5 1 2 3 4 5
3
2 5 12
3 2 4 7
4 2 3 7 12
0

Sample Output

LWW
WWL

Source

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int num[105],k,m,n,res;
int sg[10010];
int SG(int x)
{
    int i;
    if(sg[x]+1) return sg[x];
    bool vis[10010]={0};
    for(i=1;i<=k;i++)
    {
        if(x>=num[i])
            vis[SG(x-num[i])]=1;
    }
    for(i=0;vis[i];i++);
    return sg[x]=i;

}
int main()
{
    while(scanf("%d",&k)!=EOF)
    {
        if(k==0)
            break;
        for(int i=1;i<=k;i++)
            scanf("%d",&num[i]);
        memset(sg,-1,sizeof(sg));
        sg[0]=0;
        scanf("%d",&m);
        while(m--)
        {
            int nim=0;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&res);
                nim=nim^SG(res);
            }
            if(nim)
                printf("W");
            else
                printf("L");
        }
        printf("\n");
    }
    return 0;
}

  

时间: 2024-10-28 19:28:37

(SG) poj 2960的相关文章

博弈论(SG) hdu5724

百度搜一下博弈论 由感性认识到理性认识的论文 理论铺垫: 1.定义P-position和 N-position:其中P代表Previous,N代表Next.直观的说,上一次move的人有必胜策略的局面是P-position,也就是“先 手必败”,现在轮到move的人有必胜策略的局面是N-position,也就是“先手可保证必胜”. (1).无法进行任何移动的局面(也就是terminal position)是P-position: (2).可以移动到P-position的局面是N-position

Red and Black (BFS)poj 1979

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26181   Accepted: 14213 Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a

poj 3537 Crosses and Crosses (SG)

题意: 1 × n 个格子,每人每次选一个格子打上叉(不得重复),如果一个人画完叉后出现了连续的三个叉,则此人胜. 给n,判断先手胜还是先手败. 思路: 假设选择画叉的位置是i,则对方只能在前[1,i-3]中或[i+3,n]中选择画叉.子问题出现. 根据SG的定义,即可求出SG(N).看代码. 代码: int sg[2005]; int n; int dfs(int n){ if(n<0) return 0; if(sg[n]!=-1) return sg[n]; bool g[2005] =

poj 2311 Cutting Game (SG)

题意: 有一张W*H的纸片. 每人每次可以横着撕或者竖着撕,先撕出1*1那一方胜. 数据范围: W and H (2 <= W, H <= 200) 思路: 很好抽象出游戏图的模型,用SG解决.直接看代码. 代码: int dp[maxn][maxn]; int sg(int w,int h){ if(dp[w][h]!=-1) return dp[w][h]; bool g[maxn]; mem(g,false); for(int i=2;i<=w/2;++i) g[sg(i,h)^s

hdu 1848 Fibonacci again and again (SG)

题意: 3堆石头,个数分别是m,n,p. 两个轮流走,每走一步可以选择任意一堆石子,然后取走f个.f只能是菲波那契中的数(即1,2,3,5,8.....) 取光所有石子的人胜. 判断先手胜还是后手胜. 思路: 简单SG.看代码. 代码: int sg[1005]; int dfs(int x){ if(sg[x]!=-1) return sg[x]; bool vis[1005]={0}; int t1=1,t2=2; if(x>=t1) vis[dfs(x-t1)]=true; while(x

hdu 1847 Good Luck in CET-4 Everybody!(sg)

Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9703    Accepted Submission(s): 6286 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧张得连短学期的ACM都没工夫练习了,反正我知道的Kiki和Ci

pku 2425 A Chess Game (SG)

题意: 给一个由N个点组成的一张有向图,不存在环.点的编号是0~N-1. 然后给出M个棋子所在的位置(点的编号)[一个点上可同时有多个棋子]. 每人每次可移动M个棋子中的一个棋子一步,移动方向是有向边指向的方向.最后无法移动棋子的人输. 思路: 一眼就可看出的裸的SG,直接看代码吧. 代码: int sg[1005]; vector<int> graph[1005]; int dfs(int x){ // position x if(sg[x]!=-1) return sg[x]; int L

(kmp)poj 3080 ——Blue Jeans

The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tasked with wri

hdu 4559 涂色游戏(SG)

在一个2*N的格子上染色 每次可以染1*1的格子 或者2*2的格子 最涂满所有格子的人胜 m为已染色格子的个数 Alice先手 1*1的点SG值为1 SG[i]表示连续2*i的SG值 sg[3] 就是连续2*3空白格子 假如n = 3 涂一个1*1个格子的情况 那么后继状态就可能是 状态1 连续0列的格子+连续2列的格子+剩余那一列涂1个格子状态2 连续1列的格子+连续1列的格子+剩余那一列涂1个格子 涂一个2*3个格子的情况 那么后继状态就可能是 状态1 连续0列的格子+连续1列的格子+剩余的