Each pile can become two piles of any size not larger than the original pile
That is, a[i] can be split into (b[i],b[j]), in order to avoid repeating the stipulation b[i]>=b[j], namely:a[i]>=b[i]>=b[j]
It is equivalent to splitting a position into two positions. According to the SG function theory, the SG value of multiple independent positions is equal to the XOR sum of the SG values of these positions.
So the state that needs to be stored issg(b[i])^sg(b[j])
③. Learning points
博弈论_SG函数
④. Code implementation
import java.util.Arrays;import java.util.HashSet;import java.util.Scanner;publicclassMain{
staticint N=110;staticint[] f=newint[110];staticint n;publicstaticvoidmain(String[] args){
Scanner sc =newScanner(System.in);
n = sc.nextInt();int res=0;
Arrays.fill(f,-1);for(int i =0; i <n; i++){
res^=sg(sc.nextInt());}//为0表示必败局面,不为0表示必胜局面
System.out.println(res==0?"No":"Yes");}//sg模板基础进行修改staticintsg(int x){
if(f[x]!=-1){
return f[x];//若两个石堆中石子数相同,则它们的SG值也相同}//set每次递归都会刷新
HashSet set =newHashSet<>();for(int i =0; i<x; i++){
for(int j =0; j <=i; j++){
//新堆规模必须更小//公式 sg(i,j)=sg(i)^sg(j)//由SG函数理论,多个独立局面的SG值,等于这些局面SG值的异或和
set.add(sg(i)^sg(j));}}//当递归回第一层时,返回SG(x1)的值,即要用来异或的值//计算mex()for(int i =0;; i++){
if(!set.contains(i)){
return f[x]=i;}}}}