ARC092:Two Sequences(二进制 & 二分)

版权声明:本文为博主原创文章,未经博主允许可以随意转载。 https://blog.csdn.net/junior19/article/details/79952462

Problem Statement

You are given two integer sequences, each of length Na1,…,aN and b1,…,bN.

There are N2 ways to choose two integers i and j such that 1i,jN. 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.
  • 1N200,000
  • 0ai,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

Copy
2
1 2
3 4

Sample Output 1

Copy
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

Copy
6
4 6 0 0 3 3
0 5 6 5 0 3

Sample Output 2

Copy
8

Sample Input 3

Copy
5
1 2 3 4 5
1 2 3 4 5

Sample Output 3

Copy
2

Sample Input 4

Copy
1
0
0

Sample Output 4

Copy
0
题意:给两个数组A和B分别有N个数,求所有A[i]+B[j](1<=i<=N, 1<=j<=N)共N*N个数的异或和。

思路:每个位可以分别讨论,但是又涉及到进位问题,那么枚举第i位时直接截取A和B的0~i的部分,然后二分就行。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5+30;
LL a[maxn], b[maxn], ans=0;
int main()
{
    int n, l, r;
    scanf("%d",&n);
    for(int i=0; i<n; ++i) scanf("%lld",&a[i]);
    for(int i=0; i<n; ++i) scanf("%lld",&b[i]);
    for(int i=28; ~i; --i)
    {
        LL s = 1LL<<i, tmp=0;
        for(int j=0; j<n; ++j) a[j]&=(s<<1)-1, b[j]&=(s<<1)-1;
        sort(b,b+n);
        for(int j=0; j<n; ++j)
        {
            l = lower_bound(b,b+n,s-a[j])-b;
            r = lower_bound(b,b+n,(s<<1)-a[j])-b;
            tmp += r-l;//不进位
            l = lower_bound(b,b+n,(s<<1|s)-a[j])-b;
            r = lower_bound(b,b+n, (s<<2)-a[j])-b;
            tmp += r-l;//进位
        }
        if(tmp&1) ans |= s;
    }
    printf("%lld\n",ans);
    return 0;
}

阅读更多

没有更多推荐了,返回首页