洛谷P3812 【模板】(线性基模板题)同样可解SGU 275
2019-03-14 15:39
53 查看
题目背景
这是一道模板题。
题目描述
给定n个整数(数字可能重复),求在这些数中选取任意个,使得他们的异或和最大。
输入输出格式
输入格式:
第一行一个数n,表示元素个数
接下来一行n个数
输出格式:
仅一行,表示答案。
输入输出样例
输入样例#1: 复制
21 1
输出样例#1: 复制
1
就是一个线性基的模板题,就直接上代码了
[code]#include <algorithm> #include <math.h> #include <string.h> #include <vector> #include <stdio.h> #include <map> using namespace std; #define LL long long const int MAX = 1e5 + 50; int MAX_BASE = 60; int n; LL a[55]; LL b[65]; void cal() { for (int i = 0; i < n; ++i){ for (int j = MAX_BASE; j >= 0; --j){ if ((a[i] >> j) & 1) { if (b[j]) a[i] ^= b[j]; else { b[j] = a[i]; for (int k = j - 1; k >= 0; --k) if (b[k] && ((b[j] >> k) & 1)) b[j] ^= b[k]; for (int k = j + 1; k <= MAX_BASE; ++k) if ((b[k] >> j) & 1) b[k] ^= b[j]; break; } } } } } int main(int argc, char const *argv[]) { scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%lld", &a[i]); } cal(); LL ans = 0; for(int i = 0; i <= MAX_BASE; i++){ ans ^= b[i]; } printf("%lld\n", ans); return 0; }
相关文章推荐
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- 模板【洛谷P3812】 【模板】线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 异或线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 - To xor or not to xor 按位贪心 线性基
- SGU 275 To xor or not to xor (高斯消元)
- [洛谷3812]【模板】线性基
- SGU 275 To xor or not to xor(高斯消元)
- SGU-275 To xor or not to xor 高斯消元