hdu 5724 Chess (sg函数 + 状态压缩)

来源:互联网 发布:网络修真小说研究 编辑:程序博客网 时间:2024/05/21 22:31

分析
- 用二进制来压缩当前状态。
- 没有子状态的其sg为0(这在操作中就可自动实现。
- 对于当前状态的子状态进行枚举,二进制数一定是减小到所以符合sg函数转移。
- 求所有sg值的异或。
预处理sg函数的时间复杂度为O(220202)

#include <iostream>#include <cstring>#include <cstdio>using namespace std;struct jibancanyang{    int chess[1 << 21];    bool have[30];    void pre() {        for (int i = 1; i < 1 << 20; i++) {            memset(have, 0, sizeof(have));            for (int j = 0; j < 20; j++) {                if (i & (1 << j)) {                    int k;                    for (k = j - 1; k >= 0; k--) {                        if (!(i & (1 << k))) break;                    }                    if (k >= 0) {                        int temp = i ^ (1 << j) | (1 << k);                        have[chess[temp]] = true;                    }                 }            }            int cnt = 0;            while (have[cnt]) cnt++;            chess[i] = cnt;        }    }    void run() {        pre();        int T, n;         scanf("%d", &T);        while (T--) {            scanf("%d", &n);            int ans = 0;            while (n--) {                int x, s = 0, bit;                scanf("%d", &x);                while (x--) {                    scanf("%d", &bit);                    s ^= 1 << (20 - bit);                }                ans ^= chess[s];             }            puts(ans ? "YES" : "NO");        }    }}ac;int main(){    //freopen("in.txt", "r", stdin);    ac.run();    return 0;}
0 0
原创粉丝点击