zoukankan      html  css  js  c++  java
  • Two Sequences (二分+二进制) (好题)

    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 1≤i,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.
    • 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

    初看本题时以为 本题有又是一道找规律的亦或题目

    但是当把表打出来之后发现这是并不能轻易的推出规律

    看了题解用了半天才真正理解了这道题

    对于本题的暴力方法一定是不行的

    那么我们考虑按位来得出答案

    我们对每个b对1<<m 取余

    在对所有的a[i]+b[j]中进行二分

    若啊a[i]+b[j]在mod/2的一倍和二倍之间

    或者三倍和四倍之间 他对这一位就是一 

    如果跑完一遍发现这一位中有n个1

    那么我们这ans中的这一位就是1

    否则就是零

    代码

    #include<bits/stdc++.h>
    using namespace std;
    int a[200005];
    int b[200005];
    vector<int> ve;
    int main()
    {
        int ans=0;
        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]);
        long long tmp=0;
        for(int i=0; i<=29; i++)
        {
            ve.clear();
            int mod=(1<<(i+1));
            for(int j=1; j<=n; j++)
                ve.push_back(b[j]%mod);
            mod/=2;
            sort(ve.begin(),ve.end());
            tmp=0;
            for(int j=1; j<=n; j++)
            {
                int tmpl,tmpr;
                tmpl=lower_bound(ve.begin(),ve.end(),mod*1-a[j]%(mod*2))-ve.begin()-1;
                tmpr=lower_bound(ve.begin(),ve.end(),mod*2-a[j]%(mod*2))-ve.begin()-1;
                tmp+=tmpr-tmpl;
                tmpl=lower_bound(ve.begin(),ve.end(),mod*3-a[j]%(mod*2))-ve.begin()-1;
                tmpr=lower_bound(ve.begin(),ve.end(),mod*4-a[j]%(mod*2))-ve.begin()-1;
                tmp+=tmpr-tmpl;
            }
            if(tmp%2==1) ans=(1<<i)|ans;
        }
        printf("%d
    ",ans);
    
    }
    
  • 相关阅读:
    复习总结
    python 之Tornado
    MySQL 同一Windows系统上安装多个数据库
    CSS 轻松搞定元素(标签)居中问题
    Linux 解决Deepin深度系统无法在root用户启动Google Chrome浏览器的问题
    Django Windows+IIS+wfastcgi 环境下部署
    Django RestFramework(DRF)类视图
    php+ajax实现拖动滚动条分批加载请求加载数据
    Jquery+php鼠标滚动到页面底部自动加载更多内容,使用分页
    jQuery+ajax实现滚动到页面底部自动加载图文列表效果
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852269.html
Copyright © 2011-2022 走看看