hdu 3389 Game(Nim)

portal

First draw a transfer diagram on paper:

Box 134 can no longer transfer cards to other boxes, and the cards in other boxes will eventually be transferred to box 134 after several steps of transfer.

Specifically, the boxes with n % 6 == 0 or 2 or 5 are transferred to 1 3 4 after an odd number of steps , and the others must be transferred to an even number of steps .

 

Let's prove that all cards in the even-step box are in a must-lose state .

Because no matter how much the first hand moves the cards from the even-numbered box, the second hand must be able to move the cards one more box forward until it reaches 1 3 4.

 

For only one box with cards and that box is an odd-move box, the first mover wins .

It's very simple, according to the above conclusion, just move all the cards in the odd-numbered step box down one box first. This shifts to a state of first-mover-defeat.

 

The whole game can be regarded as the sum game of several sub-games, the even-numbered step box is not considered, only the cards in the odd-numbered step box are considered, which is equivalent to a Nim game with n piles of stones.

Removing k cards from an odd-numbered box is equivalent to removing k stones from a certain pile of stones. It is equivalent to taking all the stones, all the cards are in the even-numbered box, and we have proved that this state is a must-fail state.

 

So in the code, you only need to XOR the number of cards in the odd-numbered step box to find a Nim sum, and you can judge the outcome.


#include<stdio.h>
using namespace std;

int main(){
	int Case = 1;
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		
		int ans=0;
		for(int i=1;i<=n;i++){
			int x;
			scanf("%d",&x);
			if(i%6==0||i%6==2||i%6==5)
			years^=x;
		}
		printf("Case %d: ",Kase++);
		if(ans==0)printf("Bob\n");
		else printf("Alice\n");
	}
	return 0;
}

Guess you like

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