26.拆分-Nim游戏 博弈论

 

 进阶版的SG函数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 110;
 4 int n;
 5 int f[N]; //sg的值
 6 int sg(int x) {
 7     if (f[x] != -1) {
 8         return f[x];
 9     }
10     unordered_set<int> S;
11     for (int i = 0; i < x; i++) {
12         for (int j = 0; j <= i; j++) {
13             S.insert(sg(i) ^ sg(j));
14         }
15     }
16     for (int i = 0;; i++) {
17         if (!S.count(i)) {
18             return f[x] = i;
19         }
20     }
21 }
22 int main() {
23     cin >> n;
24     memset(f, -1, sizeof f);
25     int res = 0;
26     while (n--) {
27         int x;
28         cin >> x;
29         res ^= sg(x);
30     }
31     if (res) {
32         cout << "Yes" << endl;
33     } else {
34         cout << "No" << endl;   
35     }
36     return 0;
37 }
原文地址:https://www.cnblogs.com/fx1998/p/13459018.html