游戏
游戏
https://ac.nowcoder.com/acm/problem/18388
SG定理:n个有向图游戏组成的组合游戏当且仅当SG值异或和等于0时先手必输,否则先手必胜。
直接求出每个状态的SG值,最后遍历每堆石头第一次取的情况,如果能满足异或和等于0即
留给对手一个必败状态,则方案数加1。
#include <bits/stdc++.h> #include <unordered_map> using namespace std; typedef long long ll; typedef unsigned long long ull; #ifdef LOCAL #define debug(x) cout << "[" __FUNCTION__ ": " #x " = " << (x) << "]\n" #define TIME cout << "RuningTime: " << clock() << "ms\n", 0 #else #define TIME 0 #endif #define hash_ 1000000009 #define Continue(x) { x; continue; } #define Break(x) { x; break; } const int mod = 998244353; const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; #define gc p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; inline int read(){ static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = gc; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = gc; if (ch == '-') sgn = true, ch = gc; while (ch >= '0'&& ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc; return sgn ? -x : x; } ll fpow(ll a, int b, int mod) { ll res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } int a[N]; int sg[N]; vector<int>G[N]; int vis[N]; int main() { #ifdef LOCAL freopen("C:/input.txt", "r", stdin); #endif int cnt = 0; for (int i = 1; i < N; i++) { cnt++; int k = sqrt(i); for (int j = 1; j <= k; j++) { if (i % j == 0) { vis[sg[i - j]] = cnt; if (j * j != i) vis[sg[i - i / j]] = cnt; } } for (int j = 0; j <= N - 10; j++) if (vis[j] != cnt) { sg[i] = j; break; } } int n; cin >> n; int sum = 0; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), sum ^= sg[a[i]]; int ans = 0; for (int i = 1; i <= n; i++) { sum ^= sg[a[i]]; int k = sqrt(a[i]); for (int j = 1; j <= k; j++) { if (a[i] % j == 0) { if ((sg[a[i] - j] ^ sum) == 0) ans++; if (j * j != a[i] && (sg[a[i] - a[i] / j] ^ sum) == 0) ans++; } } sum ^= sg[a[i]]; } cout << ans << endl; return TIME; }