-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path3727SGA.cpp
103 lines (103 loc) · 2.01 KB
/
3727SGA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdio>
#include <cstring>
const int size = 100005, s = 12;
int cache[size];
int SG1(int x) {
if(x == 0)
return 0;
if(cache[x] != -1)
return cache[x];
bool use[size] = {};
for(int i = 1; i <= x; ++i)
use[SG1(x - i)] = true;
for(int i = 0; i < size; ++i) {
if(!use[i]) {
cache[x] = i;
return i;
}
}
throw;
}
int SG1Fast(int x) {
return x;
}
int SG2(int x) {
if(x == 0)
return 0;
if(cache[x] != -1)
return cache[x];
bool use[size] = {};
for(int i = 1; i <= x; i *= s)
use[SG2(x - i)] = true;
for(int i = 0; i < size; ++i) {
if(!use[i]) {
cache[x] = i;
return i;
}
}
throw;
}
int SG2Fast(int x) {
if(s & 1)
return x & 1;
int val = x % (s + 1);
if(val == s)
return 2;
return val % 2;
}
int SG3(int x) {
if(x == 0)
return 0;
if(cache[x] != -1)
return cache[x];
bool use[size] = {};
for(int i = s; i <= x; ++i)
use[SG3(x - i)] = true;
for(int i = 0; i < size; ++i) {
if(!use[i]) {
cache[x] = i;
return i;
}
}
throw;
}
int SG3Fast(int x) {
return x / s;
}
int SG4(int x) {
if(x == 0)
return 0;
if(cache[x] != -1)
return cache[x];
bool use[size] = {};
for(int i = 1; i <= x; ++i)
use[SG4(x - i)] = true;
for(int i = 1; i < x; ++i)
use[SG4(i) ^ SG4(x - i)] = true;
for(int i = 0; i < size; ++i) {
if(!use[i]) {
cache[x] = i;
return i;
}
}
throw;
}
int SG4Fast(int x) {
if(x == 0)
return 0;
if(x % 4 == 3)
return x + 1;
if(x % 4 == 0)
return x - 1;
return x;
}
int main() {
memset(cache, -1, sizeof(cache));
for(int i = 0; i <= 100000; ++i) {
int va = SG2(i), vb = SG2Fast(i);
printf("%d %d %d\n", i, va, vb);
if(va != vb)
throw;
}
return 0;
}