观察一下,将整个过程写出来,会发现形成一棵满二叉树,每一层要么全是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. android OTA升级包制作

    0.签名 java -Xmx2048m -jar out/host/linux-x86/framework/signapk.jar -w build/target/product/security/t ...

  2. POJ2236:Wireless Network(并查集)

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 39772   Accepted: 164 ...

  3. 【CF1023E】Down or Right(交互,贪心)

    题意: n<=500 思路:From https://blog.csdn.net/csdnjiangshan/article/details/81813227 #include<cstdi ...

  4. bzoj 3196二逼平衡树 线段树套平衡树

    比较裸的树套树,对于区间K值bz上有一道裸题,详见题解http://www.cnblogs.com/BLADEVIL/p/3455336.html(其实题解也不是很详细) //By BLADEVIL ...

  5. jquery with ajax

    with session storage: 1.ajax请求可以放在 $(document).ready(function (){...}); 里. 2. $.ajax({ url: "/a ...

  6. HDU1010(dfs+剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. UVA 10471 Gift Exchanging

    题意:就5种盒子,给出每个盒子个数,盒子总数,每个人选择这个盒子的概率.求这个人选择哪个盒子取得第一个朋友的概率最大,最大多少 dp[N][sta]表示当前第N个人面临状态sta(选择盒子的状态可以用 ...

  8. phpstudy2014没有mysqldumpslow.pl及其在性能优化的作用

    mysqldumpslow.pl的作用是监控mysql的性能瓶颈的 1)在phpstudy2014中没有这个mysqldumpslow.pl所以需要去下载一个然后放置于mysql/bin的文件夹中,由 ...

  9. 【 Linux】脚本导入格式

    在从windows文本(*.txt)格式导入到Linux中时,需要注意. 如果是直接将*.txt 导入到Linux系统,然后重命名使用会有问题,建议在linux系统中创建文件,然后直接复制内容到lin ...

  10. selenium 下拉框处理

    web应用中有很多时候我们会遇见<select></select>标签的下列列表框,一般是无法直接去操作下列列表中的选择的.selenium webdriver 提供了专门操作 ...