[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的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. 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 ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [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 ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

随机推荐

  1. AutoWidthInput

    import React from 'react'; import PropTypes from 'prop-types'; class AutoWidthInput extends React.Co ...

  2. 两个有序数列,求中间值 Median of Two Sorted Arrays

    原题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...

  3. java基础—多态(动态加载)

    一.面向对象最核心的机制——动态绑定,也叫多态

  4. SCOPE_IDENTITY和@@IDENTITY[转]

    本文转自:http://www.cnblogs.com/daydayupanan/archive/2008/09/04/1283648.html SCOPE_IDENTITY和@@IDENTITY的作 ...

  5. cocos2dx lua 打印和保存日志

    在2d游戏中,经常会出现闪退或者报错的问题,通过写文本,将日志文件发送给服务端,让后端人员进行分析. 通过lua打印日志在文本文件中: local file = io.open(cc.FileUtil ...

  6. python面向对象之抽象工厂设计模式

    class truck: def use(self): return "拉货" def __str__(self): return "大卡车" class mi ...

  7. cephfs 挂载 卸载

    #挂载 sudo ceph-fuse -m 10.1.xx.231:6789,10.1.xx.232:6789,10.1.xx.233:6789 -r /MySQL-BK /data/backup # ...

  8. PHP计算两个日期相差的年月日时分秒

    $start_time = '2017-09-06 15:12:20'; $end_time = '2018-09-08 10:20:45'; get_time($start_time,$end_ti ...

  9. 05tar命令详解

    tar 命令用于对文件进行打包压缩或解压,格式为"tar [选项][文件]". ​ 在Linux 系统中,常见的文件格式比较多,其中主要使用的是 .tar 或者 .tar.gz 或 ...

  10. 【Spring】事务的实现方式

    1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:转账. 场景设定: 用户名 余额 A 1000 B 1000 操作: A通过支付宝给B转账200块,做这件事情会进行两个操作. 1:A账号- ...