【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1
2017-02-21 12:59
453 查看
观察一下,将整个过程写出来,会发现形成一棵满二叉树,每一层要么全是0,要么全是1。
输出的顺序是其中序遍历。
每一层的序号形成等差数列,就计算一下就可以出来每一层覆盖到的区间的左右端点。
复杂度O(log(n))。
输出的顺序是其中序遍历。
每一层的序号形成等差数列,就计算一下就可以出来每一层覆盖到的区间的左右端点。
复杂度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(DFS好题)
- 【Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B】 Code For 1
- 【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A B 水 搜索
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A(贪心)
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) F
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C - Jon Snow and his Favourite Number
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined)
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A. Oath of the Night's Watch
- 【基数排序】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) C. Jon Snow and his Favourite Number
- 【概率dp】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs
- Editorial Divide by Zero and Codeforces Round #399 (Div. 1+2, combined) (A~F)
- topcoder SRM 610 DIV2 DivideByZero
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) 解题报告
- ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)