D - ShaatChara Gym - 101353D(尼姆博弈)

题目链接:https://vjudge.net/problem/Gym-101353D

题目:

Score: 100
CPU: 1s
Memory: 1024MB
Meena and Razu have been playing Shaat Chara for many years. But they think the game is too
violent. So, they came up with a new game. In this game, instead of just 1 pile, they start with n piles
of stones. The game is for two players. Each player takes turn alternatively. In each turn a player
can select any of the piles that has at least 1 stone left, and then remove some stone (at least one)
from it. The game goes on like this until there are no stones left. The player who can’t make a move
loses.
At first, Razu was very happy because there was no chance of injury in this game. But he quickly
grew frustrated, because Meena was winning all the games. Meena is very clever. She always plays
optimally. That means, if there is a chance to win, she will always win. So, Razu came to you with a
particular state of the game. He wants to know how many possible moves he may take such that
Meena’s win does not become guaranteed.
Input
In a single line, you will be given T, the number of test cases.
T test cases follow. In each test case, you will be given n, the number of piles. Then in a single line,
you will be given n numbers ai, the number of stones in the ith (1 <=i <= n) pile.
Output
You have to print the case number first in the form “Case 1: ”.
For each case you have to print only the number of winning moves from that particular state of the
game. See sample for clarification.
Sample
Input Output
3
1
1
2
1 1
3
1 1 1
Case 1: 1
Case 2: 0
Case 3: 3
Note that, winning moves mean the moves that will lead Meena to a losing state. For example, if the
current state is {1, 3}, Razu can remove 2 stone from the second pile, which is a winning state. Because
this move will lead to the state {1, 1}. Then Meena will remove 1 stone from any pile, and Razu will
remove the other in the next turn. And Meena will lose the game, because there will be no remaining
stone. But if Razu removed only 1 stone from the second pile, that will lead to the state {1, 2}, which is a
winning state for Meena. Thus Meena will win.
Also note that, this is a normal nim game of n piles. In a normal Nim game, the player making the first
move has a winning strategy if and only if the nim-sum of the sizes of the piles is nonzero. Otherwise, the
second player has a winning strategy. Nim sum of n piles is defined as follows.
Nim_sum = a1 ^ a2 ^ a3 ^……… ^ an , where ^ is the Bitwise X-OR operation.
Now you might be wondering how it would even work. To put it simply, think of it this way. If the theory is
correct, then the first player wins when the nim sum of the current piles are non-zero, and loses
otherwise. Now, consider a case where he is in a zero nim-sum position. He must make a change to the
piles, which will lead the nim-sum to non-zero, which is a winning state for the opponent. On the other

尼姆博弈:有任意堆物品,每堆物品的个数是任意的,双方轮流从中取物品,每一次只能从一堆物品中取部分或全部物品,最少取一件,取到最后一件物品的人获胜。

结论就是:把每堆物品数全部异或起来,如果得到的值为0,那么先手必败,否则先手必胜。

题意: 就是假如第一个人能胜利的情况下,他的第一次取物品,能从哪几堆取可以继续保持胜利,输出这个堆数。能让他保持胜利的方式是 他取完后,所有物品数的异或值变为0。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>

using namespace std;

const int maxn=10050;
int arr[maxn];

int main()
{
    int t,n,cnt=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int tmp=0,ans=0;
        for(int i=0; i<n;++i)
        {
            scanf("%d",&arr[i]);
            tmp^=arr[i];
        }
        for(int i=0; i<n; i++)
            if(arr[i]>(tmp^arr[i]))//大于号右边的表达式的意思是除了第i堆,
            //其它所有堆的物品数的异或值。如果第i堆的物品数大于这个异或值,
            //第i堆的物品数就可以减去一个值等于这个异或值,两个相等的数异或为0.
            ans++;
        printf("Case %d: %d\n",++cnt,ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36300700/article/details/79837924