forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSprague-Grundy.cpp
70 lines (66 loc) · 827 Bytes
/
Sprague-Grundy.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
#include <cstdio>
#include <cstring>
using namespace std;
int k, s[100], sg[10001];
bool has[10001];
void init_sg()
{
sg[0] = 0;
for (int i = 1; i <= 10000; i++)
{
memset(has, false, sizeof(has));
for (int j = 0; j < k; j++)
{
if (i - s[j] >= 0)
{
has[sg[i - s[j]]] = true;
}
}
for (int j = 0; ; j++)
{
if (has[j] == false)
{
sg[i] = j;
break;
}
}
}
}
int main()
{
while (true)
{
scanf("%d", &k);
if (k == 0)
{
break;
}
for (int i = 0; i < k; i++)
{
scanf("%d", &s[i]);
}
init_sg();
int t, c, x, res;
scanf("%d", &t);
while (t--)
{
res = 0;
scanf("%d", &c);
while (c--)
{
scanf("%d", &x);
res ^= sg[x];
}
if (res == 0)
{
printf("L");
}
else
{
printf("W");
}
}
printf("\n");
}
return 0;
}