[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 ...
随机推荐
- java 自定义一个容器类
public class ArrayList { public int index = 0; Object[] objects = new Object[2]; public void add(Obj ...
- 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系
今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系". ...
- 01_1JAVA简介
01_1JAVA简介 1. Java基础 语法基础.OO.Exception.Array.基础类.I/O Stream.Collection /Generic.Thread.TCP/UDP.GUI.M ...
- linux更新git
在CentOS中使用yum install git安装的git是1.7版本的,所以需要更新1.9以及更高版本的git. 安装方法如下: 1.安装依赖的包: yum -y install curl-de ...
- mac 使用homebrew 安装mysql
1. 安装homebrew ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" brew update ...
- module.exports exports 和export export default
首先可以知道的是这是两组不同模块规范. module.exports 是CommonJS模块规范,通过require 导入 a.js: var x = 'hello' module.exports.x ...
- 09GNU C语言程序编译
1. C 语言程序概述 GNU gcc 对 ISO 标准 C89 描述的 C 语言进行了一些扩展,其中一些扩展部分已经包括进 IOS C99 标准中.本节给出了内核中经常用到的一些 gcc 扩展语 ...
- 基于Centos7.2使用Cobbler工具定制化批量安装Centos7.2系统
1.1 定制Centos_7_x86_64.ks文件内容 # Cobbler for Kickstart Configurator for CentOS 7.2.1511 by Wolf_Dre ...
- SQL语句小练习
一.创建如下表结构(t_book) Id 主键 自增一 bookName 可变长 20 Price 小数 Author 可变长20 bookTypeId 图书类 ...
- poj 1321 排兵布阵问题 dfs算法
题意:有不规则地图,在上面放n个相同的棋子,要求摆放的时候不同行不同列.问:有多少种摆法? 思路:dfs+回溯 用一个book[]数组来表示当前列是否有放棋子 一行一行的遍历,对一行来说遍历它的列,如 ...