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

原创 2018年04月15日 20:08:02

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

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

atcoder Two Sequences(位处理)

每一位分开处理,希望可以获得所有异或的值中该位的情况. 一开始想用dp的方式,后来发现进位会使得无法处理. 所以直接用求和的方式来判断该位是否存在. 这里的方法是对所有取模,并排序,就可以用二分...
  • m0_37802215
  • m0_37802215
  • 2018-03-24 13:44:22
  • 26

AtCoder Beginner Contest 091 D - Two Sequences

题意:给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1 思路:如果有两个数x,y,且0k,那么当0k时,x+y在2k这一位上的贡献肯定为0。但是如果2k+1k+1+2k,那么x+y在...
  • FrankAx
  • FrankAx
  • 2018-03-18 09:45:39
  • 41

45.Which two statements are true about sequences created in a single instance database? (Choose two.

45.Which two statements are true about sequences created in a single instance database? (Choose two....
  • dwj19830118
  • dwj19830118
  • 2016-02-25 17:49:49
  • 1016

Codeforces 272D Dima and Two Sequences【思维+模拟】

D. Dima and Two Sequences time limit per test 2 seconds memory limit per test 256 megaby...
  • mengxiang000000
  • mengxiang000000
  • 2017-09-20 15:04:44
  • 137

Sequence I

Mr. Frog has two sequences a1,a2,⋯,ana1,a2,⋯,an and b1,b2,⋯,bmb1,b2,⋯,bm and a number p. He wants to...
  • suguoliang
  • suguoliang
  • 2017-08-20 20:02:45
  • 213

HDU - 5918 Sequence I

Mr. Frog has two sequences a1,a2,⋯,ana1,a2,⋯,an and b1,b2,⋯,bmb1,b2,⋯,bm and a number p. He wants ...
  • CS33sun
  • CS33sun
  • 2017-10-10 21:33:13
  • 212

atcoder092(中)ARC092 D - Two Sequences

【题意】给定n个数的数组A和数组B,求所有A[i]+B[j]的异或和(1 #include #include #include #include //#include //#include //#i...
  • qq_36294146
  • qq_36294146
  • 2018-03-18 12:23:07
  • 12

[Atcoder]arc92 D - Two Sequences(二进制+二分)

D - Two Sequences题目链接:D - Two Sequences题目大意:给两个数组长度为n的数组A, B。 计算所有A[i]+B[j]A[i] + B[j]的异或和。数据范围:1≤n≤...
  • ZZZZone
  • ZZZZone
  • 2018-03-26 18:11:39
  • 9

Freemark学习(三):流程控制语句&amp;list&amp;map获

【ps:本文为系列教程,在文章末尾会有本系列其他教程传送门】 在前两节学习中我们知道了freemark的hello Word 以及freemark的部分数据类型。 本节我们继续学习freemark的流...
  • gebitan505
  • gebitan505
  • 2017-08-10 11:32:28
  • 197

Kmp简单应用——Number Sequence

Given two sequences of numbers : a1 1, a2 2, ...... , aN N, and b1 1, b2 2, ...... , bM M ...
  • zdc_8023
  • zdc_8023
  • 2017-03-25 11:17:25
  • 208
收藏助手
不良信息举报
您举报文章:ARC092:Two Sequences(二进制 &amp; 二分)
举报原因:
原因补充:

(最多只允许输入30个字)