Acwing---891. Nim Game (Java)_Mathematics_Game Theory

Original title link

①. Title

Insert picture description here

②. Thinking

  • The must-win state, when the first hand performs a certain operation, and the second hand is a must-defeat state, it is a must-win state for the first hand. That is, the first mover can go to a certain defeat state.
  • In the must-defeat state, no matter how the first player operates, when the second player is a must-win state, it is a must-defeat state for the first player. That is to say, you can't go to any one of the defeated states.
  • n piles of stones, each pile has a1 a2 a3 …an
  • The upper hand to win the state a1^a2^a3^a4...^an !=0so just get exclusive or not equal to 0, so that there is emulated FLAC XOR to zero
  • State losing the upper hand a1^a2^a3^a4...^an =0so have the upper hand XOR 0, no matter how can not let take flip XOR to zero
  • At the end of the operation, the number of stones in each pile is 0^0^0^…0=0
  • During operation, if a1⊕a2⊕…⊕an=x≠0. Then the player can definitely change the XOR result to 0 by taking away a certain pile of stones.
  • If a1⊕a2⊕…⊕an=0, then no matter how the player takes it, it will inevitably lead to the inability to XOR the backhand to 0

③. Learning points

博弈论

④. Code implementation

import java.util.Scanner;

public class Main {
    
    
	/*
	 * n堆石子 每一堆有a1 a2 a3 ...an
	 * 先手必胜态 a1^a2^a3^a4...^an !=0  所以先手异或不等于0,存在取法让后手异或为0
	 * 先手必败态a1^a2^a3^a4...^an =0  所以先手异或得0,无论怎么取都无法让后手异或为0
	 */
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int res=1;
		while(n-->0) {
    
    
			int a=sc.nextInt();
			res^=a;
		}
		System.out.println((res^1)!=0?"Yes":"No");
	}

}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45480785/article/details/114104857