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

本文介绍了一种计算两个数组所有组合异或和的方法。通过逐位讨论并利用二分查找技术来解决进位问题,实现了高效求解。文章提供了一个完整的C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值