S-Nim POJ - 2960 Nim + SG函数
Code:
#include<cstdio> #include<algorithm> #include<string> #include<cstring> using namespace std; #define maxn 10003 int step[maxn],SG[maxn],m,ans,l,a,k; bool vis[maxn]; int main(){ //freopen("input.in","r",stdin); while(scanf("%d",&k) && k){ for(int i=1;i<=k;++i) scanf("%d",&step[i]); sort(step+1,step+1+k); SG[0]=0; for(int i=1;i<maxn;++i){ for(int j=1;j<=k&&step[j]<=i;++j)vis[SG[i-step[j]]]=true; for(int j=0;;++j)if(!vis[j]) { SG[i]=j; break; } for(int j=1;j<=k&&step[j]<=i;++j)vis[SG[i-step[j]]]=false; } scanf("%d",&m); while(m--){ scanf("%d",&l); ans=0; for(int i=1;i<=l;++i) scanf("%d",&a), ans^=SG[a]; if(ans==0) printf("L"); else printf("W"); } printf("\n"); } return 0; }