CodeForces768B:Code For 1 (分治)
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 n, l, r (0 ≤ n < 250, 0 ≤ r - l ≤ 105, r ≥ 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
7 2 5
4
10 3 10
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 (分治)的更多相关文章
- 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 ...
- 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 题解:分治 ...
- B. Code For 1 分治
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- BZOJ 2244: [SDOI2011]拦截导弹 (CDQ分治 三维偏序 DP)
题意 略- 分析 就是求最长不上升子序列,坐标取一下反就是求最长不下降子序列,比较大小是二维(h,v)(h,v)(h,v)的比较.我们不看概率,先看第一问怎么求最长不降子序列.设f[i]f[i]f[i ...
- Codeforces 768B - Code For 1(分治思想)
768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...
- Code Chef TSUM2(动态凸包+点分治)
题面 传送门 题解 真是毒瘤随机化算法居然一分都不给 首先这种树上的题目一般想到的都是点分 我们考虑如何统计经过当前点的路径的贡献,设当前点\(u\)在序列中是第\(c\)个,那么一条路径的贡献就是 ...
- HDU5618 & CDQ分治
Description: 三维数点 Solution: 第一道cdq分治...感觉还是很显然的虽然题目不能再傻逼了... Code: /*=============================== ...
- 【Codeforces715C&716E】Digit Tree 数学 + 点分治
C. Digit Tree time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ...
- 【BZOJ-4456】旅行者 分治 + 最短路
4456: [Zjoi2016]旅行者 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 254 Solved: 162[Submit][Status] ...
随机推荐
- iOS开发 NSArray技巧
sh数组快速倒序 NSArray *arr = @[@"first",@"second"]; arr = arr.reverseObjectEnumerator ...
- vue Syntax Error: Unexpected token {
> music@1.0.0 dev F:\music\music> node build/dev-server.js > Starting dev server...ERROR Fa ...
- android与服务器交互总结(json,post,xUtils,Volley)
http://www.23code.com/tu-biao-chart/ 从无到有,从来没有接触过Json,以及与服务器的交互.然后慢慢的熟悉,了解了一点.把我学到的东西简单的做个总结,也做个记录,万 ...
- 【Hibernate步步为营】--核心对象+持久对象全析(三)
上篇文章讨论了Hibernate持久对象的生命周期,在整个生命周期中一个对象会经历三个状态,三种状态的转换过程在开发过程中是可控的.并且是通过用法来控制它们的转化过程.详细的转化过程今天就来着重讨论下 ...
- C++学习总结2
链接上一篇日志,下面介绍下C++里面的其他内容 补充上一届里面的异常处理代码: try { cout << "try num" << endl; throw ...
- hdu5258简单枚举
百度之星复赛第一题.不明白这么水的题为何一堆人没过...这些人是咋晋级复赛的呢... /* * Author : ben */ #include <cstdio> #include < ...
- EasyRTMP实现的一套简单、高效、易用的全平台(Windows/Linux/ARM/Android/iOS)RTMP直播推送库
本文转自EasyDarwin开源团队成员Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52938980 EasyRTMP介绍 Easy ...
- ZOJ 3551 Bloodsucker <概率DP>
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3551 题意:开始有N-1个人和一个吸血鬼, 每天有两个生物见面,当人 ...
- 微信分享配置(js-sdk)
现在的微信分享给朋友-分享到朋友圈 链接带有自定义的title.描述.图片,需要配置js-sdk(地址:mp.weixin.qq.com)微信文档 需要后台配置config的参数,返回给前台 1)de ...
- Ahead-of-time compilation
https://en.wikipedia.org/wiki/Ahead-of-time_compilation