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 ...
随机推荐
- pyglet模块的EventDispatcher(事件派发对象)
事件派发对象用于处理事件的派发与响应,pyglet的window对象正是继承了它才具有处理事件的能力. 步骤: 1.注册事件类型: EventDispatcher.register_event_typ ...
- Mybatis学习--日志
学习笔记,选自Mybatis官方中文文档:http://www.mybatis.org/mybatis-3/zh/logging.html Logging Mybatis内置的日志工厂提供日志功能,具 ...
- uoj279温暖会指引我们前行
暖气来啦~ 动态树维护最大生成树裸题 #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- bzoj 2733 永无乡 线段树
题目: 支持两种操作: 合并两点所在的联通块 查询某点所在联通块内权值第k小. 题解 平衡树启发式合并随便搞一搞就好了. 我写了一个线段树合并 #include <cstdio> #inc ...
- [转]CSS遮罩——如何在CSS中使用遮罩
特别声明:此篇文章由D姐根据Christian Schaefer的英文文章原名<CSS Masks – How To Use Masking In CSS Now>进行翻译,整个译文带有我 ...
- bzoj 1941 [Sdoi2010]Hide and Seek——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1941 第二道KDtree! 枚举每个点,求出距离它的最远和最近距离.O( n * logn ...
- linux日常管理-rsync_ssh方式
现在我们有两台机器,两台机器都需要安装rsync yum -y install rsync 一台的主机名是wangshaojun IP是192.168.1.117 ,另一台的主机名是 ...
- Java进阶之美文共享
2.在Java中如何避免"!=null"式的判空语句? 3.Java问答:终极父类(3) Java问答:终极父类(下) Java问答:终极父类(上) 内存不足:杀死进程还是牺牲 ...
- 115个Java面试题和答案
面向对象编程(OOP) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可靠性和灵活性. 增加代码 ...
- Activity--弹出底部窗口
第一步 : 退出时候的布局文件exit_dialog_from_settings.xml <?xml version="1.0" encoding="UTF-8&q ...