题:https://codeforces.com/contest/1245/problem/F

分析:转化为:求区间内满足a&b==0的对数(解释见代码)

///求满足a&b==0在区间【l,r】的对数
///推导:区间[2l,2r]可由[l,r]乘3倍得来
///原因:*2我们可以看成事左移1位,那么这个位置上,对于俩个数来说
/////////可以取0,1 或1,0或0,0才依然满足 a&b==0这个题目条件
/////////这个公式可以用递归推导回溯计算,
/////////当我们回递归到区间为奇数的情况的时候,我们想办法让它变成偶数
/////////假设[a,b]中a是奇数,那么我们就把它弄成a+1,然后加上漏算的部分即可
/////////g(x,y)表示在区间[0,b]中和x满足题目条件的个数
/////////然后漏算的部分就是 (g(a,b)-g(a,a))*2
/////////b为奇数的情况也照样这样分析
/////////完 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
ll g(int a,int b){
ll ans=;
ll num=;
for(int i=;i<=b;i<<=){
if(i&b){
b^=i; if(!(a&b))
ans+=1ll<<num;
}
if(!(i&a)){///a在二进制下i位置为0的情况下,num就是计数这种情况的
num++;
}
}
return ans;
}
ll cal(int a,int b){
if(a==)
return *b-+cal(,b);
if(a==b)
return ;
ll ans=;
if(a&){///若左区间的值为奇数
ans+=(g(a,b)-g(a,a))*;///乘2是对数可以互换,然后这个就是剪掉区间缩成偶数时加上漏加的部分,类似于用前缀和算区间和
a++;
}
if(b&){///若右区间的值为偶数
ans+=(g(b-,b)-g(b-,a))*;
b--;
}
return ans+*cal(a/,b/);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int a,b;
scanf("%d%d",&a,&b);
printf("%I64d\n",cal(a,b+));
}
return ;
}

计算a^b==a+b在(l,r)的对数Codeforces Round #597 (Div. 2)的更多相关文章

  1. Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

    B. Hamming Distance Sum   Genos needs your help. He was asked to solve the following programming pro ...

  2. Codeforces Round #532 (Div. 2)- C(公式计算)

    NN is an experienced internet user and that means he spends a lot of time on the social media. Once ...

  3. Codeforces Round #114 (Div. 1) B. Wizards and Huge Prize 概率dp

    B. Wizards and Huge Prize Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  4. Codeforces Round #538 (Div. 2) CTrailing Loves (or L'oeufs?)

    这题明白的意思就是求n!在b进制下的后缀零的个数. 即最大的n!%(b^k)==0的k的值.我们需要将如果要构成b这个数,肯定是由一个个质因子相乘得到的.我们只需要求出b的质因子,然后分析n!中可以组 ...

  5. Codeforces Round #377 (Div. 2) A. Buy a Shovel【暴力/口袋里面有无限枚 10 元和一枚 r 面值的硬币,问最少可以买多少把价值为 k 的铁铲】

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection

    C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 m ...

  7. Codeforces Round #574 (Div. 2) D1. Submarine in the Rybinsk Sea (easy edition) 【计算贡献】

    一.题目 D1. Submarine in the Rybinsk Sea (easy edition) 二.分析 简单版本的话,因为给定的a的长度都是定的,那么我们就无需去考虑其他的,只用计算ai的 ...

  8. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  9. Codeforces Round #330 (Div. 1) A. Warrior and Archer 贪心 数学

    A. Warrior and Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/594 ...

随机推荐

  1. 获取网站IP地址(Linux,C)

    #include <netdb.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> ...

  2. Vuex 是什么

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vue 的官方调试工具  ...

  3. openstack trove weekly meeting时间即将更改

    为了平衡英国.巴黎.德国.美国和中国开发者的作息习惯,openstack trove项目组在5月18日的weekly meeting上开始讨论新的开会时间. 当前的开会时间是,周三 UTC 18:00 ...

  4. shell 疑难

    #!/bin/bashBIN=`which $0`BIN=`dirname ${BIN}`BIN=`cd "$BIN"; pwd`  #列出脚本所在目录全局路径

  5. Iterator迭代器,获取集合元素

    * Object next() :返回下一个元素      * boolean hasNext():判断时是否有元素可以获取 public static void main(String[] args ...

  6. mysql插入文本文档及读取

    1.把本地的一个文件插入到数据库中,数据库字段用text保存 public static void main(String[] args) { PropKit.use(“pro.txt”);Druid ...

  7. 墙壁涂色(DP)

    蒜头君觉得白色的墙面好单调,他决定给房间的墙面涂上颜色. 他买了 3 种颜料分别是红.黄.蓝,然后把房间的墙壁竖直地划分成 n 个部分,蒜头希望每个相邻的部分颜色不能相同. 他想知道一共有多少种给房间 ...

  8. 干货 | 京东云原生容器—SpringCloud实践(一)

    "云原生"成为近年热词并不是一种偶然,它不是一个软件,也不是一种框架,而是一堆理念集合,以及围绕这些理念所产生的一些最佳实践的工具.云原生天然就是作用于服务架构的,可以视作一个服务 ...

  9. 协程与异步IO

    协  程 协程,又称微线程,纤程. 协程的优势:              协程的特点在于是一个线程执行.              协程的最大优势是极高的执行效率,因为子程序切换不是线程切换,而是由 ...

  10. B - Common Divisors (codeforces)数论算法基本定理,唯一分解定理模板

    You are given an array aa consisting of nn integers. Your task is to say the number of such positive ...