Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)B(位运算,二分查找)

从低到高枚举当前位i,把所有数字对1<<(i+1)取模,因为比i位高的数字不会影响到低位,在这些数中,两两组成一对,每对的和如果在第i位上为1,++计数,如果计数为奇数,则答案上这一位为1。这个组对的过程通过排序后二分查找完成。

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int a[400007];
 5 int b[400007];
 6 int main(){
 7     ios::sync_with_stdio(false);
 8     cin.tie(NULL);
 9     cout.tie(NULL);
10     int n;
11     cin>>n;
12     for(int i=1;i<=n;++i)
13         cin>>a[i];
14     int ans=0;
15     for(int i=0;i<26;++i){
16         for(int j=1;j<=n;++j)
17             b[j]=a[j]%(1<<(i+1));
18         sort(b+1,b+1+n);
19         long long sum=0;
20         for(int j=1;j<=n;++j){
21             sum+=n-(lower_bound(b+1,b+1+n,(1<<i)+(1<<(i+1))-b[j])-b-1);//两个数相加大于等于2^i+2^(i+1)
22             if(b[j]*2>=(1<<i)+(1<<(i+1)))//查找到的数中包含b[j]自己
23                 --sum;
24             sum+=upper_bound(b+1,b+1+n,(1<<(i+1))-1-b[j])-lower_bound(b+1,b+1+n,(1<<i)-b[j]);//两个数相加大于等于2^i,小于等于2^(i+1)-1
25             if(b[j]*2>=(1<<i)&&b[j]*2<=(1<<(i+1))-1)//查找到的数中包含b[j]自己
26                 --sum;
27         }
28         if((sum/2)&1)//不分前后的查找多计算了一倍
29             ans+=(1<<i);
30     }
31     cout<<ans;
32     return 0;
33 }

原文地址:https://www.cnblogs.com/ldudxy/p/12444204.html

时间: 2024-08-30 04:26:37

Codeforces Round #626 (Div. 1, based on Moscow Open Olympiad in Informatics)B(位运算,二分查找)的相关文章

Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)

A. Even Subset Sum Problem 题意:找和是偶数的子集,没什么好说的,直接上代码. #include <iostream> #include <algorithm> using namespace std; int n,x; int a[110]; int main() { int t; scanf("%d",&t); while(t--) { x=0; scanf("%d",&n); for(int i

Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)【ABCD】(题解)

目录 涵盖知识点:思维.树状数组. 比赛链接:传送门 A - Even Subset Sum Problem B - Count Subrectangles C - Unusual Competitions D - Present E - Instant Noodles F - Reality Show 涵盖知识点:思维.树状数组. 比赛链接:传送门 A - Even Subset Sum Problem 题意: 找一个子序列使得和为偶数 题解: 选一个偶数或者两个奇数. Accept Code

Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)(A-C)

题意:根据第一段,只是让你找到任意一组元素,和为偶数即可.还可以根据OUTPUT最后一句,多种答案,输出任意一种.    解析:随便找嘛,所以如果碰到单个偶数,直接输出,否则找出任意两个奇数输出即可.都没有,就输出-1. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<map> using namespace std; typedef l

Codeforces Round #500 (Div. 2) [based on EJOI]

Codeforces Round #500 (Div. 2) [based on EJOI] https://codeforces.com/contest/1013 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define IT set<node>::iterator 6 #def

Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High [贪心 II][数据结构 I]

题目:http://codeforces.com/contest/867/problem/E 题意:模拟股票操作,每天只能买一只股票或者卖一只股票或者什么也不做,求最大利润. 题解:仔细想想是非常简单的一个贪心问题,理解为连续的多次贪心买入卖出可以合并为一次的买入卖出,且值为最优.只需要用一个最小堆每次寻找前面的最小且没有被标记为购买点的值即可.如果自己为最小值,continue.如果那个值没有被选过,为买入点,pop.如果那个值被选为了售出的点,那么取消售出的标记,把当前点标记为售出点,利润直

D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)

http://codeforces.com/contest/851/problem/D 分区间操作 1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano]

http://codeforces.com/contest/1079/problem/C 题目大意:给出一个数列a[n],让构造一个满足下列条件的数列b[n]:如果a[i]>a[i-1]那么b[i]>b[i-1],如果a[i]<a[i-1]那么b[i]<b[i-1],如果a[i]==a[i-1],那么b[i]!=b[i-1].其中1<=b[i]<=5  1<=a[i]<=2*1e5. 题解:dp[i][j]表示在位置i处取j时是否成立,如果成立则dp[i][

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最长我们肯定是取最小x和最大的y连起来就完事. 但是要求长度最小又得覆盖,那么可以这样想,我们需要把最小的x不断右移到这条线段的y, 最大的左移到x,所以就是最大x-最小y完事 #include <bits/stdc++.h> using namespace std; #define ll long

【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B:数学+排序 C:字符串搜索 A // https://codeforces.com/contest/1277/problem/A /* 题意: 给出一个数,求不大于该数的完美整数的个数(完美整数指全是一个数组成的数字,如:111, 333333, 444444, 9, 8888 ...) 分析: