AtCoder Regular Contest 092 Two Sequences AtCoder - 3943 (二进制+二分)
Problem Statement
You are given two integer sequences, each of length N: a1,…,aN and b1,…,bN.
There are N2 ways to choose two integers i and j such that 1≤i,j≤N. For each of these N2 pairs, we will compute ai+bj and write it on a sheet of paper. That is, we will write N2 integers in total.
Compute the XOR of these N2 integers.
Definition of XOR
Constraints
- All input values are integers.
- 1≤N≤200,000
- 0≤ai,bi<228
Input
Input is given from Standard Input in the following format:
N a1 a2 … aN b1 b2 … bN
Output
Print the result of the computation.
Sample Input 1
2 1 2 3 4
Sample Output 1
2
On the sheet, the following four integers will be written: 4(1+3),5(1+4),5(2+3)and 6(2+4).
Sample Input 2
6 4 6 0 0 3 3 0 5 6 5 0 3
Sample Output 2
8
Sample Input 3
5 1 2 3 4 5 1 2 3 4 5
Sample Output 3
2
Sample Input 4
1 0 0
Sample Output 4
0 题意:给你两个含有n个数的数组a,b然后我们对每一个a[i] 加上 b[j] 得到的数,把这些数全部异或起来,问最后的异或值是多少? 思路:首先我们对每一个数进行二进制拆分,对每一位进行讨论,只需要讨论二进制的第x位,在所有相加出来得到的数中是奇数个还是偶数个,如果是奇数个就对答案有贡献,贡献值为 1<<x,偶数个就没有贡献。然后问题转化为 我们要咋知道 有多少对 a[i] + b[j] 的第x位为1 由于我们每一步只讨论a[i]+b[j] 的第x位,我们可以只看a[i] 和 b[j] 的 二进制后 x 位,因为我们只需要考虑 x位的情况就知道了 a[i]+b[j] 的 第x位情况,那么我们在枚举第x位的时候,把a,b数组对 2的x+1次方 取模 ,即可得到每个数的二进制后x位。 然后利用这个结论,
对于一对数 a[i] +b[j] = num, 如果我们想要num的二进制第x位为1,需要满足: num <= a[i]+b[j] <2*num 3*num <= a[i]+b[j] < 4*num 这样我们就可以在每一次取模后的数组,对其中一个数组进行排序,然后利用二分找到满足条件的区间,通过区间的长度相加以来判定最终的满足x位是1的数量的奇偶性,来判定 是否在答案上加上贡献。
#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 db(x) cout<<"== [ "<<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 ***/ int a[maxn]; int b[maxn]; int n; int c[maxn]; int d[maxn]; int main() {//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); gbtb;cin >> n;repd(i, 1, n){cin >> a[i];}repd(i, 1, n){cin >> b[i];}int base = 1;ll ans = 0ll;for (int i = 0; i <= 28; i++){repd(j, 1, n){c[j] = a[j] % (2 * base);d[j] = b[j] % (2 * base);}sort(d + 1, d + 1 + n);int num = 0;repd(j, 1, n){int r = lower_bound(d + 1, d + 1 + n, 2 * base - c[j]) - d - 2;int l = lower_bound(d + 1, d + 1 + n, base - c[j]) - d - 1;num += r - l + 1;r = lower_bound(d + 1, d + 1 + n, 4 * base - c[j]) - d - 2;l = lower_bound(d + 1, d + 1 + n, 3 * base - c[j]) - d - 1;num += r - l + 1;}if (num & 1){ans += 1ll * base;}base *= 2;}cout << ans << 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/10828624.html
AtCoder Regular Contest 092 Two Sequences AtCoder - 3943 (二进制+二分)相关推荐
- AtCoder Regular Contest 101 (ARC101) D - Median of Medians 二分答案 树状数组
原文链接https://www.cnblogs.com/zhouzhendong/p/ARC101D.html 题目传送门 - ARC101D 题意 给定一个序列 A . 定义一个序列 A 的中位数为 ...
- AtCoder Regular Contest 100 D - Equal Cut 思维 + 前缀和
传送门 文章目录 题意: 思路: 题意: 给你一个数组aaa,你要将其分成四份,让这四份中和的最大值−-−最小值最小,输出这个最小值. n≤2e5,ai≤1e9n\le2e5,a_i\le1e9n≤2 ...
- AtCoder Regular Contest 100 E - Or Plus Max Sos dp
传送门 文章目录 题意: 思路: 题意: 给你一个长度为2n2^n2n的数组,让你对于所有的1≤k≤2n−11\le k\le 2^n-11≤k≤2n−1求最大的ai+aj,0≤i<j≤2n−1 ...
- AtCoder Regular Contest 065
AtCoder Regular Contest 065 C - Daydream Score : 300300300 points 倒着来就行了,正着来会产生歧义匹配,dreamer,dreamdre ...
- AtCoder Regular Contest 061 E - Snuke‘s Subway Trip(建图 + dijkstra最短路 / 0/1bfs / 并查集)
AtCoder Regular Contest 061 E - Snuke's Subway Trip problem 洛谷翻译 my idea 最近一直在做网络流,所以一读这题后,我就想到了最小费用 ...
- NOMURA Programming Contest 2021(AtCoder Regular Contest 121)
文章目录 A - 2nd Greatest Distance B - RGB Matching C - Odd Even Sort D - 1 or 2 E - Directed Tree F - L ...
- AtCoder题解——AtCoder Regular Contest 107——B - Quadruple
题目相关 题目链接 AtCoder Regular Contest 107 B 题,https://atcoder.jp/contests/arc107/tasks/arc107_b. Problem ...
- Atcoder Regular Contest 92 D Two Sequences F Two Faced Edges 两道神题
Atcoder 3943 Two Sequences Atcoder 3945 Two Faced Edges Atcoder 3943 Two Sequences 给两个长度为n的数组a,b,求(a ...
- AtCoder Regular Contest 071 D - 井井井 / ###
题目:http://arc071.contest.atcoder.jp/tasks/arc071_b 题意: 有一个二维的平面,给你xn根竖线和ym根横线,问这些线围成的长方形(正方形)的面积和(要求 ...
最新文章
- html 图片自动切换插件,jquery图片切换插件
- 写在开年:移植wolfssl4.3.0到w60x_sdk_3.04时的一点问题
- 设置背景图片,解决手机上背景图片高度适应问题
- D-hdu 1465 不容易系列之一(递推)
- pytorch 之 torch.bmm()函数
- 易语言html规则分析,易语言算法原理浅析【一】(示例代码)
- 解释型语言和编译型语言的区别
- 关于tcp网络通讯的几个场景的小测试
- sqlserver提权失败_利用mssql模拟登录提权
- python钓鱼评论爬取
- ECSHOP用户评论
- DeepFaceLab 2 merge阶段参数
- 我眼中的国内IT书籍以及各大出版社
- 确实有必要好好学英语
- LFS(Linux From Scratch)构建过程全记录(一):准备工作
- 程序员的8条解bug(甩锅)大招!
- MybatisPlus整合SpringBoot全教程,用起来不要太方便
- 第十六章:开发工具-traceback:异常和栈轨迹-底层异常API
- 【Arduino实验09 外部中断触发报警】
- 电子信息工程班徽设计_青春日职 | 电子信息工程学院成功举办“心系我班,熠熠生徽”班徽设计大赛...