HDU 1536 S-Nim(博弈论)
SG函数模版,学习ing...
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 int dp[10001],n; 7 int p[101]; 8 int sg(int x) 9 { 10 int i; 11 int flag[101]; 12 memset(flag,0,sizeof(flag)); 13 if(dp[x] != -1) 14 return dp[x]; 15 if(x < p[0]) 16 return dp[x] = 0; 17 for(i = 0;i < n&&x >= p[i];i ++) 18 { 19 if(dp[x-p[i]] == -1) 20 { 21 dp[x-p[i]] = sg(x-p[i]); 22 } 23 flag[dp[x-p[i]]] = 1; 24 } 25 for(i = 0;;i ++) 26 if(!flag[i]) 27 return dp[x] = i; 28 } 29 int main() 30 { 31 int m,i,j,k,temp,ans; 32 while(scanf("%d",&n)!=EOF) 33 { 34 memset(dp,-1,sizeof(dp)); 35 dp[0] = 0; 36 if(n == 0) break; 37 for(i = 0;i < n;i ++) 38 scanf("%d",&p[i]); 39 sort(p,p+n); 40 scanf("%d",&k); 41 for(i = 1;i <= k;i ++) 42 { 43 scanf("%d",&m); 44 ans = 0; 45 for(j = 0;j < m;j ++) 46 { 47 scanf("%d",&temp); 48 ans ^= sg(temp); 49 } 50 if(ans == 0) 51 printf("L"); 52 else 53 printf("W"); 54 } 55 printf("\n"); 56 } 57 return 0; 58 }