1. #include <cstdio>
  2. #include <cstring>
  3. const int Len=;
  4. const int Maxn=;
  5. int cnt,Ans,b,x,n;
  6. inline int Max(int x,int y) {return x>y?x:y;}
  7. struct Node {int next[];}Tree[Maxn*Len];
  8. void Insert(int x)
  9. {
  10. int Now=; bool k;
  11. for (int i=Len;i>=;i--)
  12. {
  13. k=x&(<<i);
  14. if (Tree[Now].next[k]==-) Tree[Now].next[k]=++cnt;
  15. Now=Tree[Now].next[k];
  16. }
  17. }
  18. int Query(int x)
  19. {
  20. int Now=,v=; bool k;
  21. for (int i=Len;i>=;i--)
  22. {
  23. k=x&(<<i);
  24. if (Tree[Now].next[!k]!=-) k=!k;
  25. v=v|(k<<i);
  26. Now=Tree[Now].next[k];
  27. }
  28. return v;
  29. }
  30. int main()
  31. {
  32. // freopen("c.in","r",stdin);
  33. while (scanf("%d",&n)!=EOF)
  34. {
  35. Ans=cnt=; memset(Tree,-,sizeof(Tree));
  36. for (int i=;i<=n;i++)
  37. {
  38. scanf("%d",&x);
  39. Insert(x);
  40. Ans=Max(Ans,x^Query(x));
  41. }
  42. printf("%d\n",Ans);
  43. }
  44. return ;
  45. }

CSU1216

在n个数取两个Xor最大。用0-1Trie.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #define LL long long
  4. using namespace std;
  5. LL n,Ans,Base[];
  6. struct Node{LL a,b;}A[];
  7. inline bool cmp(Node A,Node B) {return A.b>B.b;}
  8.  
  9. int main()
  10. {
  11. scanf("%lld",&n);
  12. for (LL i=;i<=n;i++) scanf("%lld%lld",&A[i].a,&A[i].b);
  13. sort(A+,A+n+,cmp); Ans=;
  14. for (LL i=;i<=n;i++)
  15. {
  16. for (LL j=;j>=;j--)
  17. if (A[i].a>>j&)
  18. {
  19. if (!Base[j])
  20. {
  21. Base[j]=A[i].a;
  22. break;
  23. }
  24. A[i].a^=Base[j];
  25. }
  26. if (A[i].a) Ans+=A[i].b;
  27. }
  28. printf("%lld\n",Ans);
  29. return ;
  30. }

BZOJ2460

若子集异或为0,则集合一定不为线性基。从大到小排序,贪心即可。

  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4. #define LL long long
  5. const LL Maxn=;
  6. LL a[Maxn],Kase,n,Ans,k,q,Bin[];
  7. inline void Swap(LL &x,LL &y) {LL t=x;x=y;y=t;}
  8. inline LL Get(LL Row,LL k)
  9. {
  10. if (Row<n)
  11. {if (k==) return ; else k--;}
  12. if (k>=Bin[Row]) return -;
  13. LL Ret=;
  14. for (int i=;i<=Row;i++)
  15. if (k&Bin[Row-i]) Ret^=a[i];
  16. return Ret;
  17. }
  18. int main()
  19. {
  20. scanf("%I64d",&Kase);
  21. Bin[]=; for (int i=;i<=;i++) Bin[i]=Bin[i-]<<;
  22. for (LL kase=;kase<=Kase;kase++)
  23. {
  24. printf("Case #%I64d:\n",kase);
  25. scanf("%I64d",&n);
  26. for (LL i=;i<=n;i++) scanf("%I64d",&a[i]);
  27. LL Now=;
  28. for (LL i=Bin[];i;i>>=)
  29. {
  30. LL j=Now+;
  31. while (!(i&a[j])&&j<=n) j++;
  32. if (j==n+) continue;
  33. ++Now; Swap(a[Now],a[j]);
  34. for (j=;j<=n;j++)
  35. {
  36. if (j==Now) continue;
  37. if (a[j]&i) a[j]=a[j]^a[Now];
  38. }
  39. }
  40. scanf("%I64d",&q);
  41. for (LL i=;i<=q;i++)
  42. {
  43. scanf("%I64d",&k);
  44. printf("%I64d\n",Get(Now,k));
  45. }
  46. }
  47. return ;
  48. }

