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 ...
随机推荐
- Hibernate 查询语言
查询语言 Hibernate 查询语言(HQL)是一种面向对象的查询语言,类似于 SQL,但不是去对表和列进行操作,而是面向对象和它们的属性. HQL 查询被 Hibernate 翻译为传统的 SQL ...
- 转:shell笔试题
源地址:http://blog.csdn.net/zcsylj/article/details/6799632 1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录 ...
- SpringBoot 05_集成SpringDataJpa
你还在为项目的集成头疼吗?你还在为管理大量的配置文件烦恼吗?如果是,用SpringBoot吧!今天主要介绍如果在SpringBoot的基础上创建一个集成了SpringDataJpa的项目,至于Spri ...
- Tornado demo3 - tcpecho分析
在这个demo中,主要是使用了Tornado中异步的TCP client和server来实现一个简单的echo效果(即客户端发送的message会从server端返回到client).代码的githu ...
- [转]C#线程同步(1)- 临界区&Lock
第一印象,C#关于线程同步的东西好多,保持了C#一贯的大杂烩和四不象风格(Java/Delphi).临界区跟Java差不多只不过关键字用lock替代了synchronized,然后又用Moniter的 ...
- Odoo中Qweb使用入门
参考 可参考官网例子https://doc.odoo.com/trunk/web/qweb/或 http://thierry-godin.developpez.com/openerp/tutorial ...
- JZOJ2368 【SDOI2011】黑白棋
题目 题目大意 在一个1*n的棋盘上,有黑棋和白棋交错分布,每次,一个人可以移动自己的ddd颗旗子. 问先手必胜的方案数. 思考历程 在一开始,我就有点要放弃的念头. 因为这题是一道博弈问题. 我是非 ...
- 469 Same Tree
原题网址:https://www.lintcode.com/problem/same-tree/description 描述 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构, ...
- require模块开发(一)
1.require下载和加载 1.1 下载 工欲善其事必先利其器,先下载require.js下载地址, 然后添加 require.js 到 scripts 目录 1.2 加载 然后加载require ...
- webServices学习一(了解基础和作用。)
一.第一部分 1. 带着几个问题学习: l 什么是WebService? l 它能做什么? l 为什么要学习WebService? l 学习WebService ...