AT2382 A or...or B Problem
传送门
还是看题解的啦
先考虑一个显而易见的结论:A和B二进制下最高的几位相同是没用的(设去掉的那些位之和为sum)
然后我们设\(d\)为二进制下从高位到低位第一位不相同的,\(k\)为B从高位到低位第二个不为0的
然后我们分几段来统计答案
首先,\([A,2^d-1+sum]\)显然是可以凑出来的
然后,考虑\(k\),发现\([2^d+sum,2^d+2^{k+1}+sum-1]\)也是可以凑出来的
最后,我们发现还有一种情况漏算了,确定\(d\),所以区间就是\([A+2^d,sum+2^{d+1}]\)
求并就好啦
讲的好乱啊,应该只有我一个人会来看吧
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
void read(long long &x){
char ch;bool ok;
for(ok=0,ch=getchar();!isdigit(ch);ch=getchar())if(ch=='-')ok=1;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok)x=-x;
}
#define rg register
const int maxn=110;
long long a,b,x,y,l,r,ans,ll,rr;
int aa[maxn],bb[maxn],lena,lenb,len,k;
int main(){
read(a),read(b);x=a,y=b;
while(a)aa[++lena]=a&1,a>>=1;
while(b)bb[++lenb]=b&1,b>>=1;
for(rg int i=max(lena,lenb);i;i--){
if(aa[i]!=bb[i]){len=i;break;}
x-=1ll*aa[i]<<(i-1);
y-=1ll*bb[i]<<(i-1);
}
for(rg int i=len-1;i;i--)if(bb[i]){k=i;break;}
l=x,r=(1ll<<(len-1))+(1ll<<k)-1;
ll=(1ll<<(len-1))+x,rr=(1ll<<len)-1;
if(ll>r)ans=r-l+1+rr-ll+1;
else ans=rr-l+1;
printf("%lld\n",ans);
}
AT2382 A or...or B Problem的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
随机推荐
- 获取window状态栏和标题栏的高度
1.获取状态栏高度: decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFram ...
- Qt Create 4.6.2无法自动生成Android Kit
开发环境: OS,Microsoft Windows [Version 10.0.17134.523] Qt,5.11.1 Qt Creator,4.6.2 JDK,1.8.0_181 Android ...
- python【事物 】【数据库锁】
1.数据库事物 1. 什么是事务 事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消.也就是事务具有原子性,一个事务中的一系列的操作要么全部成功,要么一 ...
- zoj 3872
D - Beauty of Array Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu S ...
- Quartz.NET+TopSelf 实现定时服务
转载http://www.cnblogs.com/jys509/p/4628926.html Quartz.NET 入门 2015-07-09 00:59 by jiangys, 67858 阅读, ...
- vs快捷键复制当前行
vs快捷键 1)如果你想复制一整行代码,只需将光标移至该行,再使用组合键“Ctrl+C”来完成复制操作,而无需选择整行.2)如果你想剪切一整行代码,只需将光标移至该行,再使用组合键“Ctrl+X”来完 ...
- Zeppelin的入门使用系列之创建新的Notebook(一)
不多说,直接上干货! 前期博客 hadoop-2.6.0.tar.gz + spark-1.6.1-bin-hadoop2.6.tgz + zeppelin-0.5.6-incubating-bin- ...
- shell脚本监控MySQL主从同步
企业面试题1:监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员. 阶段1:开发一个守护进程脚本每30秒实现检测一次. 阶段2:如果同步出现如下错误号(1158,1159,1008, ...
- Linux中发布项目的一些命令笔记
记一下,Linux中发布项目的一些难记的命令: .安装jdk a.检测是否安装了jdk 运行java -version b.若有需要将其卸载 c.查看安装那些jdk rpm -qa | grep ja ...
- 一些奇怪的Javascript用法
阅读AngularJS时,看到一些奇怪的Javascript用法.1.(function(){ a.work=function(){} })(a) 声明一个匿名函数并执行 2. ...