HDU 3032 nim or not nim? (SG function Solver)

Source: Internet
Author: User

Nim or not Nim?
Problem Descriptionnim is a two-player mathematic game of strategy in which players take turns removing objects from DiSTI NCT 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.

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 player to SE Parate 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

Source2009 multi-university Training Contest 13-host by hits

Main topic:

Alice and Bob take turns to fetch n heaps of stones, each pile of s[i], Alice first. Every time you can take a random stone from a random pile, you can divide a pile of stones into two small piles. Take the winner first.

(1≤n≤10^6, 1≤s[i]≤2^31-1)


Problem Solving Ideas:

For a given direction-free graph. Define the Sprague-grundy function for each vertex of the graph G for example the following: G (x) =mex{g (y) | Y is the successor of X, where G (x) is sg[x]
For example: Take a stone problem, there are 1 heap n stones, each time only can take {1. 3,4} a stone. Take the stone first and win. So what is the SG value for each number?
Sg[0]=0,
N=1, you can take {1} stones, remaining {0}. Mex{sg[0]}={0}. therefore sg[1]=1;
When n=2, you can take {1} of stones. Remaining {1}, mex{sg[1]}={1}. therefore sg[2]=0;
When n=3. Be able to take {1,3} stones, remaining {2,0} each. mex{sg[2],sg[0]}={0,0}, so sg[3]=1;
N=4, you can take {1,3,4} stones. Remaining {3,1,0}, mex{sg[3],sg[1],sg[0]}={1,1,0}, so sg[4]=2;
When n=5. Be able to take {1,3,4} a stone. Remaining {4,2,1}, mex{sg[4],sg[2],sg[1]}={2,0,1}, so sg[5]=3;
And so on .....
x012345678....
SG[X] 010123201....

So, for this question:

Sg[0]=0

Sg[1]=mex{sg[0]}=1

sg[2]=mex{sg[0],sg[1],sg[1,1]}=mex{0,1,1^1}=2;

sg[3]=mex{sg[0],sg[1],sg[2],sg[1,2]}=mex{0,1,2,1^2}=mex{0,1,2,3}=4;

sg[4]=mex{sg[0],sg[1],sg[2],sg[3],sg[1,3],sg[2,2]}=mex{0,1,2,4,5,0}=3;

..............................................................................

able to discover: sg[4*k+1]=4*k+1,sg[4*k+2]=4*k+2, sg[4*k+3]=4*k+4,sg[4*k+4]=4*k+3


Problem Solving Code:


#include <iostream>using namespace Std;int main () {    int t;    cin>>t;    while (t-->0) {        int sg=0,n,x;        cin>>n;        for (int i=0;i<n;i++) {            cin>>x;            if (x%4==0) sg^=x-1;            else if (x%4==3) sg^=x+1;            else sg^=x;        }        if (SG) cout<< "Alice" <<endl;        else cout<< "Bob" <<endl;    }    return 0;}





Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

HDU 3032 nim or not nim? (SG function Solver)

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.