[AtCoderContest015D]A or...or B Problem
[AtCoderContest015D]A or...or B Problem
试题描述
Nukes has an integer that can be represented as the bitwise OR of one or more integers between \(A\) and \(B\) (inclusive). How many possible candidates of the value of Nukes's integer there are?
求 \(A\) 和 \(B\) 之间选若干个数按位或起来有多少种可能的值。
输入
The input is given from Standard Input in the following format:
A
B
输出
Print the number of possible candidates of the value of Nukes's integer.
输入示例1
7
9
输出示例1
4
输入示例2
65
98
输出示例2
63
输入示例3
271828182845904523
314159265358979323
输出示例3
68833183630578410
数据规模及约定
\(1 \le A \le B < 2^{60}\)
\(A\) and \(B\) are integers.
题解
首先,\([A, B]\) 范围内的数肯定是能表示出来的。然后我们考虑一下它能表示出的最小的数和最大的数。
最小的数肯定是 \(A\)。然后我们考虑将 \(A\) 和 \(B\) 分解成二进制的形式,设它们前 \(k\) 位的二进制都相同,那么第 \(k+1\) 位上 \(B\) 的肯定是 \(1\),\(A\) 的肯定是 \(0\),那么最大值就很好考虑了。我们现在用 \((XXX...)_2\) 表示前 \(k\) 位相同的二进制,那么 \([A, B]\) 区间可以被拆成 \([A, (XXX...011...)_2]\) 和 \([(XXX...100...)_2, B]\),下面的数字应该会直观一些:
XXX...0****** = A
XXX...0111111 = C
XXX...1000000 = D
XXX...1001--- = B
上面的例子就假设后面只有 \(7\) 位,其中 * 表示 \(A\) 的后几位,- 表示 \(B\) 的后几位。
那么显然我们可以用 \(C\texttt{ }or\texttt{ }D\) 得到最大值 \(XXX...1111111\)。
除此之外,区间 \([A, C]\) 中每个数都可以和 \(D\) 进行按位或运算得到下面这个范围的所有数:
XXX...1****** = E
XXX...1111111 = F
注意到我们用 \([D, B]\) 内部的所有数按位或起来可以得到这个范围的所有数:
XXX...1000000 = D
XXX...1001111 = G
然后我们发现除此之外,再无其他可以搞出来的数了。考虑只用 \([A, G]\) 中的数,组出来的是 \([A, G]\) 或 \([E, F]\) 的数;只用 \([E, F]\) 的数,只能组出来 \([E, F]\) 中的数;两个区间都用只能组出来 \([E, F]\) 中的数,因为数是越或越大的。
所以最后答案就是 \([A, G] \bigcup [E, F]\) 中的所有数了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std;
#define LL long long
LL read() {
LL x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
int main() {
LL l = read(), orgl = l, r = read(), orgr = r, tr = r, ans;
for(int i = 59; i >= 0; i--)
if((l ^ r) >> i & 1) {
l |= 1ll << i;
int tmp = i;
for(tmp--; tmp >= 0; tmp--) tr |= 1ll << tmp;
for(i--; i >= 0 && !(r >> i & 1); i--) ;
for(i; i >= 0; i--) r |= 1ll << i;
break;
}
if(r >= l) return printf("%lld\n", tr - orgl + 1), 0;
ans = r - orgl + 1 + tr - l + 1;
printf("%lld\n", ans);
return 0;
}
[AtCoderContest015D]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 ...
随机推荐
- 2002-2003 ACM-ICPC Northeastern European Regional Contest (NEERC 02) H Heroes Of Might And Magic (隐含dp)
问题是求一个方案,实际隐含一个dp.法力是递减的,所以状态是DAG,对于一个确定的状态,我们贪心地希望英雄的血量尽量大. 分析:定义状态dp[i][p][h]表示是已经用了i的法力值,怪兽的位置在p, ...
- UVA 11925 Generating Permutations 生成排列 (序列)
题意:要用一个有序的序列生成给定序列,操作有两种,一是交换前两个元素,二是把第一个元素移动到最后去. 思路有两种: 1.映射,把给定序列映射成有序的序列,然后按照同样的替换规则把有序的序列映射掉,然后 ...
- Kunernetes集群架构与组件
架构如图: master节点:主要是集群控制面板的功能,来管理整个集群,包括全局的角色,调度,都是在master节点进行控制 有三个组件: API Server: 是 k8s提供的一个统一入口,它是 ...
- java中的同步与异步
在多线程的环境中,经常会碰到数据的共享问题,即当多个线程需要访问同一个资源时,它们需要以某种顺序来确保该资源在某--时刻只能被-一个线程使用,否则,程序的运行结果将会是不可预料的,在这种情况下就必须对 ...
- php的字符转换 & php登入注册界面设计以及源码 & 分离公共部分
我们在编写的时候总是会出现乱码 https://www.cnblogs.com/mafeng/p/5827215.html php登入注册界面设计以及源码 https://blog.csdn.net/ ...
- NSCharacterSet去除字符串中的空格、删除指定\任意字符集
一.去除首尾的空格 /** 1.去除首尾的空格*/ NSString *strMsg=@" 简书作者:CoderZb "; NSString *strResult = [strMs ...
- 几种排序算法的比较转自http://blog.csdn.net/keenweiwei/article/details/3697452
1冒泡排序: 已知一组无需数据a[1],a[2],a[3],a[4],a[5][a[n],将其按升序排列,首先找出这组数据中最大值,将a[1]与a[2]比较,若a[1]大,则交换两者的值,否则不变,在 ...
- python3爬取墨迹天气并发送给微信好友,附源码
需求: 1. 爬取墨迹天气的信息,包括温湿度.风速.紫外线.限号情况,生活tips等信息 2. 输入需要查询的城市,自动爬取相应信息 3. 链接微信,发送给指定好友 思路比较清晰,主要分两块,一是爬虫 ...
- Android内核编译步骤
android_4.0.4_tq210$ source build/envsetup.shandroid_4.0.4_tq210$ lunch 5/android_4.0.4_tq210$ make ...
- Python中re(正则表达式)模块使用方法
Python中常用的正则表达式处理函数: re.match re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词. import re text = "JGood ...