链接:https://ac.nowcoder.com/acm/contest/180/D
来源:牛客网

xor序列
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
小a有n个数,他提出了一个很有意思的问题:他想知道对于任意的x, y,能否将x与这n个数中的任意多个数异或任意多次后变为y

输入描述:
第一行为一个整数n,表示元素个数
第二行一行包含n个整数,分别代表序列中的元素
第三行为一个整数Q,表示询问次数
接下来Q行,每行两个数x,y,含义如题所示
输出描述:
输出Q行,若x可以变换为y,输出“YES”,否则输出“NO”
示例1
输入
复制
5
1 2 3 4 5
3
6 7
2 1
3 8
输出
复制
YES
YES
NO
说明
对于(6,7)来说,6可以先和3异或,再和2异或
对于(2,1)来说,2可以和3异或
对于(3,8)来说,3不论如何都不能变换为8
备注:
对于100%的数据,n,Q<=105
保证所有运算均在int范围内

题意:

思路:

异或的性质:

y^y=0 则 x^y^y=x

令 x^z=y 两边异或x , 则 x^z^x=y^x -> z= y^x

即在数组中 找出一些数异或起来等于z即可。

这恰好是线性基的基础功能。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/struct LB {// 注意最高是60还是62ll d[61], p[61];int cnt, mx;LB() {memset(d, 0, sizeof(d));memset(p, 0, sizeof(p));cnt = 0, mx = 61;}void init() {memset(d, 0, sizeof(d));memset(p, 0, sizeof(p));}bool add(ll val) {/*插入时判断之前是否有数会与val异或得0,判第k小时如果有为0的情况,k要减一*/for (int i = mx - 1; i >= 0; i--) {if (val & (1LL << i)) {if (!d[i]) {d[i] = val; break;}val ^= d[i];}}return val > 0;}bool query(ll val) { // 查询val这个数是否存在for (int i = mx - 1; i >= 0; i--) {if (val & (1LL << i)) {if (!d[i]) return 0;val ^= d[i];}}return 1;}ll query_max(ll val) {ll ret = val;for (int i = mx - 1; i >= 0; i--) if ((ret ^ d[i]) > ret) ret ^= d[i];return ret;}ll query_min() {for (int i = 0; i < mx; i++) if (d[i]) return d[i];return 0;}void rebuild() {//消元,保存到p数组cnt = 0;for (int i = 0; i < mx; i++) {for (int j = 0; j < i; j ++ )if (d[i] & (1LL << j)) d[i] ^= d[j];}for (int i = 0; i < mx; i++) if (d[i]) p[cnt++] = d[i];}ll query_kth(ll k) { //使用前需要rebuildll ret = 0;if (k >= (1LL << cnt)) return -1;for (int i = cnt - 1; i >= 0; i--) if (k & (1LL << i)) ret ^= p[i];return ret;}ll find(ll x) { //找x是第几大的数,需保证x一定在ll ret = 0, c = 0;for (int i = 0; i < mx; i++) {if (d[i]) {if (x >> i & 1) ret += (1LL << c);c++;}}return ret;}LB operator+(const LB & _A)const {  //合并LB ret = *this;for (int i = mx - 1; i >= 0; i--) if (_A.d[i]) ret.add(_A.d[i]);return ret;}
};
LB base=LB();int main()
{//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);int q;int n,x;cin>>n;repd(i,1,n){cin>>x;base.add(x);}cin>>q;int y;while(q--){cin>>x>>y;x^=y;if(base.query(x)){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}return 0;
}inline void getInt(int* p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}}else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}}
}

转载于:https://www.cnblogs.com/qieqiemin/p/11249973.html

