hdu1536博弈论sg函数

用sg函数对每组数据中所有的数处理。。得到的结果异或。为0必输。。否则必胜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<iostream>
//#include<algorithm>
#include<set>
#include<cstdio>
using namespace std;
//const int MAXK=102;
const int MAXM=102;
//const int MAXL=102;
const int MAXH=10002;
set<int>de;
int dp[MAXH];
inline int sg(int cur)
{
    if(dp[cur]!=-1)
    {
        return dp[cur];
    }
    int i;
    set<int>::const_iterator cp;
    bool visited[MAXM];
    memset(visited,0,sizeof(visited));
    if(cur==0)
    {
        return dp[cur]=0;
    }
    bool tag=0;
    for(cp=de.begin();cp!=de.end();cp++)
    {
        if(cur-*cp>=0)
        {
            int r;
            if(dp[cur-(*cp)]!=-1)
            {
                r=dp[cur-(*cp)];
            }else
            {
                r=sg(cur-(*cp));
            }
            if(r==0)
            {
                tag=1;
            }
            //cout<<r<<" ";
            visited[r]=1;
        }else
        {
            break;
        }
    }
    i=0;
    int mex;
    if(tag)
    {
        int j=0;
        while(visited[j])
        {
            j++;
        }
        return dp[cur]=j;
    }else
    {
        return dp[cur]=0;
    }
}
int main()
{
    int k;
    while(scanf("%d",&k)!=EOF)
    {
        memset(dp,-1,sizeof(dp));
        de.clear();
        if(k==0)
        {
            break;
        }
        int i;
        for(i=0;i<=k-1;i++)
        {
            int s;
            scanf("%d",&s);
            de.insert(s);
        }
        int m;
        scanf("%d",&m);
        bool result[MAXM];
        /*for(i=0;i<=10000;i++)
        {
            sg(i);
        }*/
        for(i=0;i<=m-1;i++)
        {
            int l;
            scanf("%d",&l);
            int j=0;
            int sum=0;
            for(j=0;j<=l-1;j++)
            {
                int heap;
                scanf("%d",&heap);
                sum=sum^sg(heap);
            }
            if(sum)
            {
                result[i]=1;
            }else
            {
                result[i]=0;
            }
 
        }
        for(i=0;i<=m-1;i++)
        {
            if(result[i])
            {
                printf("W");
            }else
            {
                printf("L");
            }
        }
        printf("\n");
    }
    return 0;
}

  

posted @   77695  阅读(897)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示