首页 > 其他 > 详细

bzoj 4269 再见Xor 线性基

时间:2018-08-14 19:21:00      阅读:142      评论:0      收藏:0      [点我收藏+]

题面

题目传送门

解法

第一问就是线性基的裸题

第二问也很类似,从低位向高位枚举,如果线性基上这一位有数,那么直接异或后返回

时间复杂度:\(O(n\ log\ a_i)\)

代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == ‘-‘) f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - ‘0‘, c = getchar(); x *= f;
}
struct Linear_Base {
    int a[40];
    void ins(int x) {
        for (int i = 31; ~i; i--)
            if ((x >> i) & 1) {
                if (!a[i]) {a[i] = x; break;}
                x ^= a[i];
            }
    }
} b;
main() {
    int n; read(n);
    for (int i = 1; i <= n; i++) {
        int x; read(x);
        b.ins(x);
    }
    int mx = 0;
    for (int i = 31; ~i; i--)
        if ((mx ^ b.a[i]) > mx) mx ^= b.a[i];
    int mx2 = mx;
    for (int i = 0; i <= 31; i++)
        if (b.a[i]) {mx2 ^= b.a[i]; break;}
    cout << mx << ‘ ‘ << mx2 << "\n";
    return 0;
}

bzoj 4269 再见Xor 线性基

原文:https://www.cnblogs.com/copperoxide/p/9476712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!