洛谷 [P2575] 高手过招

SG函数+状压记忆化搜索

观察题目发现,每一行都是独立的,只要处理出来每一行的SG值,异或起来就好

每一行的SG值可以用状压+记忆化搜索的方法来求,对位运算技术是个很大的考验

注意SG值要预处理出来,不能每次算
(WA的原因是 a数组在递归的过程中没有被清空,而是直接覆盖了,如果动态开数组就没有这样的问题)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAXN = 1100005;
int init() {
    int rv = 0, fh = 1; 
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') fh = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        rv = (rv<<1) + (rv<<3) + c - '0';
        c = getchar();
    }
    return fh * rv;
}
int T, n, SG[MAXN], a[30];
int lowbit(int x) {
    return x & (-x);//与树状数组中相同,取一个二进制数最右边的1
}
int SG_search(int cur) {
    if(SG[cur] != -1) return SG[cur];
    int t = cur + 1 - lowbit(cur + 1), k = lowbit(t), cnt = 0, tmp;
        //t 是 cur 去掉最后的所有1
    while(k) {
        t ^= k;tmp = k;
        for( ; (cur ^ k) < cur ; k >>= 1) ;
        a[cnt++] = SG_search(cur ^ k ^ tmp);
        k = lowbit(t);
    }
    sort(a, a + cnt);
    if(a[0]) return SG[cur] = 0;
    for(int i = 1 ; i < cnt ; i++) {
        if(a[i] - a[i - 1] > 1) return SG[cur] = a[i - 1] + 1;
    }
    return SG[cur] = a[cnt - 1] + 1;
}
int main() {
    freopen("in.txt", "r", stdin);
    T = init();
    memset(SG, -1, sizeof(SG));
    int a = 0;
    for(int i = 0 ; i < 20 ; i++) {
        a |= (1<<i);
        SG[a] = 0;
    }
    for(int i = 1 ; i <= (1<<20) ; i++) {
        if(SG[i] == -1) SG_search(i);
    }
    while(T--) {
        n = init();
        int ans = 0;
        for(int i = 1 ; i <= n ; i++) {
            int t = init();
            int temp = 0;
            for(int j = 1 ; j <= t ; j++) {
                int k = init();
                temp = temp | (1 << (20 - k));
            }
            ans ^= SG[temp];
        }
        if(ans) printf("YES\n");
        else printf("NO\n");
    }
    fclose(stdin);
    return 0;
}

转载于:https://www.cnblogs.com/Mr-WolframsMgcBox/p/8479531.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值