hdu 2999 Stone Game, Why are you always there?

来源:互联网 发布:java dll 反编译工具 编辑:程序博客网 时间:2024/05/17 08:22

题目链接:点这里。

Problem Description

“Alice and Bob are playing stone game...”“Err.... Feel bored about the stone game? Don’t be so, because stone game changes all the time!”“What the hell are they thinking for?”“You know, whenever Alice is trying to make fun of Bob, she asked him to play stone game with him.”“Poor Bob... What’s the rule today?”“It seems Alice only allows some fixed numbers of continuous stones can be taken each time. And they begin with one string of stones.”“A string? Formed as a circle or a line?”“A line.”“Well, I think I may help Bob with that.”“How?”“I may tell him to skip this round if he has no chance to win.”“Good idea maybe, I mean, Alice always let Bob play first, because she think herself is smart enough to beat Bob no matter how.”“Yes, she’s actually right about herself. Let me see if Bob has a chance to win...”......

Input

There are multiple test cases, for each test case:The first line has a positive integer N (1<=N<=100).The second line contains N positive integers, a1, a2 ... an, separated by spaces, which indicate the fixed numbers Alice gives.The third line, a positive integer M. (M<=1000)Following M lines, one positive integer K (K<=1000) each line. K means in this round, the length of the stone string.

Output

For each K, output “1” if Bob has a chance to win, output “2” if Bob has no chance, or “0” if it’s undeterminable.

Sample Input

31 5 111

Sample Output

1

【题意】

开始时有一排石子,两个人轮流取,其中只能选择连续的一段取走。不能进行操作的人失败。其中告诉你能选择的一段的长度。问你先手是否能必胜。

【分析】

如果不考虑石子是一排的话,那就是最单纯的nim博弈了。考虑一排其实也简单,无非就是后记状态变成了两个状态的异或和,稍微改改就行了。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<map>#include<queue>#include<sstream>using namespace std;#define rep0(i,l,r) for(int i = (l);i < (r);i++)#define rep1(i,l,r) for(int i = (l);i <= (r);i++)#define rep_0(i,r,l) for(int i = (r);i > (l);i--)#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)#define MS0(a) memset(a,0,sizeof(a))#define MS_1(a) memset(a,-1,sizeof(a))#define MSinf(a) memset(a,0x3f,sizeof(a))#define sin1(a) scanf("%d",&(a))#define sin2(a,b) scanf("%d%d",&(a),&(b))#define sll1(a) scanf("%lld",&(a))#define sll2(a,b) scanf("%lld%lld",&(a),&(b))#define sdo1(a) scanf("%lf",&(a))#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))#define inf 0x3f3f3f3f//#define lson i<<1,l,mid//#define rson ((i<<1)|1),mid+1,r#define uint unsigned inttypedef pair<int,int> PII;#define A first#define B second#define pb push_back#define MK make_pair#define ll long longtemplate<typename T>void read1(T &m) {    T x=0,f=1;    char ch=getchar();    while(ch<'0'||ch>'9') {        if(ch=='-')f=-1;        ch=getchar();    }    while(ch>='0'&&ch<='9') {        x=x*10+ch-'0';        ch=getchar();    }    m = x*f;}template<typename T>void read2(T &a,T &b) {    read1(a);    read1(b);}template<typename T>void read3(T &a,T &b,T &c) {    read1(a);    read1(b);    read1(c);}template<typename T>void out(T a) {    if(a>9) out(a/10);    putchar(a%10+'0');}template<typename T>void outn(T a) {    if(a>9) out(a/10);    putchar(a%10+'0');    puts("");}///------------------------------------------------------------------------------------int num[1005];int len;int sg[1005];int getsg(int x){    if(sg[x]!=-1) return sg[x];    bool flag[1005];    memset(flag,false,sizeof(flag));    rep1(i,0,len)    {        int t = num[i];///能切出来的长度        if(t>x) break;        rep1(j,0,x-t)        flag[getsg(j)^getsg(x-t-j)]=true;    }    rep0(i,0,1005) if(!flag[i]) return (sg[x]=i);}int main() {//    freopen("in.txt","r",stdin);    while(sin1(len)!=EOF)    {        MS_1(sg);        sg[0]=0;        int x;        rep0(i,0,len) read1(num[i]);        sort(num,num+len);        int xx=len;        len = 0;        rep0(i,1,xx)        {            if(num[i]-num[len]) num[++len]=num[i];        }        int que;        read1(que);        while(que--)        {            read1(x);            puts(getsg(x)?"1":"2");        }    }    return 0;}