牛客练习赛26 D xor序列 (线性基)相关推荐

  1. 牛客 - 17968 - xor序列 - 线性基

    https://ac.nowcoder.com/acm/problem/17968 下面是错误的做法,因为题目要求必须使用x,而y在check的时候不一定用到等价于x的线性基来构成. 正确的做法是直接 ...

  2. 牛客练习赛26 E-树上路径 (树链剖分+线段树)

    链接:https://ac.nowcoder.com/acm/contest/180/E 来源:牛客网 树上路径 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语 ...

  3. 牛客练习赛44 A 小y的序列 (模拟,细节)

    链接:https://ac.nowcoder.com/acm/contest/634/A 来源:牛客网 小y的序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语 ...

  4. 解题报告(一)C、(牛客练习赛41 F)简单数学题(数论 + FWT)(3.5)

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  5. 牛客练习赛81 E. 小 Q 与函数求和 1( “简单莫比乌斯反演” ,欧拉函数性质)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 牛客练习赛81 E. 小 Q 与函数求和 1( "简单莫比乌斯反演" ) Prob ...

  6. 牛客练习赛52 | C | [烹饪] (DP,裴蜀定理,gcd)

    牛客练习赛52 C 烹饪 链接:https://ac.nowcoder.com/acm/contest/1084/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...

  7. 牛客练习赛68 A.牛牛的mex

    牛客练习赛68 A.牛牛的mex 题目链接 题目描述 牛牛现在有一个长度为 nnn 的序列 a1,a2,-,ana_1,a_2,\ldots,a_na1​,a2​,-,an​.现在牛牛有 qqq 次询 ...

  8. 2021牛客练习赛90

    2021牛客练习赛90 B.寒冬信使 C.盾与战锤 B.寒冬信使 题目链接:https://ac.nowcoder.com/acm/contest/11180/B code: #include< ...

  9. 牛客练习赛#105(A-D)

    牛客练习赛#105 文章目录 牛客练习赛#105 A.切蛋糕的贝贝 B.抱歉,这没有集美 C.打牌的贝贝 D.点分治分点 A.切蛋糕的贝贝 题意 有一个正n边形,想通过下列的切法切成面积比为1:1:4 ...

最新文章

  1. 什么检索是借助计算机技术进行自动标引的,自动文献检索系统
  2. Sql Server实用操作-SQL语句导入导出大全
  3. 斯坦福大学深度学习与自然语言处理第一讲:引言
  4. 原子性和一致性的区别是什么?
  5. 嵌入式常见笔试题总结
  6. 链表简单实现(增删查改)
  7. NBear.Mapping使用教程(5):实体对象与NameValueCollection,Dicitonary以及NBear.Mapping性能
  8. 信息学奥赛一本通 1163:阿克曼(Ackmann)函数
  9. python求单链表的长度_709. 设计链表(Python)
  10. 【云快讯】《微软Sharepoint 2016 Beta版发布,强化混合云搜索功能》
  11. 前端工作、学习中常用工具推荐
  12. IP address 和子网划分
  13. 24c存储器读写软件_必知必会-存储器层次结构
  14. 划分数算法概述及习题
  15. 求1到n的所有质数(素数)
  16. 愚人节、物联网、飞鸽与IP
  17. python求直角三角形个数的公式_Python3 欧拉计划 问题71-75
  18. 球半足球分析,瑞典超:哥德堡 VS 代格福什 7月5日
  19. 怎么去掉WIN7窗口文本框中淡绿色的底色
  20. “中国十大名校”之争,缘何让百度气急败坏封贴?

热门文章

  1. Java Spring singleton bean的创建源代码
  2. 有道云笔记不需要通过开通会员的方式来去除广告显示
  3. 最简单的教程:在Ubuntu操作系统里安装Docker
  4. ABAP正则表达式 vs SPLIT INTO 1
  5. 龙卷风优化软件测试面试题,暑X好物大推荐,最强真无线降噪耳机带你开启夏日解压模式...
  6. 一个大型虚拟项目包含位于不同地点的许多干系人_项目管理与人生
  7. Redis启动报错:[20000] 07 Apr 17:55:34.562 # Creating Server TCP listening socket 127.0.0.1:6379: bind: N
  8. python编程入门第九讲,第九讲作业---函数
  9. notnull注解_参数校验注解Validated和Valid的区别,这次终于有人说清楚了
  10. java写入txt文件 不替换_java非覆盖写入文件及在输出文本中换行