线性基板子+高斯消元

3 篇文章 0 订阅

线性基讲解:

视频:

https://www.bilibili.com/video/BV1A7411j7eX?from=search&seid=18060346418081718473

blog:

https://www.luogu.com.cn/blog/BeWild/post-suan-fa-jing-sai-jin-jie-zhi-na-shuo-lun-dan-yuan-zuo-ye-xuan-post

To xor or not to xor

The sequence of non-negative integers A1, A2, ..., AN is given. You are to find some subsequence Ai1, Ai2, ..., Aik (1 <= i1 < i2 < ... < ik <= N) such, that Ai1 XOR Ai2 XOR ... XOR Aik has a maximum value.

Input
The first line of the input file contains the integer number N (1 <= N <= 100). The second line contains the sequence A1, A2, ..., AN (0 <= Ai <= 10^18).

Output
Write to the output file a single integer number -- the maximum possible value of Ai1 XOR Ai2 XOR ... XOR Aik.

Sample test(s)

Input
3
11 9 5

Output
14

Author:Michael R. Mirzayanov
Resource:ACM ICPC 2004-2005, NEERC, Southern Subregional Contest
Date:Saratov, October 7, 2004



 

 sourse:SGU-275

一个经典的求子集异或最大的问题,就是把每个都插入线性基,如果用的方法是在插入的时候消掉其他元素的该位1,那么久在计算结果的时候直接把线性基的所有元素异或。如果插入的方法是在插入时直接插入不做处理,那么每次更新计算结果的时候if(ans^p[i]>ans) ans^=p[i]即可

这里是在插入之后做了处理,就是消掉了其他行的1。(也是高斯消元的思想)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
typedef long long ll;
ll a[maxn],p[maxn],nubcnt;//a存放所有输入,p是线性基 
void guass(int n)//这个函数是把所有的元素都插入了线性基 ,是个离线算法 
{
	memset(p,0,sizeof(p));
	for(int i=1;i<=n;i++)
	{
		for(int j=63;j>=0;j--)
		{
			if((a[i]>>j)&1)
			{
				if(p[j]) a[i]^=p[j];
				else
				{
					p[j]=a[i];
					break;
				}
			}
		}
	}
	for(int i=63;i>=0;i--)//这里是所有元素插入完毕之后再一起处理,消元 
	{
		if(!p[i])
		    continue;
		for(int j=i+1;j<=62;j++)
		{
			if((p[j]>>i)&1) p[j]^=p[i];
		}
	}
}
int main()
{
	int t,n,q;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) 
	   	scanf("%lld",&a[i]);
	guass(n);
	ll ans=0;
	for(int i=0;i<=63;i++)//把所有线性基里的元素异或就是最大值 
	    ans^=p[i];
    printf("%lld\n",ans);
    return 0;
}

2021.8.24

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值