bzoj 2844: albus就是要第一个出场 高斯消元

LINK

题意:看题目不如看样例解释。给出有n个数的集合,对这些子集中的数求异或,升序统计所有子集得到的数(重复会被计入),询问一个数x,问这个数出现的第一个位置

思路:在这里要求一个所有可能出现的异或值,对于这个要求有个思想和概念很适用这类题——线性基。线代里面学过线性无关组,可用高斯消元解得,在本题中的线性基类似,是能够构造所有出现异或值得线性无关组。总的来说本质思维就是高斯消元。

/** @Date    : 2017-07-03 10:40:20
  * @FileName: bzoj 2844 高斯消元 线性基.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;
const LL mod = 10086;

int n, q;
int bit[64];
int a[N], cnt;
void gauss()
{
	cnt = 1;
	for(int k = 30; k >= 0; k--)
	{
		for(int i = cnt; i <= n; i++)
		{
			if(a[i] & (1 << k))
			{
				for(int j = 1; j <= n; j++)
				{
					if(j != i && (a[j] & (1 << k)))
						a[j] ^= a[i];
				}
				swap(a[i], a[cnt]);//因为存的是从高到低 放到前面
				cnt++;
				break;
			}
		}
	}
	/*for(int i = 1; i <= n; i++)
		printf("%d", a[i]);
	cout << endl;
	cout << "~" << cnt << endl;*/
}

int main()
{
	while(cin >> n)
	{
		for(int i = 1; i <= n; i++)//下标索引写错7发WA 
			scanf("%d", a + i);
		scanf("%d", &q);
		gauss();
		LL ans = 0;
		int t = 0;
		for(int i = 1; i < cnt; i++)
		{
			if((t^a[i]) <= q)//通过判断异或和是否大于Q 二进制计算小于Q数的个数
				t ^= a[i], ans = (ans + (LL)(1LL << (cnt - i - 1))) % mod;
		}
		for(int i = 1; i <= n - cnt + 1; i++)
			ans = (ans * 2LL) % mod;
		ans++;
		printf("%lld
", ans % mod);
	}
    return 0;
}
原文地址:https://www.cnblogs.com/Yumesenya/p/7115705.html