POJ 2068 Nim (game dp)

Source: Internet
Author: User

Nim
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1403 Accepted: 791

Description

Let ' s play a traditional game Nim. You and I is 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 over to a four-stones from the heap. You play first and the one who removed the last stone loses.
The this game has a winning strategy. To see this, your first remove four stones and leave stones. No matter how I play, I'll end up with leaving 92-95 stones. Then you'll in turn leave stones to me (verify this is always possible). This is 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 had a winning strategy and you were 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 is seated around the table, with each player have opponents at both sides. Turn around the table so the the teams play alternately. Second, let's vary the maximum number of stones each player can take. That's, each player have his/her own maximum number of stones he/she can take at each turn (the minimum are always one). So the game was asymmetric and may even was unfair.

In general, when played between-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 be in each turn. In the other words, the 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 ta Ke at each turn. Your team plays first. Your job is, given those numbers, to instantaneously judge whether Your team have a winning strategy.

Incidentally, there is a rumor that captain the future and her officers of the Hakodate-maru love this game, and they are killing Their time playing it during their missions. You wonder where the stones is? Well, they does not has stones but does has 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 have 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 is your team ' s players and 2nd, 4th, 6th, ... the opponents. Numbers is separated by a single space character. Assume 1 <= n <=, 1 <= Mi <=, and 1 <= S < 2^13.

Output

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

Sample Input

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

Sample Output

011

Source

Japan 2001


title: There are two teams from the stone heap to stone, each team has n people, each time a limited number of stones to take system, and then

Take the last piece of the team to lose. Now ask the first team (1,3,5,7 ...) if they can win the game.

train of thought: Dp[i][j] represents the first I personal take, stone Heap remaining J block Stone. When J is 0, there is no stone, this time is wins, is 1.

In the successor, there must be a winning state.


#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int m=1<<14; const int Maxn=50;int dp[maxn][m],a[maxn],tol,n;void Initial () {    memset (dp,-1,sizeof (DP));} void input () {    scanf ("%d", &tol);    for (int i=0;i<2*n;i++) scanf ("%d", &a[i]);} int DP (int x,int num) {    if (dp[x][num]!=-1)  return dp[x][num];    if (num==0)  return dp[x][num]=1;    dp[x][num]=0;    for (int i=1;i<=a[x] && i<=num;i++)        if (! DP ((x+1)% (2*n), num-i))            dp[x][num]=1;    return dp[x][num];} int main () {    while (scanf ("%d", &n)!=eof)    {        if (n==0) break  ;        Initial ();        Input ();        printf ("%d\n", DP (0,tol));    }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2068 Nim (game dp)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.