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. POJ3114 有些图缩点/改图/最短路

    没想到手感还在~ 不须要又一次建图.仅仅要依据条件改改权值就可以. 还跑k次SPFA~ #include<cstdio> #include<iostream> #include ...

  2. Java中字符串转为16进制表示

    Java中字符串转为16进制表示 String str = "鲸"; char[] chars = "0123456789ABCDEF".toCharArray ...

  3. 【翻译自mos文章】当并行事务恢复进程在执行时,禁用并行事务恢复的方法

    当并行事务恢复进程在执行时,禁用并行事务恢复的方法 How to Disable Parallel Transaction Recovery When Parallel Txn Recovery is ...

  4. sql的一些知识_高级

    1.视图 http://www.cnblogs.com/wang666/p/7885934.html 2.存储过程 http://www.cnblogs.com/wang666/p/7920748.h ...

  5. sql的一些知识_数据分组

    group by--------按**分组查询 SELECT rank, MIN(weight)as min_weight FROM userinfo GROUP BY rank 对分组的值进行过滤需 ...

  6. 几种常用的listenner

    1.ServletContextListener:监控web容器的启动和关闭 2.HttpSessionListener:监控bs结构中b的session创建和session销毁 3.HttpSess ...

  7. IOS_DatePicker_PickerView_SegmentControl_键盘处理

    H:/0712/01_UIController_MJViewController.m // MJViewController.m // 01-总结复习 // Created by apple on 1 ...

  8. NoSQL数据库-MongoDB和Redis

    http://blog.csdn.net/tea_wu/article/details/19050277 http://www.uml.org.cn/sjjm/201212205.asp

  9. Spring MVC访问静态资源

    http://www.cnblogs.com/yank/p/4477204.html SpringMVC访问静态资源 在SpringMVC中常用的就是Controller与View.但是我们常常会需要 ...

  10. mysql 海量数据删除

    百度知道 - mysql删除海量数据   MySQL 数据库删除大批量数据的优化     看到这儿的话,最后看下这篇文章,对于操作海量数据的sql深入分析 cnblogs - 深度分析DROP,TRU ...