AcWing----892. Step-Nim Game (Java)_Mathematics Knowledge_Game Theory

Original title link

①. Title

Insert picture description here

②. Thinking

  • If the XOR value of the odd-numbered steps is 0 when the first move is made, the first move must be defeated, otherwise it must be won
  • Analyzing the odd stepped n1^n3^n5^n7.....n=0upper hand win, if equal to 0 sente losing
  • In the first move, if the odd-numbered step XOR is not 0, according to the classic Nim game, there is always a way to make the odd-numbered step XOR 0
  • When 后手移动偶数there are stones on the steps, the first hand only needs to move the stones moved by the opponent to the next step, so that the stones on the odd steps are equivalent to unchanged.
  • When 后手移动奇数there are stones on the steps, the exclusive OR of the odd steps left to the first player is not 0. According to the classic Nim game, the first player can always find a solution to make the odd steps exclusive or 0

③. Learning points

博弈论

④. Code implementation

import java.util.Scanner;

public class Main {
    
    
	/*
	 * 判断奇数为上的台阶 n1^n3^n5^n7.....n=0 先手必胜
	 * 	若不等于0 先手必败
	 */
	/*
	 * 先手时,如果奇数台阶异或非0,根据经典Nim游戏,先手总有一种方式使奇数台阶异或为0
	 * 当后手移动偶数台阶上的石子时,先手只需将对手移动的石子继续移到下一个台阶,这样奇数台阶的石子相当于没变,
	 * 当后手移动奇数台阶上的石子时,留给先手的奇数台阶异或非0,根据经典Nim游戏,先手总能找出一种方案使奇数台阶异或为0
	 */
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int res=0;
		for (int i =1; i <=n; i++) {
    
    
			int x=sc.nextInt();
			if(i%2==1) {
    
     //奇数位上的台阶
				res^=x;
			}
		}
		System.out.println(res==0?"No":"Yes");
	}

}

Insert picture description here

Guess you like

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