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] ...
随机推荐
- JavaScript中的Math方法演示
<html> <head> <script type="text/javascript"> var num = 12.4; alert(Math ...
- UVA - 1416 Warfare And Logistics (最短路)
Description The army of United Nations launched a new wave of air strikes on terroristforces. The ob ...
- 我的Android进阶之旅------>解决:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
错误描写叙述 今天在Android Studio项目中添加了jackson的开发包,编译执行时候.引发了例如以下的错误: Error:Execution failed for task ':app:t ...
- rf-demos (request)
*** Settings *** Library RequestsLibrary Library Collections Library XML *** Test Cases *** case1 Cr ...
- ARM体系结构与编程-5
GET通经常使用于包括定义常量的源文件. 比如:GET 2440addr.inc 用AREA定义一个段.ENTRY用于指定程序的入口点,END用于告诉汇编器源文件已经结束. 比如: AREA init ...
- node JS 微信开发
JS-SDK 要点 微信测试号; 扫码登录;无需认证(只是名称统一为微信测试号)http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/logi ...
- 动态控制body最小高度
//动态控制body最小高度 var windowHeight = $(document).height() - 164; $(".body-content").css({ &qu ...
- Selenium学习(二)入门小例子
1)打开百度页面 2)输入“hello” 3)点击百度一下按钮 from selenium import webdriver url = "http://" + "www ...
- 01 json方式封装通信接口
新建一个json_api.php<?php class Response{ /** *按json方式输出通信 *@param integet $code 状态码 *@param string $ ...
- 目标检测之积分图---integral image 积分图2
前面在图像处理一栏中涉及到boxfilter 的时候,简单介绍过积分图,就是每个像素点是左边和上边的累加和,这样的话可以方便均值和方差,以及直方图统计的相关运算,这里再次结合网络资源重新单独对积分图做 ...