HihoCoder - 1172 Game Game · Nim Game · 2 (Nim Game)

describe

Alice and Bob are going to play a game about coins this time:
N coins are arranged in a row, some heads up and some tails up, numbered 1..N from left to right . Now the two of them take turns flipping the coin, each time they can only turn over one coin that is heads up, and they can decide whether to flip any coin to the left of the coin after flipping one coin as they wish. (Flip to back or flip to front). Whoever flips the last coin up heads wins. Similarly, in this game, Alice still moves first, and both of them take the optimal strategy. For a given initial situation, will Alice win or Bob will win?

Tip: Turning Turtles

enter

Line 1: 1 positive integer N, representing the number of coins. 1≤N≤10,000
Line 2: 1 string, the ith character represents the state of the coin number i, 'H' for heads up, and 'T' for tails.

output

Line 1: 1 string, output "Alice" if Alice can win, otherwise output "Bob"

sample input

8
HHTHTTHT

Sample output

Bob

ideas

Quoting the official solution, very detailed

First, let's break down the situation, and we only consider one coin at a time.
Might as well set the situation where all the coins come up tails as situation 0.
Suppose there are now N coins, only the first one is heads up. This position can only be converted to a position where all coins are tails. We assume that the position is position 1, then position 1 can be transformed into position 0.
Suppose only the second one is face up. The situation can be transformed into: only coin 1 comes up heads; all coins come up tails. We assume this position is position 2, and position 2 can be transformed into position 1 and position 0.
In the same way, we can infer that the situation in which the i-th coin comes up heads is situation i, and situation i can be transformed into situation 0..i-1.

Now, let's consider splitting a given position into a set of positions for a single coin, for example given {HHTHTTHT}, where H means heads and T means tails. Then it is the current position = {Position 1, Position 2, Position 4, Position 7}. Each time we can change one of the positions, when position 0 occurs, remove it from the set.
Does this look like a Nim game? However, the truth is not so simple.

Further analysis, if there are i, j (j < i) two coins face up at the same time. We split this position into 2 single positions: position i and position j.
When reversing i we consider moving from position i to position j, then we have two positions j.
It means that the j-th piece has been reversed twice, that is, it has returned to the back-up state.
Then we get a property of this game: when there are two identical positions, it is equivalent to the merger of these two positions to become position 0.

This situation does not exist in the Nim game, so does it affect the state of the Nim game?
Let's think about it, in the Nim game, if there are two heaps of the same number, such as A[i]=A[j]. The xor operation we use when calculating the Nim game state, xor has commutative and associative laws. Then we can become:
(A[i] xor A[j]) xor Other
Since A[i] = A[j], so A[i] xor A[j] = 0. The above formula is actually:
0 xor Other
That is to say, in the original Nim game, if there are two piles with the same number, in fact, the two piles have no effect on the overall situation, and it can be considered that the two pairs are merged into one A heap with a quantity of 0.

code

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
const int N=10000+50;
int main()
{
    string s;
    int n,ans=0;
    cin>>n>>s;
    for(int i=0; i<n; i++)
        if(s[i]=='H')ans^=i+1;
    puts(ans==0?"Bob":"Alice");
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688867&siteId=291194637