HDU 3032 nim or not nim? (game SG to play the table to find the law)

Source: Internet
Author: User

Transmission Door


nim or not nim?Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1457 Accepted Submission (s): 720


problem Descriptionnim is a two-player mathematic game of strategy in which players take turns removing objects From distinct heaps. On each turn, a-player must remove at least one object, and may remove any number of objects provided they all come from T He same heap. The

Nim is usually played as a misere game, with which the player to take the last object loses. Nim can also is played as a normal play game, which means that the person who makes the last move (i.e., who takes the Las T object) wins. This was called normal play because most games follow this convention, even though Nim usually does not.

Alice and Bob are tired of playing Nim under the standard rule, so they make a difference by also allowing the play Er to separate one of the heaps into and smaller ones. That's, each turn the player could either remove any number of objects from a heap or separate a heap into a to the other smaller ones , and the one who takes the last object wins.  
InputInput contains multiple test cases. The first line was an integer 1≤t≤100, the number of test cases. Each case begins with a integer N, indicating the number of the heaps, the next line contains N integers s[0], s[1], .... , S[n-1], representing heaps with s[0], s[1], ..., s[n-1] objects respectively. (1≤n≤10^6, 1≤s[i]≤2^31-1) 
Outputfor each test case, output a line which contains either "Alice" or "Bob", which are the winner of this game. Alice would play first. Asume they never make mistakes. 
Sample Input
232 2 323 3
 
Sample Output
Alicebob
 
Sourcemulti-university Training Contest 13-host by hits 
Main topic:

There are two people Alice and Bob are playing a game, the content of the game is a total of M heap of stones, everyone has two operations:
1. Can take any number of each heap but not to take;

2. You can change the current heap of stones (the current number of stones must be >=2) into two piles;

Finally, who wins first, Alice gets


Problem Solving Ideas:

First we look at the data range, s[i]<2^31-1, so we can not directly use the SG function, but we could first make a table, and then see what the law, first we analyze, if there is no second operation behind then this is a simple NIM game, is to ask for a difference or value on the line, then add the second operation, then consider its successor state, his later state, sg[i] = mex{i-1,i-2 ... SG[I-1]^SG[1],SG[I-2]^SG[2]}; So we can make a table through this successor State, through which we find some rules, that is, when x%4==0, sg[x] = x-1, when x%4==3, sg[x] = x+ 1, the other case is sg[x] = x;


My Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace    std;const int maxn = 1e3+5;int sg[maxn];int get_sg (int x) {if (sg[x]!=-1) return sg[x];    int HASH[MAXN];    memset (hash, 0, sizeof (hash));    for (int i=x-1;i>=0;i--) HASH[GET_SG (i)]=1;    for (int j=1; j<=x/2; j + +) Hash[get_sg (x-j) ^get_sg (j)]=1;    int k;        for (k=0; k<maxn; k++) {if (!hash[k]) {return sg[x] = k;    }}}int Main () {/** test hit table **//* memset (sg,-1,sizeof (SG));    for (int i=0; i<50; i++) {printf ("sg[%d] =%d\n", I,get_sg (i));    }*/int T;    cin>>t;        while (t--) {int m;        int ans = 0;        cin>>m;            while (m--) {int x;            cin>>x;            if (x%4==0) ans ^= (x-1);            else if (x%4 = = 3) ans ^= (x+1);        else ans ^= x;    } if (ans)        Puts ("Alice");    Else puts ("Bob"); } return 0;}


HDU 3032 nim or not nim? (game SG to play the table to find the law)

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.