计算a^b==a+b在(l,r)的对数Codeforces Round #597 (Div. 2)
题: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)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #538 (Div. 2) CTrailing Loves (or L'oeufs?)
这题明白的意思就是求n!在b进制下的后缀零的个数. 即最大的n!%(b^k)==0的k的值.我们需要将如果要构成b这个数,肯定是由一个个质因子相乘得到的.我们只需要求出b的质因子,然后分析n!中可以组 ...
- 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 ...
- 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 ...
- Codeforces Round #574 (Div. 2) D1. Submarine in the Rybinsk Sea (easy edition) 【计算贡献】
一.题目 D1. Submarine in the Rybinsk Sea (easy edition) 二.分析 简单版本的话,因为给定的a的长度都是定的,那么我们就无需去考虑其他的,只用计算ai的 ...
- 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 ...
- 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 ...
随机推荐
- PAT 2019 秋
考试的还行.不过略微有点遗憾,但是没想到第一题会直接上排序和dfs,感觉这次的题目难度好像是倒着的一样.哈哈哈哈. 第一题实在是搞崩心态,这道题给我的感觉是,可以做,但事实上总是差点啥. 第二题,第三 ...
- 图像算法五:【图像小波变换】多分辨率重构、Gabor滤波器、Haar小波
原 https://blog.csdn.net/alwaystry/article/details/52756051 图像算法五:[图像小波变换]多分辨率重构.Gabor滤波器.Haar小波 2018 ...
- Java算法练习——两数之和
题目链接 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...
- java-正则表达式判断移动联通电信手机号
package com.linbilin.phone; import java.util.regex.Matcher; import java.util.regex.Pattern; public c ...
- Python中Opencv和PIL.Image读取图片的差异对比
近日,在进行深度学习进行推理的时候,发现不管怎么样都得不出正确的结果,再仔细和正确的代码进行对比了后发现原来是Python中不同的库读取的图片数组是有差异的. image = np.array(Ima ...
- Linux--Shell传递参数
参考:http://www.runoob.com/linux/linux-shell-passing-arguments.html
- 27. docker compose 单机 均衡负载
1.编写Dockerfile #Dockerfile FROM python:2.7 LABEL maintaner="eaon eaon123@docker.com" COPY ...
- linux文件软链接与硬链接
1.命令格式: ln [参数][源文件或目录][目标文件或目录] 软链接只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间. 2.命令功能: Linux文件系统中,有所谓的链接(link),我们 ...
- 86.QuerySet API常用的方法详解:get方法
get方法的查询条件只能有一条数据满足,如果匹配到多条数据都满足,就会报错:如果没有匹配到满足条件的数据,也会报错. 示例代码如下: from django.http import HttpRespo ...
- mybatis框架快速入门
通过快速入门示例,我们发现使用mybatis 是非常容易的一件事情,因为只需要编写 Dao 接口并且按照 mybatis要求编写两个配置文件,就可以实现功能.远比我们之前的jdbc方便多了.(我们使用 ...