观察一下,将整个过程写出来,会发现形成一棵满二叉树,每一层要么全是0,要么全是1。

输出的顺序是其中序遍历。

每一层的序号形成等差数列,就计算一下就可以出来每一层覆盖到的区间的左右端点。

复杂度O(log(n))。

#include<cstdio>
using namespace std;
typedef long long ll;
ll n,l,r;
bool a[66];
int e;
int main()
{
// freopen("b.in","r",stdin);
scanf("%I64d%I64d%I64d",&n,&l,&r);
if(n==0)
{
puts("0");
return 0;
}
while(n)
{
a[++e]=n%2ll;
n/=2ll;
}
ll j=1;
int ans=0;
for(int i=e;i;--i)
{
if(a[i])
{
ll L=(l-j)/(j*2)+((l-j)%(j*2)!=0);
if(l<j) L=0ll;
ll R=(r-j)/(j*2);
if(r<j) R=-1ll;
ans+=(int)(R-L+1ll);
}
j*=2ll;
}
printf("%d\n",ans);
return 0;
}

【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1的更多相关文章

  1. 【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones

    打表找规律即可. 1,1,2,2,2,3,3,3,3,4,4,4,4,4... 注意打表的时候,sg值不只与剩下的石子数有关,也和之前取走的方案有关. //#include<cstdio> ...

  2. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1

    地址:http://codeforces.com/contest/768/problem/B 题目: B. Code For 1 time limit per test 2 seconds memor ...

  3. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C - Jon Snow and his Favourite Number

    地址:http://codeforces.com/contest/768/problem/C 题目: C. Jon Snow and his Favourite Number time limit p ...

  4. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs

    地址:http://codeforces.com/contest/768/problem/D 题目: D. Jon and Orbs time limit per test 2 seconds mem ...

  5. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A. Oath of the Night's Watch

    地址:http://codeforces.com/problemset/problem/768/A 题目: A. Oath of the Night's Watch time limit per te ...

  6. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)

    C题卡了好久,A掉C题之后看到自己已经排在好后面说实话有点绝望,最后又过了两题,总算稳住了. AC:ABCDE Rank:191 Rating:2156+37->2193 A.Oath of t ...

  7. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A B 水 搜索

    A. Oath of the Night's Watch time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. 【概率dp】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs

    直接暴力dp就行……f(i,j)表示前i天集齐j种类的可能性.不超过10000天就能满足要求. #include<cstdio> using namespace std; #define ...

  9. 【基数排序】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C. Jon Snow and his Favourite Number

    发现值域很小,而且怎么异或都不会超过1023……然后可以使用类似基数排序的思想,每次扫一遍就行了. 复杂度O(k*1024). #include<cstdio> #include<c ...

随机推荐

  1. Angular白名单&&Angular拦截器 全局通用

    //angular 白名单全局通用 app.config([ '$compileProvider', function ($compileProvider) { $compileProvider.aH ...

  2. Swing学习篇 API [一]Swing常用组件

    1.按钮(Jbutton) Swing中的按钮是Jbutton,它是javax.swing.AbstracButton类的子类,swing中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且 ...

  3. bzoj 2525 [Poi2011]Dynamite 二分+树形dp

    [Poi2011]Dynamite Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 270  Solved: 138[Submit][Status][D ...

  4. WebOS系列-了解Wekbit【邓侃】

    注:[转载请注明文章来源.保持原样] 出处:http://www.cnblogs.com/jyli/archive/2010/02/02/1660634.html  作者:李嘉昱 这是Kan老大的We ...

  5. php getimagesize()函数获取图片宽度高度

    //php自带函数 getimagesize() $img_info = getimagesize('tomener.jpg'); echo '<pre>'; print_r($img_i ...

  6. input输入浮动提示

    html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. MyBatis对象关联关系----多对多的保存与查询

    模拟情景: 对象:学生,课程 关系:一个学生可选多个课程,一门课程可被多个学生选择 一.保存 1.创建数据库表,student,course,student_course,其中student_cour ...

  8. SHOI 2007 仙人掌图(BZOJ 1023)

    1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2564  Solved: 1062 Descrip ...

  9. python 读 excel 模块: xlrd

    主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python ...

  10. 使用SpringMVC解决Ajax跨域问题

    package com.mengyao.system.filter; import java.io.IOException; import javax.servlet.FilterChain; imp ...