hdu 2999 Stone Game, Why are you always there? 博弈论

SG函数应用!!

代码如下:

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 int sg[1002],a[101],n;
 8 int get_sg(int m)
 9 {
10     if(sg[m]!=-1) return sg[m];
11     bool vis[10000];
12     memset(vis,0,sizeof(vis));
13     for(int i=0;i<n&&a[i]<=m;i++)
14         for(int j=0;j<=m-a[i]-j;j++){
15         vis[get_sg(m-a[i]-j)^get_sg(j)]=1;
16     }
17     int i=0;
18     while(vis[i]) i++;
19     return sg[m]=i;
20 }
21 int main()
22 {
23     int i,j,m,p,k;
24     while(scanf("%d",&n)!=EOF){
25         for(i=0;i<n;i++) scanf("%d",&a[i]);
26         sort(a,a+n);
27         for(i=j=1;i<n;i++)
28         if(a[i]!=a[j-1]){
29             a[j++]=a[i];
30         }
31         n=j;
32         scanf("%d",&m);
33         memset(sg,-1,sizeof(sg));
34         sg[0]=0;
35         while(m--){
36             scanf("%d",&k);
37             puts(get_sg(k)?"1":"2");
38         }
39     }
40     return 0;
41 }
View Code

 

 

posted @ 2013-09-03 22:12  _随心所欲_  阅读(267)  评论(0编辑  收藏  举报