Game-nim Games

Source: Internet
Author: User

1069 Nim Games

Base time limit: 1 seconds space limit: 131072 KB score: 0 Difficulty: Basic problem

There are n heaps of stones. A B Two people take turns, a take first. Each time can only from a bunch of a few, can be a bunch of all take away, but not to take, the last 1 stones to win the people. Suppose a B is very clever, there is no mistake in the process of taking the stone. Give the number of N and each heap of stones, and ask who can win the game at the end. For example: 3 piles of gravel, 1 capsules per heap. A take 1, B take 1, at this time there are 1 piles left, so a can get the last 1 stones.

Input

Line 1th: A number n, indicating that there are n heap of stones. (1 <= n <= 1000) 2-n + 1 rows: n heap of stones. (1 <= a[i] <= 10^9)

Output

If a WINS output A, if B wins output B.

Input example

3 1 1 1

Output example

A

"Idea handling from http://www.cnblogs.com/easonliu/p/4472541.html"

This time we are talking about an old and classic game problem: the Nim game.

The Nim game is the classic Fair Combo Game (ICG), for the ICG game we have the following definitions:
1, two players;
2. Two players take turns, each action can choose one in the limited legal operation set;
3, any kind of game possible situation (position), the legal operation set only depends on the situation itself; the change of situation is called "move".
4. If the player's legal operation set is empty when the player is in the turn, the contestant will be awarded a negative.

For the third, we have a further definition of position, we will position into two categories:
P-position: In the current situation, the initiator will be defeated.
N-position: In the current situation, the initiator win.

They have the following properties:
1. The situation that the legal operation set is empty is p-position;
2. The situation that can be moved to P-position is n-position;
3. All movements can only reach the n-position situation is p-position.

In this game, we already know that a[] = {0,0,..., 0} is in P situation, then we can deduce all possible situations by reverse enumeration, the total number of states is a[1]*a[2]*...*a[n]. And each time the state shifts a lot.
It's a great time, but it's a viable approach.

Of course, we'll talk about this topic here, which means it's definitely not that complicated. Yes, there's a very magical conclusion to this game:

for a situation, when and only if A[1] xor a[2] xor ... xor a[n] = 0 o'clock, the situation is p.

The evidence for this conclusion is as follows:
1. All 0 states are P-situation, i.e. a[i]=0, then a[1] xor a[2] xor ... xor a[n] = 0.
2. From any one a[1] xor a[2] xor ... xor a[n] = k! = 0 The state can be moved to A[1] XOR a[2] xor ... xor a[n] = 0 state. Because of the particularity of the XOR calculation, we know that there must be a a[i] the highest bit is the same as 1 of the highest bit of K, then there is bound to be a[i] XOR K < a[i], so we can change the value of a[i] to A[i] ', make a[1] xor A[2] xor ... xor a[ I] ' xor ... xor a[n] = 0.
3. For any situation, if A[1] xor a[2] xor ... xor a[n] = 0, then there is no one move can make the new situation a[1] XOR A[2] xor ... xor a[n]! = 0. Because of the particularity of the XOR calculation, we can know that there must be an even number of 1 o'clock in this position 1 will be eliminated. Changing only one a[i] will, in any case, change the number of 1, leading to a[1] XOR a[2] xor ... xor a[n]! = 0.
The above three satisfies the transfer nature of the n,p situation in the ICG game, so the correctness of this conclusion is also proved.

The code is as follows

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace Std;
int a,n,p;
int main ()
{
cin>>n;
cin>>p;
n--;
while (n>0) {
cin>>a;
P^=a;
n--;
}
if (p==0) cout<< "B" <<endl;
else cout<< "A" <<endl;
return 0;
}

Game-nim Games

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.