hdu-1944&& 1534 S-Nim

来源:互联网 发布:mac撤销快捷键 编辑:程序博客网 时间:2024/04/29 01:42

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;}


 

0 0
原创粉丝点击