这个博弈论设计到Mex运算和SG函数,比上次得稍微复杂一点,但是宗旨不变,就是利用异或运算,0为必败,非零必胜。
题目
定理1:对于集合S,mex(S)=mex({x1,x2…})=S中 没有出现的 最小非负整数
定理2:sg(n)=mex({sg(i1),sg(i2),sg(i3)…})。
定理3:sg(G1)^ sg(G2) ^ sg(G3)…sg(Gn)为n个有向图的异或和,对于n个有向图游戏,这个异或和就是它的答案(和nim类似,这个定理感兴趣可以去证明)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int n, m;
static int N = 110, M = 10010;
static int f[] = new int[M], pickup[] = new int[N];
public static void main(String[] args) throws IOException {
String[] s = br.readLine().split(" ");
m = Integer.parseInt(s[0]);
s = br.readLine().split(" ");
for (int i = 0; i < m; i++) pickup[i] = Integer.parseInt(s[i]);
Arrays.fill(f, -1);
int res = 0;
s = br.readLine().split(" ");
n = Integer.parseInt(s[0]);
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(s[i]);
res ^= sg(x);
}
if (res != 0) pw.println("Yes");
else pw.println("No");
pw.flush();
pw.close();
br.close();
}
public static int sg(int x) {
if (f[x] != -1) return f[x];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < m; i++) {
int sum = pickup[i];
if (x >= sum) set.add(sg(x - sum));
}
for (int i = 0; ; i++)
if (!set.contains(i))
return f[x] = i;
}
}