Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility.

Initially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position  sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.

Now the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?

Input

The first line contains three integers nlr (0 ≤ n < 250, 0 ≤ r - l ≤ 105r ≥ 1, l ≥ 1) – initial element and the range l to r.

It is guaranteed that r is not greater than the length of the final list.

Output

Output the total number of 1s in the range l to r in the final sequence.

Examples

Input
7 2 5
Output
4
Input
10 3 10
Output
5

Note

Consider first example:

Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.

For the second example:

Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5.

题意:一开始有一个数N,每次把所有大于1的数变为x/2,x%2,x/2。知道不能操作。样例如题。

思路:找规律我们得知,最后的01串有很多的对称性。首先推出最后一层有num=2^(lg2(N)+1) -1个数,并且以x=(num+1)/2为对称轴,所以如果在对称轴的右边,我们可以把它对称到x轴的左边。   然后把对称轴左边的区间[1,x]又看成一个整体,它又以x2=(x+1)/2为对称轴,如果在x2右边,又把它对称到x2左边.....一直对称下去,直到把它对称到一个对称轴上。而我们可以求出对称轴上对应的数的值,就是N的二进制对应的数。

比如10的二进制表示为10(ten)=1010(two)。10=1(1)+0(2)+1(4)+0(8),(括号里的是最后一层的对称轴位置,也是最后一层二进制对应位置的结果)。

(ps:也可以用分形来做。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=;
ll find(ll x){
ll y=log2(x);
if(1LL<<y==x) return y;
return find((1LL<<(y+))-x);
}
int main()
{
ll N,L,R,ans=,Bit;
cin>>N>>L>>R;
Bit=log2(N);
for(ll i=L;i<=R;i++)
ans+=(N>>(Bit-find(i)))&1LL;
cout<<ans<<endl;
return ;
}

CodeForces768B:Code For 1 (分治)的更多相关文章

  1. Codeforces768B Code For 1 2017-02-21 22:17 95人阅读 评论(0) 收藏

    B. Code For 1 time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. code for 1 - 分治

    2017-08-02 17:23:14 writer:pprp 题意:将n分解为n/2, n%2, n/2三部分,再将n/2分解..得到一个序列只有0和1,给出[l, r]问l到r有几个1 题解:分治 ...

  3. B. Code For 1 分治

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. BZOJ 2244: [SDOI2011]拦截导弹 (CDQ分治 三维偏序 DP)

    题意 略- 分析 就是求最长不上升子序列,坐标取一下反就是求最长不下降子序列,比较大小是二维(h,v)(h,v)(h,v)的比较.我们不看概率,先看第一问怎么求最长不降子序列.设f[i]f[i]f[i ...

  5. Codeforces 768B - Code For 1(分治思想)

    768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  6. Code Chef TSUM2(动态凸包+点分治)

    题面 传送门 题解 真是毒瘤随机化算法居然一分都不给 首先这种树上的题目一般想到的都是点分 我们考虑如何统计经过当前点的路径的贡献,设当前点\(u\)在序列中是第\(c\)个,那么一条路径的贡献就是 ...

  7. HDU5618 & CDQ分治

    Description: 三维数点 Solution: 第一道cdq分治...感觉还是很显然的虽然题目不能再傻逼了... Code: /*=============================== ...

  8. 【Codeforces715C&716E】Digit Tree 数学 + 点分治

    C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...

  9. 【BZOJ-4456】旅行者 分治 + 最短路

    4456: [Zjoi2016]旅行者 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 254  Solved: 162[Submit][Status] ...

随机推荐

  1. awk如何区分shell脚本传进来的参数和自身的参数?awk如何获取shell脚本传进来的参数;awk中如何执行shell命令

    问题:对于shell脚本,$0表示脚本本身,$1表示脚本的第一个参数,$2……依次类推:对于awk,$1表示分割后的第一个字段,$2……依次类推.那么对于shell脚本中的awk如何区分两者呢? 答案 ...

  2. CMake使用hellocmake&&make的使用

    2016-12-11   20:38:32 已经知道cmake这个东西很长的时间了,一直没有试验过,知道它是一个编译工具,在opencv和Linux下都有makefile的内容.感觉现在对源码的编译有 ...

  3. Opencv 图片边缘检测和最小外接矩形

    #include "core/core.hpp" #include "highgui/highgui.hpp" #include "imgproc/i ...

  4. 【IE】IE对line-height 失效的的解决方案

    微软的IE9 + Extjs3.1 确实头疼.在使用了line-height:20px 的Tree的样式,可是一直没有生效, 以下给出3中解决方式: 方案1.加padding-top: <div ...

  5. vue2.0 自定义过滤器

    2.0中已经废弃了过滤器,需要我们自定义 <div id="app"> {{message|uppercase}} </div> //过滤器 Vue.fil ...

  6. HDU1532_Drainage Ditches(网络流/EK模板/Dinic模板(邻接矩阵/前向星))

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 综合运用: C++11 多线程下生产者消费者模型详解(转)

    生产者消费者问题是多线程并发中一个非常经典的问题,相信学过操作系统课程的同学都清楚这个问题的根源.本文将就四种情况分析并介绍生产者和消费者问题,它们分别是:单生产者-单消费者模型,单生产者-多消费者模 ...

  8. Jememeter和Loadrunner测试MySQL性能

    From:http://blog.csdn.net/testingstar/article/details/60579454 MySQL数据库性能测试的方法 前置条件: 安装系统:windows 7 ...

  9. Python+Selenium框架unittest执行脚本方法之discover()方法

    继续接着介绍,如何利用unittest管理和执行测试用例的问题,这里我们还是利用之前已经有的三条测试用例,如果你跳过了前面文章,请回到框架设计篇的第八篇和第七篇,里面有相关测试类的文件.本文来介绍,如 ...

  10. systemd、upstart和system V

    http://blog.csdn.net/kumu_linux/article/details/7653802  systemd是Linux下的一种init软件,由Lennart Poettering ...