Xor && 线性基练习
#include <cstdio>
#include <cstring>
const int Len=;
const int Maxn=;
int cnt,Ans,b,x,n;
inline int Max(int x,int y) {return x>y?x:y;}
struct Node {int next[];}Tree[Maxn*Len];
void Insert(int x)
{
int Now=; bool k;
for (int i=Len;i>=;i--)
{
k=x&(<<i);
if (Tree[Now].next[k]==-) Tree[Now].next[k]=++cnt;
Now=Tree[Now].next[k];
}
}
int Query(int x)
{
int Now=,v=; bool k;
for (int i=Len;i>=;i--)
{
k=x&(<<i);
if (Tree[Now].next[!k]!=-) k=!k;
v=v|(k<<i);
Now=Tree[Now].next[k];
}
return v;
}
int main()
{
// freopen("c.in","r",stdin);
while (scanf("%d",&n)!=EOF)
{
Ans=cnt=; memset(Tree,-,sizeof(Tree));
for (int i=;i<=n;i++)
{
scanf("%d",&x);
Insert(x);
Ans=Max(Ans,x^Query(x));
}
printf("%d\n",Ans);
}
return ;
}
CSU1216
在n个数取两个Xor最大。用0-1Trie.
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
LL n,Ans,Base[];
struct Node{LL a,b;}A[];
inline bool cmp(Node A,Node B) {return A.b>B.b;} int main()
{
scanf("%lld",&n);
for (LL i=;i<=n;i++) scanf("%lld%lld",&A[i].a,&A[i].b);
sort(A+,A+n+,cmp); Ans=;
for (LL i=;i<=n;i++)
{
for (LL j=;j>=;j--)
if (A[i].a>>j&)
{
if (!Base[j])
{
Base[j]=A[i].a;
break;
}
A[i].a^=Base[j];
}
if (A[i].a) Ans+=A[i].b;
}
printf("%lld\n",Ans);
return ;
}
BZOJ2460
若子集异或为0,则集合一定不为线性基。从大到小排序,贪心即可。
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
const LL Maxn=;
LL a[Maxn],Kase,n,Ans,k,q,Bin[];
inline void Swap(LL &x,LL &y) {LL t=x;x=y;y=t;}
inline LL Get(LL Row,LL k)
{
if (Row<n)
{if (k==) return ; else k--;}
if (k>=Bin[Row]) return -;
LL Ret=;
for (int i=;i<=Row;i++)
if (k&Bin[Row-i]) Ret^=a[i];
return Ret;
}
int main()
{
scanf("%I64d",&Kase);
Bin[]=; for (int i=;i<=;i++) Bin[i]=Bin[i-]<<;
for (LL kase=;kase<=Kase;kase++)
{
printf("Case #%I64d:\n",kase);
scanf("%I64d",&n);
for (LL i=;i<=n;i++) scanf("%I64d",&a[i]);
LL Now=;
for (LL i=Bin[];i;i>>=)
{
LL j=Now+;
while (!(i&a[j])&&j<=n) j++;
if (j==n+) continue;
++Now; Swap(a[Now],a[j]);
for (j=;j<=n;j++)
{
if (j==Now) continue;
if (a[j]&i) a[j]=a[j]^a[Now];
}
}
scanf("%I64d",&q);
for (LL i=;i<=q;i++)
{
scanf("%I64d",&k);
printf("%I64d\n",Get(Now,k));
}
}
return ;
}
HDU3949
求第K大的Xor和,求出线性基,把K转为二进制在Xor就可以了
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
const LL Mod=;
const LL Maxn=;
struct Node{LL b[Maxn],c;}a[Maxn];
LL Base[Maxn],cnt,Ans,n,m;
inline LL Pow(LL x,LL y)
{
LL Ret=;
while (true)
{
if (y&) Ret=(Ret*x)%Mod;
x=(x*x)%Mod; y>>=;
if (y==) break;
}
return Ret;
}
inline bool cmp(Node A,Node B) {return A.c<B.c;}
int main()
{
scanf("%lld%lld",&n,&m);
for (LL i=;i<=n;i++)
for (LL j=;j<=m;j++) scanf("%lld",&a[i].b[j]);
for (LL i=;i<=n;i++) scanf("%lld",&a[i].c);
sort(a+,a+n+,cmp);
for (LL i=;i<=n;i++)
{
bool flag=false;
for (LL j=;j<=m;j++)
if (a[i].b[j])
{
if (!Base[j])
{
Base[j]=i; flag=true;
break;
}
LL t=Mod-(a[i].b[j]*Pow(a[Base[j]].b[j],Mod-))%Mod;
for (LL k=j;k<=m;k++)
a[i].b[k]=(a[i].b[k]+(t*a[Base[j]].b[k])%Mod)%Mod;
}
if (flag) Ans+=a[i].c,cnt++;
}
printf("%lld %lld\n",cnt,Ans);
return ;
}
BZOJ4004
即求出线性基,然后贪心取即可。
#include <cstdio>
#include <cstring>
#define LL long long
inline void Get_Int(LL & x)
{
char ch=getchar(); x=;
while (ch<'' || ch>'') ch=getchar();
while (ch>='' && ch<='') {x=x*+ch-'';ch=getchar();}
}
inline void Swap(LL &x,LL &y) {LL t=x;x=y;y=t;}
inline LL Max(LL x,LL y) {return x>y?x:y;}
inline LL Min(LL x,LL y) {return x>y?y:x;}
//==================================================
const LL Maxn=;
LL head[Maxn],father[Maxn][],Dep[Maxn],n,m,Bin[],x,u,v,cnt,Sum;
bool vis[Maxn];
struct Edge{LL to,next;}edge[Maxn<<];
struct Base
{
LL a[];
inline void Clr() {memset(a,,sizeof(a));}
inline void Insert(LL x)
{
for (LL i=;i>=;i--)
if (Bin[i]&x)
{
if (!a[i]) {a[i]=x; return;}
x^=a[i];
}
}
};
Base f[Maxn][],Ans;
inline void Add(LL u,LL v) {edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;}
void Dfs(LL u)
{
vis[u]=true;
for (LL i=head[u];i!=-;i=edge[i].next)
if (!vis[edge[i].to])
{
father[edge[i].to][]=u;
Dep[edge[i].to]=Dep[u]+;
Dfs(edge[i].to);
}
}
inline void Init()
{
for (LL j=;j<=;j++)
for (LL i=;i<=n;i++)
{
father[i][j]=father[father[i][j-]][j-];
for (LL k=;k>=;k--) if (f[i][j-].a[k])f[i][j].Insert(f[i][j-].a[k]);
for (LL k=;k>=;k--) if (f[father[i][j-]][j-].a[k])f[i][j].Insert(f[father[i][j-]][j-].a[k]);
}
}
void Solve(LL u,LL v)
{
if (Dep[u]<Dep[v]) Swap(u,v);
for (LL i=;i>=;i--)
if (Dep[father[u][i]]>=Dep[v])
{
for (LL k=;k>=;k--) if (f[u][i].a[k]) Ans.Insert(f[u][i].a[k]);
u=father[u][i];
}
if (u==v)
{
for (LL k=;k>=;k--) if (f[u][].a[k]) Ans.Insert(f[u][].a[k]);
return;
} for (LL i=;i>=;i--)
if (father[u][i]!=father[v][i])
{
for (LL k=;k>=;k--) if (f[u][i].a[k]) Ans.Insert(f[u][i].a[k]);
for (LL k=;k>=;k--) if (f[v][i].a[k]) Ans.Insert(f[v][i].a[k]);
u=father[u][i],v=father[v][i];
}
for (LL k=;k>=;k--) if (f[u][].a[k])Ans.Insert(f[u][].a[k]);
for (LL k=;k>=;k--) if (f[v][].a[k])Ans.Insert(f[v][].a[k]);
for (LL k=;k>=;k--) if (f[father[u][]][].a[k])Ans.Insert(f[father[u][]][].a[k]);
} int main()
{
Bin[]=; for (LL i=;i<=;i++) Bin[i]=Bin[i-]<<;
Get_Int(n),Get_Int(m);
for (LL i=;i<=n;i++)
{
Get_Int(x);
f[i][].Insert(x);
}
memset(head,-,sizeof(head));
for (LL i=;i<n;i++)
{
Get_Int(u),Get_Int(v);
Add(u,v),Add(v,u);
}
father[][]=; Dep[]=;
memset(vis,false,sizeof(vis)); Dfs();
Init();
for (LL i=;i<=m;i++)
{
Get_Int(u),Get_Int(v);
Ans.Clr();
Solve(u,v); Sum=;
for (LL j=;j>=;j--) Sum=Max(Sum,Sum^Ans.a[j]);
printf("%lld\n",Sum);
}
return ;
}
BZOJ4568
倍增合并线性基,贪心求最大值即可
Xor && 线性基练习的更多相关文章
- 【BZOJ-2115】Xor 线性基 + DFS
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2142 Solved: 893[Submit][Status] ...
- BZOJ.2115.[WC2011]Xor(线性基)
题目链接 \(Description\) 给定一张无向带边权图(存在自环和重边).求一条1->n的路径,使得路径经过边的权值的Xor和最大.可重复经过点/边,且边权和计算多次. \(Soluti ...
- BZOJ2115:[WC2011] Xor(线性基)
Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...
- HDU3949 XOR (线性基)
HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...
- [hdu3949]XOR(线性基求xor第k小)
题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- HDU3949 XOR(线性基第k小)
Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...
- 2019牛客多校第一场H XOR 线性基模板
H XOR 题意 给出一组数,求所有满足异或和为0的子集的长度和 分析 n为1e5,所以枚举子集肯定是不可行的,这种时候我们通常要转化成求每一个数的贡献,对于一组数异或和为0.我们考虑使用线性基,对这 ...
- BZOJ 2115 [Wc2011] Xor ——线性基
[题目分析] 显然,一个路径走过两边是不需要计算的,所以我么找到一条1-n的路径,然后向该异或值不断异或简单环即可. 但是找出所有简单环是相当复杂的,我们只需要dfs一遍,找出所有的环路即可,因为所有 ...
- BZOJ 2844: albus就是要第一个出场 [高斯消元XOR 线性基]
2844: albus就是要第一个出场 题意:给定一个n个数的集合S和一个数x,求x在S的$2^n$个子集从小到大的异或和序列中最早出现的位置 一开始看错题了...人家要求的是x第一次出现位置不是第x ...
随机推荐
- 20169212《Linux内核原理与分析》第八周作业
理论 task_struct的结构关系 非常庞大的数据结构,400多行代码.包括对进程链表的管理,控制台,文件系统描述,文件描述符,内存管理描述,信号描述等. 创建一个新进程在内核中的执行过程 for ...
- jqueryGannt用法
jqueryGannt主要用于甘特图的展示,直接上步骤 1.下载 jquery.min.js js/jquery.fn.gantt.js 的js ,可以自己去github上搜官方的. 其他的要上传对应 ...
- laravel cookie写入
$cookie = cookie('cookie_name', 'value', 5); $data = ['title'=>'hello world']; ...
- js 判断IE浏览器,包含IE6/7/8/9
//判断IE6-9的版本,num可取值为6/7/8/9//若不给参数num,仅判断浏览器是否为IE,不判断版本 var isIE = function (num) { if(arguments.len ...
- Traveling in Blade & Soul
Traveling in Blade & Soul Walking is too simple. Having trained their physics and spirits for ye ...
- 数据中心第三方服务、金融IT外包服务、社保医疗信息化解决方案,这三类业务是什么关系,区别在哪?
这个话题很大,牵扯很多,试着回答一下,算是胡扯了. 三类业务的关系,都是IT外包,至于外包的内容很杂.DC的外包,多半是基建和建维,一般不牵扯到软件开发,网站建设类的.金融IT外包就复杂多了,信息系统 ...
- BZOJ 1189 二分匹配 || 最大流
1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1155 Solved: 420[Submi ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- Cookie与Session用法
以下是Cookie的完整实例 MyEclipse新建web Project工程,建两个jsp文件,如下 1.login.jsp <%@ page language="java" ...
- win10 内测14352 加入了容器 和docker新功能,想体验的赶快升级
原来只在server2016上有,现在加入到win0内测版了windows 容器提供了两种级别的隔离技术,分别是Windows Server container 和Hyper-V Container ...