CF 1323D-Present(二进制+思维)

题意:http://codeforces.com/problemset/problem/1323/D

题目见图就懂

思路:

一般这种题都是枚举二进制的位。假设现在我们要知道第k位0/1,易知,我们枚举ai,那么2^(k-1)~2^k-1和大于2^k+2^(k-1)的答案都是可以的,所以我们查询在区间-a【i】范围里的数量就是和ai的对数,还有:如果ai就在范围里,要减掉哦

总对数/2就是这位的1数

#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc  exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr strcat
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
#include <iomanip>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
clock_t __START,__END;
double __TOTALTIME;
void _MS(){__START=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
const db E=2.718281828;
const db PI=acos(-1.0);
const ll INF=(1LL<<60);
const int inf=(1<<30);
const db ESP=1e-9;
const int mod=(int)1e9+7;
const int N=(int)1e6+10;

int er[N];
int a[N],b[N];
int get(int n,int x)
{
    int pos=0;
    for(int i=19;i>=1;--i)
    {
        if(pos+er[i]<=n&&b[pos+er[i]]<=x)
            pos+=er[i];
    }
    return pos;
}

int main()
{
    er[1]=1;
    for(int i=2;i<=30;++i)er[i]=er[i-1]*2;
    int n;
    sc("%d",&n);
    for(int i=1;i<=n;++i)sc("%d",&a[i]);
//    _MS();
    int ans=0;
    for(int pos=1;pos<=25;++pos)
    {
        for(int i=1;i<=n;++i)b[i]=a[i]&((1<<pos)-1);
        sort(b+1,b+1+n);
        ll res=0;
        for(int i=1;i<=n;++i)
        {
            int now=b[i];
            int l1=1<<(pos-1),r1=(1<<pos)-1;
            int l2=(1<<(pos-1))+(1<<pos);
            l1-=now,r1-=now;
            l2-=now;
            int cnt1=get(n,r1)-get(n,l1-1);
            if(now>=l1&&now<=r1)cnt1--;
            int cnt2=n-get(n,l2-1);
            if(now>=l2)cnt2--;
            res+=cnt1+cnt2;
        }
        res/=2;
        if(res&1)ans|=1<<(pos-1);
    }
    pr("%d\n",ans);
//    _ME();
    return 0;
}

/**************************************************************************************/

猜你喜欢

转载自www.cnblogs.com/--HPY-7m/p/12581000.html
今日推荐