hdu2999Stone Game, Why are you always there?

来源:互联网 发布:手机版新闻软件 编辑:程序博客网 时间:2024/05/21 07:06

Stone Game, Why are you always there?

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 726 Accepted Submission(s): 250

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

Source
2009 Multi-University Training Contest 19 - Host by BNU

Recommend
chenrui

题意:
给一个k块石头的石头堆。每次可以取a[0],a[1],…a[n]个石头。先取不了的人输掉比赛。问谁赢?
思路:
博弈题。计算sg值即可。需要注意的是,一开始要去除a[0],a[1],…a[n]中的重复,否则会超时。
代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int m,n,k;int a[107];int sg[1007];int getsg(int x){    if(x<a[0])        return sg[x]=0;    if(sg[x]!=-1)        return sg[x];    int res=0;    int s[1007];    memset(s,0,sizeof s);    for(int i=0;i<x;i++)    {        for(int j=0;j<n&&x-i-a[j]>=0;j++)        {            if(sg[i]==-1)                sg[i]=getsg(i);            if(sg[x-i-a[j]]==-1)                sg[x-i-a[j]]=getsg(x-i-a[j]);            s[sg[i]^sg[x-i-a[j]]]=1;        }    }    for(int i=0;i<=1000;i++)    {        if(s[i]==0)        {            return sg[x]=i;        }    }}int b[1007];int main(){    int nn=0;    while(scanf("%d",&nn)!=EOF)    {        memset(sg,-1,sizeof sg);        for(int i=0;i<nn;i++)        {            int x;            scanf("%d",&b[i]);        }        sort(b,b+nn);        a[0]=b[0];        n=1;        for(int i=1;i<nn;i++)        {            if(b[i]!=b[i-1])            {                a[n++]=b[i];            }        }        int ans=0;        scanf("%d",&m);        for(int i=0;i<m;i++)        {            ans=0;            int x;            scanf("%d",&x);            ans^=getsg(x);             if(ans)            {                puts("1");            }            else                puts("2");        }    }    return 0;}
0 0
原创粉丝点击