参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6423483.html

B. Code For 1

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

  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 ≤ 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

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和区间(l,r),每次都能把任意数拆成 n/2,n%2,n/2 三个数,直到变成0和1,问区间l,r里有多少个1?

  如 7 2 5

    7 → 3  1  3;

    3 → 1  1  1;

  所以能拆成 7个 1,所以在2--5之间数字1的个数为4。

  同理 10  3  10

    10 → 5  0  5;

      5 → 2  1  2;

      2 → 1  0  1;

  故拆成 → [ 1  0  1      1      1  0  1     0     1  0  1      1      1  0  1  ]

  3--10之间数字1的个数为5.

解法:  分治的思想,二分法

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, l, r, s = , ans;
void solve(ll a, ll b, ll l, ll r, ll d){//二分的思想
if ( a > b || l > r ) return;
else{
ll mid = (a+b)/;
if ( r < mid )solve(a,mid-,l,r,d/);
else if ( mid < l )solve(mid+,b,l,r,d/);
else {
ans += d%;
solve(a,mid-,l,mid-,d/);
solve(mid+,b,mid+,r,d/);
}
}
}
int main(){
cin >> n >> l >> r;
ll p = n;
while ( p >= ){
p /= ;
s = s*+;
}
solve(,s,l,r,n);
cout << ans << endl;
return ;
}

Codeforces 768B B. Code For 1的更多相关文章

  1. 【codeforces 768B】Code For 1

    [题目链接]:http://codeforces.com/contest/768/problem/B [题意] 一开始给你一个数字n; 让你用这个数字n根据一定的规则生成序列; (如果新生成的序列里面 ...

  2. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  3. Codeforces 768B Code For 1

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

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

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

  5. CodeForces - 965E Short Code

    Discription Arkady's code contains nn variables. Each variable has a unique name consisting of lower ...

  6. Codeforces 543A Writing Code

    http://codeforces.com/problemset/problem/543/A 题目大意:n个人,一共要写m行程序,每个程序员每行出现的bug数为ai,要求整个程序出现的bug数不超过b ...

  7. CodeForces 543A - Writing Code DP 完全背包

    有n个程序,这n个程序运作产生m行代码,但是每个程序产生的BUG总和不能超过b, 给出每个程序产生的代码,每行会产生ai个BUG,问在总BUG不超过b的情况下, 我们有几种选择方法思路:看懂了题意之后 ...

  8. Codeforces 965E Short Code 启发式合并 (看题解)

    Short Code 我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg. 正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置. 用 ...

  9. 【codeforces 765B】Code obfuscation

    [题目链接]:http://codeforces.com/contest/765/problem/B [题意] 让你把每个变量都依次替换成a,b,c,-.d这些字母; 且要按顺序先用a再用b-.c.d ...

随机推荐

  1. 朱晔的互联网架构实践心得S1E3:相辅相成的存储五件套

    朱晔的互联网架构实践心得S1E3:相辅相成的存储五件套 [下载本文PDF进行阅读] 这里所说的五件套是指关系型数据库.索引型数据库.时序型数据库.文档型数据库和缓存型数据库. 上图显示了一套读写服务搭 ...

  2. 朱晔的互联网架构实践心得S1E2:屡试不爽的架构三马车

    朱晔的互联网架构实践心得S1E2:屡试不爽的架构三马车 [下载本文PDF进行阅读] 这里所说的三架马车是指微服务.消息队列和定时任务.如下图所示,这里是一个三驾马车共同驱动的一个立体的互联网项目的架构 ...

  3. 跨语言调用Hangfire定时作业服务

    跨语言调用Hangfire定时作业服务 背景 Hangfire允许您以非常简单但可靠的方式执行后台定时任务的工作.内置对任务的可视化操作.非常方便. 但令人遗憾的是普遍都是业务代码和hagnfire服 ...

  4. 最小的N个和(堆)

    描述: 有两个长度为N的序列 AB,从AB中各选一个数,可以得到N^2个和,求这N^2个和中最小的N个 输入 5 1 3 2 4 5 6 3 4 1 7 输出 2 3 4 4 5 分析: 首先限定输出 ...

  5. Day14 Python基础之os/sys/hashlib模块(十二)

    os模块 os.getcwd()  #获取当前工作路径 os.chdir(‘dirname1/dirname2/dirname3’) #改变当前脚本的工作路径,相当于cmd下的cd os.makedi ...

  6. JVM原理分析

    1 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.由一套字节码指令集.一组寄存器.一个栈.一个垃圾回收 ...

  7. java web石家庄铁道大学课程管理系统

    package kecheng Kc.java package kecheng; public class Kc { private int id; private String classname; ...

  8. .net WCF WF4.5 状态机、书签与持久化

    想看源码请直接翻到最后,使用方式如下图 如果同时需要多个书签可以直接在需要的位置创建书签,会认为是同一个实例. 若需要实现的效果是同时需要好几个部门审核,那么只要在对应的位置同时创建多个书签即可. 编 ...

  9. html问题汇总

    1.textarea换行 textarea中无法使用<br/>换行,需要使用\n 2.textarea无法提交 我们知道表单中的元素需要设置name属性才能够提交,但是如果设置了disab ...

  10. StackWalk64

    #include <Windows.h>   #define  PULONG_PTR ULONG** #define  PULONG ULONG* #define  ULONG_PTR U ...