POJ2069Nim

来源:互联网 发布:java调用dll JNI 编辑:程序博客网 时间:2024/06/06 02:37

易水人去,明月如霜。

Description

Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones on the table (we know the number of stones exactly). We play in turn and at each turn, you or I can remove on to four stones from the heap. You play first and the one who removed the last stone loses.
In this game, you have a winning strategy. To see this, you first remove four stones and leave 96 stones. No matter how I play, I will end up with leaving 92 - 95 stones. Then you will in turn leave 91 stones for me (verify this is always possible). This way, you can always leave 5k+1 stones for me and finally I get the last stone, sigh. If we initially had 101 stones, on the other hand, I have a winning strategy and you are doomed to lose.

Let's generalize the game a little bit. First, let's make it a team game. Each team has n players and the 2n players are seated around the table, with each player having opponents at both sides. Turn around the table so the two teams play alternately. Second, let's vary the maximum number of stones each player can take. That is, each player has his/her own maximum number of stones he/she can take at each turn (The minimum is always one). So the game is asymmetric and may even be unfair.

In general, when played between two teams of experts, the outcome of a game is completely determined by the initial number of stones and the maximum number of stones each player can take at each turn. In other words, either team has a winning strategy.

You are the head-coach of a team. In each game, the umpire shows both teams the initial number of stones and the maximum number of stones each player can take at each turn. Your team plays first. Your job is, given those numbers, to instantaneously judge whether your team has a winning strategy.

Incidentally, there is a rumor that Captain Future and her officers of Hakodate-maru love this game, and they are killing their time playing it during their missions. You wonder where the stones are? Well, they do not have stones but do have plenty of balls in the fuel containers!

Input

The input is a sequence of lines, followed by the last line containing a zero. Each line except the last is a sequence of integers and has the following format.

n S M1 M2 . . . M2n

where n is the number of players in a team, S the initial number of stones, and Mi the maximum number of stones ith player can take. 1st, 3rd, 5th, ... players are your team's players and 2nd, 4th, 6th, ... the opponents. Numbers are separated by a single space character. You may assume 1 <= n <= 10, 1 <= Mi <= 16, and 1 <= S < 2^13.

Output

The output should consist of lines each containing either a one, meaning your team has a winning strategy, or a zero otherwise.

Sample Input

1 101 4 41 100 4 43 97 8 7 6 5 4 30

Sample Output

011
团体尼姆赛:传统的尼姆游戏由两名玩家进行,在一堆石头中,双方轮流取走任意合法数量块石头,取走最后一块石头的玩家落败。多人尼姆游戏将参赛人数拓展至两个队伍,每支队伍有n名队员交错入座,单次分别能最多取走Mi块石头,取走S块石头中的最后一块的队伍失败,求第一支队伍是否有必胜策略?

思路:枚举每一个人拿多少块进行深搜,判断即可

#include <cstdio>#include <cstring>#include <iostream>#include <cmath>using namespace std;int read(){    int s=0,f=1;    char ch;    ch=getchar();    while(ch<'0'||ch>'9')    {        if(ch=='-')         f*=-1;        ch=getchar();    }    while(ch>='0'&&ch<='9')    {        s=s*10+ch-48;        ch=getchar();    }    return f*s;}int dp[22][8888];int a[22]; int n;int dfs(int x,int S){    if(dp[x][S]!=-1) return dp[x][S];    for(int i=1;i<=a[x];i++)    {        int t=S-i;        if(t<0) break;        int yy;        if(x+1>=2*n) yy=0;        else yy=x+1;        if(dfs(yy,t)==0) return dp[x][S]=1;    }    return dp[x][S]=0;}int main(){while(1){    n=read();    if(!n) break;    int s;    s=read();    for(int i=0;i<2*n;i++)        a[i]=read();    memset(dp,-1,sizeof(dp));    for(int i=0;i<2*n;i++)    {        dp[i][0]=1;    }  int ans=dfs(0,s);  printf("%d\n",ans);} return 0;}


0 0
原创粉丝点击