Good Bye 2015 B. New Year and Old Property 计数问题
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 and1010 = 10102. Two of them (1012 and 1102) have the described property.
题意: 算出 a,b 中 二进制下,含有 且 只含有1个0的数 的数目
题解: 对于 10110010 枚举每一位为0所带来的答案就好了
或者你可以数位dp
//meek #include<bits/stdc++.h> #include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include<map> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define pb push_back #define fi first #define se second #define MP make_pair const int N=400+100; const ll INF = 1ll<<61; const int inf = 1000000007; const int MOD= 1000007; int d[N]; int sum[N]; ll solve(ll n) { if(n<0) return 0; ll tmp = n; int len = 0; mem(sum); while(tmp) d[++len]= tmp%2,tmp/=2; for(int i=len;i>=1;i--) sum[i] = sum[i+1] +d[i]; ll ans = 0; int f= 0; for(int i=1;i<=len;i++) { if((!f||d[i])&&sum[i+1]==len-i) { ans+=(len-i);//cout<<ans<<endl; } else ans+=(len-i==0?0:(len-i-1)); if(d[i] == 0)f=1; // cout<<ans<<endl; } return ans; } int main() { ll a,b; scanf("%lld%lld",&a,&b); printf("%lld\n",solve(b)-solve(a-1)); // printf("%lld\n",); return 0; }