hdu 2999 Stone Game, Why are you always there?

36 篇文章 0 订阅

题目链接:点这里

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

3
1 5 1
1
1

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 int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define MK make_pair
#define ll long long
template<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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值