线性基
P3812 【模板】线性基
#include <bits/stdc++.h>using namespace std;typedef long long ll;struct linearbasis {ll base[64], flag, cnt;void add(ll x) {for(int i = 62; ~i; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return ;}x ^= base[i];}}flag = 1;}ll query_max() {ll ans = 0;for(int i = 62; ~i; i--) {if((ans ^ base[i]) > ans) {ans ^= base[i];}}return ans;}ll query_min() {for(int i = 0; i <= 62; i++) {if(base[i]) {return base[i];}}}void rebuild() {cnt = 0;for(int i = 62; i >= 0; i--) {for(int j = i - 1; j >= 0; j--) {if(base[i] >> j & 1) {base[i] ^= base[j];}}}for(int i = 0; i <= 62; i++) {if(base[i]) {ll temp = base[i];base[i] = 0;base[cnt++] = temp;}}}ll query_k(ll k) {k -= flag;if(k == 0) return 0;if(k >= 1ll << cnt) return -1;ll ans = 0;for(int i = 62; ~i; i--) {if(k >> i & 1) {ans ^= base[i];}}return ans;}void init() {memset(base, 0, sizeof base), flag = cnt = 0;}
}a;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n;cin >> n;for (int i = 1; i <= n; i++) {ll x; cin >> x;a.add(x);}printf("%lld\n", a.query_max());return 0;
}
G. Shortest Path Problem?
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 2e5 + 10;int head[N], to[N], nex[N], value[N], cnt;int dis[N], vis[N], n, m;struct linearbasis {ll base[64], flag, cnt;void add(ll x) {for(int i = 62; ~i; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return ;}x ^= base[i];}}flag = 1;}ll query_max() {ll ans = 0;for(int i = 62; ~i; i--) {if((ans ^ base[i]) > ans) {ans ^= base[i];}}return ans;}ll query_min() {for(int i = 0; i <= 62; i++) {if(base[i]) {return base[i];}}}void rebuild() {cnt = 0;for(int i = 62; i >= 0; i--) {for(int j = i - 1; j >= 0; j--) {if(base[i] >> j & 1) {base[i] ^= base[j];}}}for(int i = 0; i <= 62; i++) {if(base[i]) {ll temp = base[i];base[i] = 0;base[cnt++] = temp;}}}ll query_k(ll k) {k -= flag;if(k == 0) return 0;if(k >= 1ll << cnt) return -1;ll ans = 0;for(int i = 62; ~i; i--) {if(k >> i & 1) {ans ^= base[i];}}return ans;}ll solve() {ll ans = dis[n];for (int i = 62; i >= 0; i--) {if ((ans ^ base[i]) < ans) {ans ^= base[i];}}return ans;}void init() {memset(base, 0, sizeof base), flag = cnt = 0;}
}a;void add(int x, int y, int w) {to[cnt] = y;nex[cnt] = head[x];value[cnt] = w;head[x] = cnt++;
}void dfs(int rt, int w) {vis[rt] = 1, dis[rt] = w;for (int i = head[rt]; i; i = nex[i]) {if (vis[to[i]]) {a.add(w ^ dis[to[i]] ^ value[i]);}else {dfs(to[i], w ^ value[i]);}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= m; i++) {int x, y, w;scanf("%d %d %d", &x, &y, &w);add(x, y, w);add(y, x, w);}dfs(1, 0);printf("%lld\n", a.solve());return 0;
}
[WC2011]最大XOR和路径
#include <bits/stdc++.h>using namespace std;typedef long long ll;struct linearbasis {ll base[64], flag, cnt;void add(ll x) {for(int i = 62; ~i; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return ;}x ^= base[i];}}flag = 1;}ll query_max(ll ans = 0) {for(int i = 62; ~i; i--) {if((ans ^ base[i]) > ans) {ans ^= base[i];}}return ans;}ll query_min() {for(int i = 0; i <= 62; i++) {if(base[i]) {return base[i];}}}void rebuild() {cnt = 0;for(int i = 62; i >= 0; i--) {for(int j = i - 1; j >= 0; j--) {if(base[i] >> j & 1) {base[i] ^= base[j];}}}for(int i = 0; i <= 62; i++) {if(base[i]) {ll temp = base[i];base[i] = 0;base[cnt++] = temp;}}}ll query_k(ll k) {k -= flag;if(k == 0) return 0;if(k >= 1ll << cnt) return -1;ll ans = 0;for(int i = 62; ~i; i--) {if(k >> i & 1) {ans ^= base[i];}}return ans;}void init() {memset(base, 0, sizeof base), flag = cnt = 0;}
}a;const int N = 1e5 + 10;int head[N], to[N], nex[N], cnt = 1, n, m;ll value[N], dis[N];bool vis[N];void add(int x, int y, ll w) {to[cnt] = y;nex[cnt] = head[x];value[cnt] = w;head[x] = cnt++;
}void dfs(int rt, int fa, ll w) {vis[rt] = 1, dis[rt] = w;for (int i = head[rt]; i; i = nex[i]) {if (to[i] == fa) {continue;}if (vis[to[i]]) {a.add(w ^ dis[to[i]] ^ value[i]);}else {dfs(to[i], rt, w ^ value[i]);}}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= m; i++) {int x, y;ll w;scanf("%d %d %lld", &x, &y, &w);add(x, y, w);}dfs(1, 0, 0);printf("%lld\n", a.query_max(dis[n]));return 0;
}
P3292 [SCOI2016]幸运数字
用线段树来维护区间线性基并,最后用树剖来进行查询操作。
#include <bits/stdc++.h>
#define mid (l + r >> 1)
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define ls rt << 1
#define rs rt << 1 | 1using namespace std;typedef long long ll;const int N = 2e4 + 10;int head[N], to[N << 1], nex[N << 1], cnt = 1;int son[N], fa[N], sz[N], dep[N], top[N], id[N], rk[N], tot;int n, m;ll value[N], tree[N << 2][64], ans[64];void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}void dfs1(int rt, int f) {dep[rt] = dep[f] + 1;sz[rt] = 1, fa[rt] = f;for(int i = head[rt]; i; i = nex[i]) {if(to[i] == f) continue;dfs1(to[i], rt);if(!son[rt] || sz[son[rt]] < sz[to[i]]) son[rt] = to[i];sz[rt] += sz[to[i]];}
}void dfs2(int rt, int tp) {top[rt] = tp; tot++;rk[tot] = rt, id[rt] = tot;if(!son[rt]) return ;dfs2(son[rt], tp);for(int i = head[rt]; i; i = nex[i]) {if(to[i] == fa[rt] || to[i] == son[rt]) continue;dfs2(to[i], to[i]);}
}void insert(ll * base, ll x) {for(int i = 60; i >= 0; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return ;}x ^= base[i];}}
}void merge(ll * a, ll * b) {for(int i = 60; i >= 0; i--) {if(b[i]) {insert(a, b[i]);}}
}void push_up(int rt) {merge(tree[rt], tree[ls]);merge(tree[rt], tree[rs]);
}void build(int rt, int l, int r) {if(l == r) {insert(tree[rt], value[rk[l]]);return ;}build(lson);build(rson);push_up(rt);
}void query(int rt, int l, int r, int L, int R) {if(l >= L && r <= R) {merge(ans, tree[rt]);return ;}if(L <= mid) query(lson, L, R);if(R > mid) query(rson, L, R);
}ll query_max() {ll res = 0;for(int i = 60; i >= 0; i--) {if((res ^ ans[i]) > res) {res ^= ans[i];}ans[i] = 0;}return res;
}ll solve(int x, int y) {while(top[x] != top[y]) {if(dep[top[x]] < dep[top[y]]) swap(x, y);query(1, 1, n, id[top[x]], id[x]);x = fa[top[x]];}if(dep[x] > dep[y]) swap(x, y);query(1, 1, n, id[x], id[y]);return query_max();
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for(int i = 1; i <= n; i++) {scanf("%lld", &value[i]);}for(int i = 1; i < n; i++) {int x, y;scanf("%d %d", &x, &y);add(x, y);add(y, x);}dfs1(1, 0);dfs2(1, 1);build(1, 1, n);for(int i = 1; i <= m; i++) {int x, y;scanf("%d %d", &x, &y);printf("%lld\n", solve(x, y));}return 0;
}
[CQOI2013] 新Nim游戏
#include <bits/stdc++.h>using namespace std;typedef long long ll;struct linearbasis {ll base[64], flag, cnt;bool add(ll x) {for(int i = 62; ~i; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return false;}x ^= base[i];}}flag = 1;return true;}ll query_max(ll ans = 0) {for(int i = 62; ~i; i--) {if((ans ^ base[i]) > ans) {ans ^= base[i];}}return ans;}ll query_min() {for(int i = 0; i <= 62; i++) {if(base[i]) {return base[i];}}}void rebuild() {cnt = 0;for(int i = 62; i >= 0; i--) {for(int j = i - 1; j >= 0; j--) {if(base[i] >> j & 1) {base[i] ^= base[j];}}}for(int i = 0; i <= 62; i++) {if(base[i]) {ll temp = base[i];base[i] = 0;base[cnt++] = temp;}}}ll query_k(ll k) {k -= flag;if(k == 0) return 0;if(k >= 1ll << cnt) return -1;ll ans = 0;for(int i = 62; ~i; i--) {if(k >> i & 1) {ans ^= base[i];}}return ans;}void init() {memset(base, 0, sizeof base), flag = cnt = 0;}
}a;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n;scanf("%d", &n);vector<int> ans(n);for (int i = 0; i < n; i++) {scanf("%d", &ans[i]);}sort(ans.begin(), ans.end(), greater<int> ());ll res = 0;for (int i = 0; i < n; i++) {if (a.add(ans[i])) {res += ans[i];}}printf("%lld\n", res);return 0;
}
[BJWC2011]元素
#include <bits/stdc++.h>using namespace std;typedef long long ll;struct linearbasis {ll base[64], flag, cnt;bool add(ll x) {for(int i = 62; ~i; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return false;}x ^= base[i];}}flag = 1;return true;}ll query_max(ll ans = 0) {for(int i = 62; ~i; i--) {if((ans ^ base[i]) > ans) {ans ^= base[i];}}return ans;}ll query_min() {for(int i = 0; i <= 62; i++) {if(base[i]) {return base[i];}}}void rebuild() {cnt = 0;for(int i = 62; i >= 0; i--) {for(int j = i - 1; j >= 0; j--) {if(base[i] >> j & 1) {base[i] ^= base[j];}}}for(int i = 0; i <= 62; i++) {if(base[i]) {ll temp = base[i];base[i] = 0;base[cnt++] = temp;}}}ll query_k(ll k) {k -= flag;if(k == 0) return 0;if(k >= 1ll << cnt) return -1;ll ans = 0;for(int i = 62; ~i; i--) {if(k >> i & 1) {ans ^= base[i];}}return ans;}void init() {memset(base, 0, sizeof base), flag = cnt = 0;}
}a;const int N = 1e3 + 10;struct Res {ll id, value;void read() {scanf("%lld %lld", &id, &value);}bool operator < (const Res &t) const {return value > t.value;}}b[N];int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {b[i].read();}sort (b + 1, b + 1 + n);ll ans = 0;for (int i = 1; i <= n; i++) {if (a.add(b[i].id)) {continue;}ans += b[i].value;}printf("%lld\n", ans);return 0;
}
Square Subsets
#include <bits/stdc++.h>using namespace std;typedef long long ll;struct linearbasis {ll base[64], flag, cnt;bool add(ll x) {for(int i = 62; ~i; i--) {if(x >> i & 1) {if(!base[i]) {base[i] = x;return false;}x ^= base[i];}}flag = 1;return true;}ll query_max(ll ans = 0) {for(int i = 62; ~i; i--) {if((ans ^ base[i]) > ans) {ans ^= base[i];}}return ans;}ll query_min() {for(int i = 0; i <= 62; i++) {if(base[i]) {return base[i];}}}void rebuild() {cnt = 0;for(int i = 62; i >= 0; i--) {for(int j = i - 1; j >= 0; j--) {if(base[i] >> j & 1) {base[i] ^= base[j];}}}for(int i = 0; i <= 62; i++) {if(base[i]) {ll temp = base[i];base[i] = 0;base[cnt++] = temp;}}}ll query_k(ll k) {k -= flag;if(k == 0) return 0;if(k >= 1ll << cnt) return -1;ll ans = 0;for(int i = 62; ~i; i--) {if(k >> i & 1) {ans ^= base[i];}}return ans;}void init() {memset(base, 0, sizeof base), flag = cnt = 0;}
}a;int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67};const int mod = 1e9 + 7;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int n, ans = 0;cin >> n;for (int i = 1; i <= n; i++) {int x, value = 0;cin >> x;for (int j = 0; j < 19; j++) {int cnt = 0;while (x % prime[j] == 0) {cnt++;x /= prime[j];}if (cnt & 1) {value |= 1 << j;}}if (a.add(value)) {ans++;}}ll res = 1;for (int i = 1; i <= ans; i++) {res = res * 2 % mod;}printf("%lld\n", res - 1);return 0;
}