HDU3949

求第K大的Xor和,求出线性基,把K转为二进制在Xor就可以了

  1. #include <cstdio>
  2. #include <algorithm>
  3. #define LL long long
  4. using namespace std;
  5. const LL Mod=;
  6. const LL Maxn=;
  7. struct Node{LL b[Maxn],c;}a[Maxn];
  8. LL Base[Maxn],cnt,Ans,n,m;
  9. inline LL Pow(LL x,LL y)
  10. {
  11. LL Ret=;
  12. while (true)
  13. {
  14. if (y&) Ret=(Ret*x)%Mod;
  15. x=(x*x)%Mod; y>>=;
  16. if (y==) break;
  17. }
  18. return Ret;
  19. }
  20. inline bool cmp(Node A,Node B) {return A.c<B.c;}
  21. int main()
  22. {
  23. scanf("%lld%lld",&n,&m);
  24. for (LL i=;i<=n;i++)
  25. for (LL j=;j<=m;j++) scanf("%lld",&a[i].b[j]);
  26. for (LL i=;i<=n;i++) scanf("%lld",&a[i].c);
  27. sort(a+,a+n+,cmp);
  28. for (LL i=;i<=n;i++)
  29. {
  30. bool flag=false;
  31. for (LL j=;j<=m;j++)
  32. if (a[i].b[j])
  33. {
  34. if (!Base[j])
  35. {
  36. Base[j]=i; flag=true;
  37. break;
  38. }
  39. LL t=Mod-(a[i].b[j]*Pow(a[Base[j]].b[j],Mod-))%Mod;
  40. for (LL k=j;k<=m;k++)
  41. a[i].b[k]=(a[i].b[k]+(t*a[Base[j]].b[k])%Mod)%Mod;
  42. }
  43. if (flag) Ans+=a[i].c,cnt++;
  44. }
  45. printf("%lld %lld\n",cnt,Ans);
  46. return ;
  47. }

BZOJ4004

