http://codeforces.com/contest/768/problem/B

我的做法是,观察到,只有是x % 2的情况下,才有可能出现0

其他的,都是1来的,所以开始的ans应该是R - L + 1

那么现在就是要看那些是x % 2的,然后放在的位置是属于【L, R】的,有多少个0,减去就行。

一开始,总长度是可以算出来的,然后就能算出一开始的n % 2的位置是那个,就是mid了,然后根据L和R选择递归

左边还是右边,然后我发现类似于线段树。。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e6 + 20;
LL n, L, R;
LL calc(LL val) {
if (val > 1) {
return 2 * calc(val / 2);
}
return 1;
}
LL ans;
void dfs(LL cur, LL L, LL R, LL be, LL en) {
if (cur == 0) {
return;
}
if (be == en) {
return;
}
LL mid = (be + en) >> 1;
if (L <= mid && mid <= R && cur % 2 != 1) {
ans--;
}
if (L > mid) {
dfs(cur / 2, L, R, mid + 1, en);
} else if (R <= mid) {
dfs(cur / 2, L, R, be, mid);
} else {
dfs(cur / 2, L, R, be, mid);
dfs(cur / 2, L, R, mid + 1, en);
}
}
void work() {
cin >> n >> L >> R;
if (n == 0) {
cout << 0 << endl;
return;
}
ans = R - L + 1;
dfs(n, L, R, 1, 2 * calc(n) - 1);
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return 0;
}

  

最新文章

  1. tecplot 脚本文件
  2. ajax 对象创建 兼容各个浏览器
  3. DNS初识
  4. CSS引入外部字体
  5. Objective-C运行时编程 - 实现自动化description方法的思路及代码示例
  6. JDBC连接MySQL数据库及演示样例
  7. Java 实现大整数加减乘除
  8. WebGIS中使用ZRender实现前端动态播放轨迹特效的方案
  9. 我眼中的ASP.NET Core之微服务 (二)
  10. Win 10 下 android studio显示 Intel haxm无法安装,以及VT-X和hyper-x的冲突问题
  11. 深入研究 Mini ASP.NET Core(迷你 ASP.NET Core),看看 ASP.NET Core 内部到底是如何运行的
  12. oi回忆录
  13. SpringBoot学习(三)--&gt;Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池
  14. Laravel-Excel 导入 Excel 文件----为什么只获取到最后一行数据?
  15. [转]zookeeper集群 initLimit和syncLimit
  16. 【二十二】mysqli事务处理与预处理总结
  17. Vigenere加密
  18. bzoj千题计划188:bzoj1923: [Sdoi2010]外星千足虫 (高斯—若尔当消元法解异或方程组)
  19. 使用Phantom omni力反馈设备控制机器人
  20. IOS-如何优雅地拦截按钮事件(判断是否需要登录)

热门文章

  1. 使用 fetch 代替 ajax(在不支持的浏览器上使用 XHR); This kind of functionality was previously achieved using XMLHttpRequest.
  2. mysql 数据库导入错误:40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET
  3. C# partial分部类和分部方法
  4. DeepLearningFlappyBird-深度学习玩游戏-1-环境搭建
  5. Mysql的Merge存储引擎实现分表查询
  6. TTL以及LVDS接口传输【转】
  7. HDU3667 Transportation —— 最小费用流(费用与流量平方成正比)
  8. HNOI2008 越狱 (组合数学)
  9. Myeclipse----Hibernate环境搭建
  10. eclipse恢复界面默认设置