题目:给出一串连续的K个石头,一个集合allow,每次取出若干个连续的石头(数量必须为集合allow中的),最后取完的获胜,问有没有必胜策略。
思路:求出sg值
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3fn
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
//0x3f3f3f3f
const int maxn=1e4+50;
int allow[105],N;
int sg[maxn];
int get_sg(int n){
if(sg[n]!=-1)
return sg[n];
bool mex[maxn];
mm(mex,false);
for(int i=1;i<=N;i++){
if(n<allow[i])
break;
for(int j=0;j+allow[i]<=n;j++){
int k=n-j-allow[i];
int pos=(get_sg(j)^get_sg(k));
mex[pos]=true;
}
}
for(int i=0;;i++)
if(!mex[i]){
sg[n]=i;
break;
}
return sg[n];
}
int main(){
int M,K;
while(~scanf("%d",&N)){
for(int i=1;i<=N;i++)
scanf("%d",&allow[i]);
sort(allow+1,allow+N+1);
N=unique(allow+1,allow+N+1)-allow-1;
mm(sg,-1);
scanf("%d",&M);
while(M--){
scanf("%d",&K);
int ans=get_sg(K);
if(ans>0)
printf("1\n");
else
printf("2\n");
}
}
return 0;
}