即求出线性基,然后贪心取即可。

  1. #include <cstdio>
  2. #include <cstring>
  3. #define LL long long
  4. inline void Get_Int(LL & x)
  5. {
  6. char ch=getchar(); x=;
  7. while (ch<'' || ch>'') ch=getchar();
  8. while (ch>='' && ch<='') {x=x*+ch-'';ch=getchar();}
  9. }
  10. inline void Swap(LL &x,LL &y) {LL t=x;x=y;y=t;}
  11. inline LL Max(LL x,LL y) {return x>y?x:y;}
  12. inline LL Min(LL x,LL y) {return x>y?y:x;}
  13. //==================================================
  14. const LL Maxn=;
  15. LL head[Maxn],father[Maxn][],Dep[Maxn],n,m,Bin[],x,u,v,cnt,Sum;
  16. bool vis[Maxn];
  17. struct Edge{LL to,next;}edge[Maxn<<];
  18. struct Base
  19. {
  20. LL a[];
  21. inline void Clr() {memset(a,,sizeof(a));}
  22. inline void Insert(LL x)
  23. {
  24. for (LL i=;i>=;i--)
  25. if (Bin[i]&x)
  26. {
  27. if (!a[i]) {a[i]=x; return;}
  28. x^=a[i];
  29. }
  30. }
  31. };
  32. Base f[Maxn][],Ans;
  33. inline void Add(LL u,LL v) {edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;}
  34. void Dfs(LL u)
  35. {
  36. vis[u]=true;
  37. for (LL i=head[u];i!=-;i=edge[i].next)
  38. if (!vis[edge[i].to])
  39. {
  40. father[edge[i].to][]=u;
  41. Dep[edge[i].to]=Dep[u]+;
  42. Dfs(edge[i].to);
  43. }
  44. }
  45. inline void Init()
  46. {
  47. for (LL j=;j<=;j++)
  48. for (LL i=;i<=n;i++)
  49. {
  50. father[i][j]=father[father[i][j-]][j-];
  51. for (LL k=;k>=;k--) if (f[i][j-].a[k])f[i][j].Insert(f[i][j-].a[k]);
  52. for (LL k=;k>=;k--) if (f[father[i][j-]][j-].a[k])f[i][j].Insert(f[father[i][j-]][j-].a[k]);
  53. }
  54. }
  55. void Solve(LL u,LL v)
  56. {
  57. if (Dep[u]<Dep[v]) Swap(u,v);
  58. for (LL i=;i>=;i--)
  59. if (Dep[father[u][i]]>=Dep[v])
  60. {
  61. for (LL k=;k>=;k--) if (f[u][i].a[k]) Ans.Insert(f[u][i].a[k]);
  62. u=father[u][i];
  63. }
  64. if (u==v)
  65. {
  66. for (LL k=;k>=;k--) if (f[u][].a[k]) Ans.Insert(f[u][].a[k]);
  67. return;
  68. }
  69.  
  70. for (LL i=;i>=;i--)
  71. if (father[u][i]!=father[v][i])
  72. {
  73. for (LL k=;k>=;k--) if (f[u][i].a[k]) Ans.Insert(f[u][i].a[k]);
  74. for (LL k=;k>=;k--) if (f[v][i].a[k]) Ans.Insert(f[v][i].a[k]);
  75. u=father[u][i],v=father[v][i];
  76. }
  77. for (LL k=;k>=;k--) if (f[u][].a[k])Ans.Insert(f[u][].a[k]);
  78. for (LL k=;k>=;k--) if (f[v][].a[k])Ans.Insert(f[v][].a[k]);
  79. for (LL k=;k>=;k--) if (f[father[u][]][].a[k])Ans.Insert(f[father[u][]][].a[k]);
  80. }
  81.  
  82. int main()
  83. {
  84. Bin[]=; for (LL i=;i<=;i++) Bin[i]=Bin[i-]<<;
  85. Get_Int(n),Get_Int(m);
  86. for (LL i=;i<=n;i++)
  87. {
  88. Get_Int(x);
  89. f[i][].Insert(x);
  90. }
  91. memset(head,-,sizeof(head));
  92. for (LL i=;i<n;i++)
  93. {
  94. Get_Int(u),Get_Int(v);
  95. Add(u,v),Add(v,u);
  96. }
  97. father[][]=; Dep[]=;
  98. memset(vis,false,sizeof(vis)); Dfs();
  99. Init();
  100. for (LL i=;i<=m;i++)
  101. {
  102. Get_Int(u),Get_Int(v);
  103. Ans.Clr();
  104. Solve(u,v); Sum=;
  105. for (LL j=;j>=;j--) Sum=Max(Sum,Sum^Ans.a[j]);
  106. printf("%lld\n",Sum);
  107. }
  108. return ;
  109. }

BZOJ4568

倍增合并线性基,贪心求最大值即可

