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 ...
随机推荐
- leetcode 35 Search Insert Position(二分法)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Android终端与服务器数据传输解决方案
Android终端与服务器数据传输解决方案 Android终端三种与服务器传输方式: Socket传输 WebService传输 Post/Get获取数据方式 网络实现条件 端口:指定 协议:TC ...
- CURL抓取网页内容
<?php $curl = curl_init();//初始化一个cURL对象 $url = "http://cart.jd.com/cart/cart.html?backurl=ht ...
- 【LeetCode】081. Search in Rotated Sorted Array II
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- windows7下c++11环境搭建
1.安装codeblocks 13.12 2.下载安装tdm-gcc-4.8.1-3 3.配置coldblocks的编译器(settings->compiler->compiler set ...
- Poj 1316 Self Numbers(水题)
一.Description In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called se ...
- Express Route的配置
ExpressRoute在中国已经Preview了. 本篇文章讲介绍ExpressRoute如何配置. Express Route的逻辑拓扑结构: 在配置Express Route之前,需要做VLAN ...
- UML核心元素--边界
定义:边界是无形的,是可大可小的,同时参与者.用例和边界又有着相生相克的性质.与其说边界是UML元素,还不如说它是一种分析方法. 1.需求是动态的过程:系统边界是无形的,看不到的,不好理解,倒不如说需 ...
- 继承自DynamicObject的对象的Xml序列化
默认情况下,对继承自DynamicObject的对象进行序列化操作是不会报错的,但是并没有实际序列化出任何东西来 为了让它进行序列化,我们改造一下实现类,实现IXmlSerializable接口 代码 ...
- ES6学习之字符串的扩展
字符的Unicode表示法 \uxxxx可以表示一个双字节的字符(xxxx表示字符编码,范围在0000---FFFF),超出此范围用两个双字节表示. console.log("\u0061& ...