http://acm.hdu.edu.cn/showproblem.php?pid=1944
题意:在普通的Nim游戏上加入一些限制。给定一个集合S, 每次只能取S的元素个石子。
题解:SG函数。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100 + 10;
int sg[10000+10], a[maxn], k;
int mex(int n) {
bool vis[maxn] = {false};
for(int i=0; i<k; i++) {
int t = n - a[i];
if(t < 0) continue;
if(sg[t] == -1) sg[t] = mex(t);
vis[sg[t]] = true;
}
for(int i=0; ; i++) if(!vis[i]) return i;
}
int main() {
int m;
while(cin >> k && k) {
string s;
for(int i=0; i<k; i++) cin >> a[i];
cin >> m;
memset(sg, -1, sizeof(sg));
while(m--) {
int j, ans = 0, hi;
cin >> j;
while(j--) {
cin >> hi;
ans ^= mex(hi);
}
if(ans) s += 'W';
else s += 'L';
}
cout << s << endl;
}
return 0;
}