Xor && 线性基练习的更多相关文章

  1. 【BZOJ-2115】Xor 线性基 + DFS

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2142  Solved: 893[Submit][Status] ...

  2. BZOJ.2115.[WC2011]Xor(线性基)

    题目链接 \(Description\) 给定一张无向带边权图(存在自环和重边).求一条1->n的路径,使得路径经过边的权值的Xor和最大.可重复经过点/边,且边权和计算多次. \(Soluti ...

  3. BZOJ2115:[WC2011] Xor(线性基)

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

  4. HDU3949 XOR (线性基)

    HDU3949 XOR Problem Description XOR is a kind of bit operator, we define that as follow: for two bin ...

  5. [hdu3949]XOR(线性基求xor第k小)

    题目大意:求xor所有值的第k小,线性基模板题. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  6. HDU3949 XOR(线性基第k小)

    Problem Description XOR is a kind of bit operator, we define that as follow: for two binary base num ...

  7. 2019牛客多校第一场H XOR 线性基模板

    H XOR 题意 给出一组数,求所有满足异或和为0的子集的长度和 分析 n为1e5,所以枚举子集肯定是不可行的,这种时候我们通常要转化成求每一个数的贡献,对于一组数异或和为0.我们考虑使用线性基,对这 ...

  8. BZOJ 2115 [Wc2011] Xor ——线性基

    [题目分析] 显然,一个路径走过两边是不需要计算的,所以我么找到一条1-n的路径,然后向该异或值不断异或简单环即可. 但是找出所有简单环是相当复杂的,我们只需要dfs一遍,找出所有的环路即可,因为所有 ...

  9. BZOJ 2844: albus就是要第一个出场 [高斯消元XOR 线性基]

    2844: albus就是要第一个出场 题意:给定一个n个数的集合S和一个数x,求x在S的$2^n$个子集从小到大的异或和序列中最早出现的位置 一开始看错题了...人家要求的是x第一次出现位置不是第x ...

随机推荐

  1. oracle当前的连接数

    怎样查看oracle当前的连接数呢?只需要用下面的SQL语句查询一下就可以了. select * from v$session where username is not null select us ...

  2. .net乱码问题

    最近在给一个客户做framwwork版本升级,从1.0版本升级到4.0版本,发现最大的问题就是乱码. 在1.0版本下,gb2312编码能够运行得很好,可是升级到4.0后就都是乱码. 随后将webcon ...

  3. iOS时间问题

    在iOS开发中,经常会遇到各种各样的时间问题,8小时时差,时间戳,求时间间隔,农历等等.解决办法网上比比皆是,但大多零零散散,很多资料并没有说明其中问题.这里集中总结一下,以便于以后查阅和供大家参考. ...

  4. Javascript学习笔记3 Javascript与BOM简介

    什么是BOM BOM是browser object model的缩写,简称浏览器对象模型 BOM提供了独立于内容而与浏览器窗口进行交互的对象 由于BOM主要用于管理窗口与窗口之间的通讯,因此其核心对象 ...

  5. ArcGIS Add-in插件开发从0到1及实际案例分享

    同学做毕设,要求我帮着写个ArcGIS插件,实现功能为:遍历所有图斑,提取相邻图斑的公共边长及其他属性(包括相邻图斑的ID),链接到属性表中.搞定后在这里做个记录.本文分两大部分: ArcGIS插件开 ...

  6. Mysql主数据库+备份数据库部署教程

    转:http://www.111cn.net/database/mysql/76450.htm 本文我们来讲讲Mysql主备如何部署,这里说的主是指Mysql主数据库,备是从数据库,备可以是多个,也可 ...

  7. WEB安全性测试

    SQL注入   所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意)的SQL命令 ...

  8. cron表达式使用详解

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  9. Linux系统程序的运行级别

    Linux系统有7个运行级别: 运行级别 描述 0 系统停机状态,系统默认运行级别不能设为0,否则不能正常启动 1 但用户工作状态,root权限,用于系统维护,禁止远程登录 2 多用户状态(没有NFS ...

  10. 实验一 DOS命令解释程序的编写

    一.目的和要求 1. 实验目的 (1)认识DOS: (2)掌握命令解释程序的原理: (3)掌握简单的DOS调用方法: (4)掌握C语言编程初步. 2.实验要求 编写类似于DOS,UNIX的命令行解释程 ...