Hdu3032 nim or not nim? (SG function)

Source: Internet
Author: User

Nim or not Nim?

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1710 Accepted Submission (s): 845


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

Test instructions: The deformation of the Nim game, n heap of stones, each time you can take any number of stones in a heap, or divide it into two piles of not 0, the last to win.

Analysis: Push the value of SG in one step ...

It is obvious that sg[0]=0, sg[1]=1;

Then the successor of the state 2 has: 0,1 and (1, 1), their SG value is 0,1,0; so sg[2]=2;

Then the successor of the state 3 has: 0,1,2 and (1, 2), their SG value is 0,1,2,3; so sg[3]=4;

Then the successor of the state 4 has: 0,1,2,3, (1, 3) and (2, 2), their SG value is 0,1,2,4,5,0; so sg[4]=3;

..........

Finally, according to the introduced SG value, we get: for all k >= 0, there is sg (4k+1) = 4k+1; sg (4k+2) = 4k+2; sg ( 4k+3) = 4k+4; sg (4k+4) = 4k+3.

#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm>  Using namespace Std;const double eps = 1e-6;const double pi = ACOs ( -1.0); const int INF = 1e9;const int MOD = 1e9+7; #define ll long Long#define CL (b) memset (A,b,sizeof (a)) #define Lson (i<<1) #define Rson ((i<<1) | |) #define N 1000010 int gcd (int a,int b) {return B?GCD (b,a%b): A;}    int main () {int t,n,x;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        int ans = 0;            for (int i=1; i<=n; i++) {scanf ("%d", &x);            if (x%4==0) ans ^= (x-1); else if (x%4==1| |            x%4==2) ans ^= x;        else ans ^= (x+1);        } if (ans) cout<< "Alice" <<endl;    else cout<< "Bob" <<endl; } return 0;}



Hdu3032 nim or not nim? (SG function)

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.