agc015D A or...or B Problem
题意:求用若干个(至少一个)[A,B]中的数进行or操作能得到多少本质不同的数
$1 \leq A \leq B < 2^{60}$
一直在想数位dp,看了题解之后感觉自己就是个sb
我们先把$A,B$前面(高位)相同的二进制位忽略掉,反正无论怎么或都是一样的
那么我们找到一个最大的$p$使得$A>>p \& 1 \ne B>>p \& 1$
下面的$A,B$都是把$p+1$位之后忽略掉(看作0)之后的。
令$T=1<<p$,我们把$[A,B]$看作两部分,$X=[A,T)$和$Y=[T,B]$
我们可以分为三种情况:
1、只选$X$里面的数,最后或出来的数的范围是$[A,T)$
2、只选$Y$里面的数。
令$k$是$B$里面最高位的1的位置(除了$p$这一位)
最后或出来的数的范围就是$[T,T+(1<<k+1)-1]$
3、两个集合里面的数都要选,最后或出来的数的范围是$[T+A,2*T-1]$,就是选$T$和一个$X$里面的数
注意第二种情况和第三种情况有重叠部分。
我怎么把题解翻译了一遍……
这么低的复杂度也让我有点惊讶呢
这种题特点在于,集合里面数是连续的,然后或起来也就是几段连续的。
//Serene
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
#define db double
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define lc son[pos][0]
#define rc son[pos][1]
const int W=60;
ll n,m,ans; char cc;ll ff;
template<typename T>void read(T& aa) {
aa=0;ff=1; cc=getchar();
while(cc!='-'&&(cc<'0'||cc>'9')) cc=getchar();
if(cc=='-') ff=-1,cc=getchar();
while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
aa*=ff;
} int lb(ll x) {int rs=0;while(x) rs++,x>>=1;return rs;} int main() {
read(n); read(m);
ll p=W,x;
while(p>=0&&((n>>p)&1)==((m>>p)&1)) p--;
if(p>=0) {
n&=(1LL<<p+1)-1;
m&=(1LL<<p)-1;
ans+=(1LL<<p)-n;
x=lb(m);
ans+=(1LL<<x);
n=max(n,1LL<<x);
ans+=(1LL<<p)-n;
}
else ans=1;
printf("%lld\n",ans);
return 0;
}
agc015D 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基础之Date类
Date类: Date类概述 类 Date 表示特定的瞬间,精确到毫秒. 构造方法 public Date() public Date(long date) 成员方法 public long getT ...
- C# 判断当前请求是GET、还是POST ?
方法一: HttpContext.Current.Request.RequestType == "POST" //当前请求为:POST 方法二: if(Request.Serv ...
- springboot 2 集成 redis 缓存 序列化
springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...
- Centos7解决在同一局域网内无法使用ssh连接
参考: https://www.cnblogs.com/liyuanhong/articles/5785368.html 一.修改网卡设置 nano /etc/sysconfig/network-sc ...
- jQuery3动画+创建元素
一.jQuery的动画 1.jQuery自带的动画 1>变化的是width height opacity display <!DOCTYPE html> <html lang= ...
- 【DM642学习笔记二】dsp基础实验:发光二级管的显示 led.c
1,OSDFPGA配置一个专用的8位寄存器控制指示灯亮灭,访问地址为90080017h,由电路图可知低电平点亮. 2,程序运行时,可直接editmemory.即修改90080017h地址的值(可在Ed ...
- MySQL 报错:Translating SQLException with SQL state '42000', error code '1064', message
MySQL报错详细日志 2019-09-12 16:42:29 [http-nio-80-exec-25] DEBUG [org.springframework.jdbc.support.SQLErr ...
- Leetcode153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中最小值
假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重 ...
- 在vue项目中正确的引入jquery
最近学习vue,习惯性的通过<script>标签引入jquery,写完后报错才想起来,这种方式在vue是不适用的. 1:因为已经安装了vue脚手架,所以需要在webpack中全局引入jqu ...
- 使用代码创建rabbitmq交换机和队列绑定
1.获取channel对象 2.声明(创建)对列 // 第一个参数,queueName:对列名称.数据类型:String// 第二个参数,durable:是否持久化, 队列的声明默认是存放到内存中的, ...