POJ2960 S-Nim


 1 /*
 2  POJ2960 S-Nim
 3  http://poj.org/problem?id=2960
 4  博弈论 SG函数
 5  *
 6  */
 7 
 8 #include <cstdio>
 9 #include <algorithm>
10 #include <cstring>
11 using namespace std;
12 const int Nmax=10005;
13 //s[]:可以取走的石子个数  
14 //sg[]:0~n的SG函数值  
15 //hhash[]:mex{}  
16 //#define test
17 int s[105],sg[Nmax],hhash[Nmax];       
18 void getSG(int n)//n为s[]的长度,即可取的种数  
19 {  
20     int i,j;
21     sg[0]=0;
22     //memset(sg,0,sizeof(sg));  
23     for(i=1;i<Nmax;i++)//Nmax为求解范围  
24     {  
25         memset(hhash,0,sizeof(hhash));  
26         for(j=1;j<=n;j++)
27             if(i>=s[j])
28                 hhash[sg[i-s[j]]]=1;  
29         for(j=0;j<Nmax;j++)    //求mes{}中未出现的最小的非负整数  
30         {  
31             if(hhash[j]==0)  
32             {  
33                 sg[i]=j;  
34                 break;  
35             }  
36         }  
37     }  
38 }  
39 
40 int main()
41 {
42     int n;
43     #ifdef test
44     freopen("2960.in","r",stdin);
45     #endif
46     while(scanf("%d",&n) && n)
47     {
48         //memset(s,0,sizeof(s));
49         for(int i=1;i<=n;i++)
50             scanf("%d",&s[i]);
51         getSG(n);
52         int t,a;
53         scanf("%d",&t);
54         while(t--)
55         {
56             int ans=0;
57             int k;
58             scanf("%d",&k);
59             while(k--) 
60             {
61                 scanf("%d",&a);
62                 ans^=sg[a];
63             }
64             if(!ans)
65                 printf("L");
66             else
67                 printf("W");
68         }
69         printf("\n");
70     }
71     return 0;
72 }

优质内容筛选与推荐>>
1、【HTML】HTML 基础
2、HTTP
3、面向对象例题
4、Gym - 101350G Snake Rana(容器原理)
5、【模板整合计划】高阶数据结构—分块


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号