CF Good Bye 2015 B- New Year and Old Property(CF611B)
来源:互联网 发布:sql 新建存储过程 编辑:程序博客网 时间:2024/05/15 02:36
The year 2015 is almost over.
Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.
Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?
Assume that all positive integers are always written without leading zeros.
The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.
Print one integer – the number of years Limak will count in his chosen interval.
5 10
2
2015 2015
1
100 105
0
72057594000000000 72057595000000000
26
In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and 1010 = 10102. Two of them (1012 and 1102) have the described property.
题意:给你两个数a,b,让你算出来从a到b一共b-a+1个数中一共有几个数的二进制中只有一个0(比如5的二进制101,6的二进制110都是)。
思路:一开始想的比较复杂想要找到最小的大于等于a的二进制只有一个零的数,然后不停的将0后移,如果0到了最后一个则进位,直到大于b为止,后来看了别人的代码,算了算时间复杂度,发现直接把所有的二进制只有一位零的数遍历一遍也不会超时间...
我的代码:
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<queue>#include<math.h>using namespace std;unsigned long long INF=0x8000000000000000;//-9223372036854775808void pr(long long n){long long i,k,x[100];for(i=0;n!=0;i++)// 判断条件 n!=0{x[i]=n%2;n=n/2;}for(k=i-1;k!=(-1);k--)// 判断条件 k!=-1printf("%d",x[k]);// 输出x[k],不是x[i]}int main(){ long long a,b,temp; int ans=0; cin>>a>>b; int n=0,l,s; temp=a; while(1) { if(temp&INF) { s=n; break; } temp=temp<<1; n++; } temp=temp<<1; n++; while(1 && n<=64) { if(!(temp&INF)) { l=n; break; } temp=temp<<1; n++; } if(n<63) { temp=temp<<1; n++; while(n<64) { if(!(temp&INF)) { a+=(1LL<<(63-n)); } temp=temp<<1; n++; } } else if(n==64) { a+=(1LL<<(64-s)); a-=(1LL<<(63-s)); l=s; s--; } while(1) { if(a<=b) { ans++; } else if(a>b) break; if(l<63) { a+=(1LL<<(63-l)); l++; a-=(1LL<<(63-l)); } else if(l<=63) { a++; a+=(1LL<<(64-s)); a-=(1LL<<(63-s)); l=s; s--; } } cout<<ans<<endl; return 0;}
别人的代码:
#include <iostream>#include <cstdio>#include <queue>#include <stack>#include <map>using namespace std;long long a,b,rs,x;int main(){ cin >> a >> b; for(int i = 1; i < 63; i++) for(int j = 0; j < i-1; j++) { x = ((1LL << i) - 1 - (1LL << j)); if(a <= x && x <=b ) rs++; } cout<<rs; return 0;}
- CF Good Bye 2015 B- New Year and Old Property(CF611B)
- Codeforces Good Bye 2015 B. New Year and Old Property (DFS)
- Good Bye 2015-New Year and Old Property(二进制暴力枚举)
- CF Good Bye 2015 A- New Year and Days(CF611A)
- CF Good Bye 2015 C- New Year and Domino(CF611C)
- cf#gb2015-B - New Year and Old Property-暴力
- CF~Good Bye 2014 B. New Year Permutation
- B. New Year and Old Property
- Codeforences Goodbye 2015 B New Year and Old Property
- CF Good Bye 2015 C. New Year and Domino && D. New Year and Ancient Prophecy (DP)
- CodeForces Good Bye 2016 :C New Year and Rating、D New Year and Fireworks、E New Year and Old Subsequ
- codeforces Good Bye 2016-B New Year and North Pole
- CF Good Bye 2015 E. New Year and Three Musketeers(贪心+枚举)
- CF Good Bye 2015 F. New Year and Cleaning(思维)
- Good Bye 2013---B. New Year Present
- Good Bye 2014--B. New Year Permutation
- Good Bye 2014 B. New Year Permutation
- Codeforces Good Bye 2015 C. New Year and Domino (预处理)
- 第二章:CallsManager调用 startOutgoingCall开始拨号流程之二InCallController.java类进行framework层service绑定
- Caffe学习系列(4):激活层(Activiation Layers)及参数
- java Set 用法
- python中的条件选择语句和循环语句
- 第二章:CallsManager调用 startOutgoingCall开始拨号流程之三InCallService完成framework层每路通话转换
- CF Good Bye 2015 B- New Year and Old Property(CF611B)
- Caffe学习系列(5):其它常用层及参数
- host name may not be null
- 关闭Sublime更新提示
- python中函数的使用
- 解析XML数据
- 为革命保护视力 — 给 Visual Studio 换颜色
- 递归与尾递归 (C语言)
- HTTP协议入门——1.1版本