题目:有S个石子,有两个队,每个队有n个人,每个人每次有数量限制,取最后一个石子的输,0,2,4,6,...属于1队,其余的属于2队,按0,1,2,3,4,..,2n,0,1,2,....的顺序轮流操作,问先手必胜还是先手必败,1队先手。
思路:利用NP状态定理解决
代码:
#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 0x3f3f3f3f
#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+5;
int sg[25][maxn];
int num[25];
int n,S;
int get_sg(int id,int stone){
if(sg[id][stone]!=-1)
return sg[id][stone];
if(stone==1) return sg[id][stone]=0;
if(stone==0) return sg[id][stone]=1;
sg[id][stone]=0;
for(int i=1;i<=num[id]&&stone-i>=0;i++)
if(get_sg((id+1)%(2*n),stone-i)==0)
sg[id][stone]=1;
return sg[id][stone];
}
int main(){
while(~scanf("%d",&n)){
if(!n) break;
scanf("%d",&S);
for(int i=0;i<2*n;i++)
scanf("%d",&num[i]);
mm(sg,-1);
int ans=get_sg(0,S);
if(ans>0) printf("1\n");//先手必胜
else printf("0\n");//先手必败
}
return 0;
}