AtCoder Beginner Contest 091 D - Two Sequences

D - Two Sequences


Time limit : 3sec / Memory limit : 256MB

Score : 500 points

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



这道题的意义在于把数拆成二进制,顶多28位,那么二进制的每一位要么是0,要么是1,先拆第一组,在将第二组每一个数按位拆开,看会不会与第一组值的相同位产生进位,有进位的的直接异或和就OK,没有的不用管,你会发现如果这样写不优化复杂度会达到O(28*n*n),会超时,优化,把第一组数按位排序,然后二分去找即可。比如第二组中的一个数一位是0,那么就不可能产生进位,因为第一组这位上找不到2(只有0或1;第二组中的一个数一位是1,那那么二分搜索出1的位置k即可,利用(n-k+1)就是这个数在这一级上产生进位的数量。复杂度可降至O(28*nlogn).然后算不进位的.第一组同位上0的个数a,1的个数b;第二组同位上0的个数c,1的个数d;公式为((a*d)%2+(b*c)%2)%2,直接和之前的进位异或值异或即可。所以复杂度为O(30*n)

主要是二进制+异或和时时处理+二分 O(28*nlogn+30*n)



//aaakirito
#include 
#include 
using namespace std;
#define mmax 200000+10
int a[mmax],b[mmax],x[30][mmax],y[mmax];
int fun(int k,int n,int v){//二分
    int l=1,r=n;
    int h=(l+r)/2;
    while(l<=r){
        if(v<=x[k][h]&&x[k][h-1]x[k][h])
            l=h+1;
        h=(l+r)/2;
    }
    return n+1;
}
int main() {
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    for(int k=0;k<28;k++){
        if(k==0)
            for(int i=1;i<=n;i++) x[k][i]=a[i]&1;//2ˆ0次幂,a[i]最后一位的拆分
        else
            for(int i=1;i<=n;i++) x[k][i]=x[k-1][i]^((1<


你可能感兴趣的:(ACM算法题)