【题目大意】:让你找两个数之间有多少个round number。round number的定义是:把一个数化成2进制,如果0的个数>=1的个数。那么该数为round number。
【解题思路】:
先把输入的数化成2进制,然后得到长度len,那么很容易求得长度比其小的round number的数目。然后再对长度len来说,找每一位比它小的round number的数目。
不妨设count(n,k)为2进制n位时需要有k个0时的round number数。b数组记录该数的2进制的每一位。那么有递归公式:
count(n,k)=count(n-1,k-1)(if b[n-1]==0).
count(n,k)=c(n-2,k-1)+count(n-1,k).(ifb[n-1]==1,)
【代码】:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
using namespace std;
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long
int c[50][50];
int cc[50];
int b[50];
int to_binary(int t){
int i=1;
while(t){
b[i++]=t%2;
t/=2;
}
return i-1;
}
int count(int n,int k){
if(k>=n||k<0) return 0;
if((n==1&&k==0)||(n==2&&k==1)) return 1;
if(b[n-1]==0) return count(n-1,k-1);
else return c[n-2][k-1]+count(n-1,k);
}
int solve(int n){
int ans=0;
int len=to_binary(n);
for (int i=(len+1)/2; i<len; i++)
ans+=count(len,i);
for (int i=1; i<len; i++)
ans+=cc[i];
return ans;
}
void init(){
c[1][0]=c[1][1]=1;
for (int i=2; i<=32; i++){
c[i][0]=1;
for (int j=1; j<=i; j++)
c[i][j]=c[i-1][j]+c[i-1][j-1];
}
cc[1]=0;
for (int i=2; i<=32; i++){
for (int j=(i+1)/2; j<i; j++)
cc[i]+=c[i-1][j];
}
return ;
}
int main(){
init();
int st,en;
while (~scanf("%d%d",&st,&en)){
printf("%d\n",solve(en)-solve(st-1));
}
return 0;
}