1. 程式人生 > >hihor 學習日記:hiho一下 第四十六週(SG函式)

hihor 學習日記:hiho一下 第四十六週(SG函式)

http://hihocoder.com/contest/hiho46/problem/1

這個SG函式算是講的比較易懂了,

AC程式碼:

#include <bits/stdc++.h>

using namespace std;
#define LL long long
const int Mod = 1e9 + 7;
const int maxn = 2e4 + 5;
const double eps = 0.00000001;
const int INF = 0x3f3f3f3f;


int SG[maxn], S[maxn];

void GetSG(int n) {
    SG[
0] = 0; for (int i = 1; i <= n; i ++) { memset(S, 0, sizeof(S)); S[0] = 1; for (int j = 1; j < i; j ++) { S[SG[j]] = 1; S[SG[j] ^ SG[i - j]] = 1; } for (int j = 1; ; j ++) if(!S[j]) { SG[i] = j; break
; } } } int main() { GetSG(20000); int N; cin >> N; int ans = 0; while(N --) { int x; cin >> x; ans ^= SG[x]; } if(ans) cout << "Alice" << endl; else cout << "Bob" << endl; return 0; }