Round Numbers(前导0判断)

时间:2022-09-19 17:53:57

Description

The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets to be milked first. They can't even flip a coin because it's so hard to toss using hooves.

They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,
otherwise the second cow wins.

A positive integer N is said to be a "round number" if the binary representation of N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones, it is not a round number.

Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.

Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤ Start < Finish ≤ 2,000,000,000).

Input

Line 1: Two space-separated integers, respectively Start and Finish.

Output

Line 1: A single integer that is the count of round numbers in the inclusive range Start.. Finish

Sample Input

2 12
Sample Output
6
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>

typedef long long ll;
using namespace std;

int bit[50];//注意是二进制的位数 
ll dp[50][50][50]; 
/*
他这个是从高位向低位,并且他的记录是从低位向高位
这样的话,我们就可以从低位不断的增添,一直到高位,
但是如果像 codeforces上的题那样的话, 我们就不好判断
他的这个位置,这就会很难受 
*/
ll dfs(int num0,int num1,int first,int pos,int limit){
	if(pos==0){
		if(first) return 1;//判断全是0的情况 
		return num0>=num1;
	}
	if(!limit&&!first&&dp[pos][num0][num1]!=-1) return dp[pos][num0][num1];
	int up=limit?bit[pos]:1;
	ll ans=0;
	for(int i=0;i<=up;i++){
		if(first){//考虑前导0 
			if(i==0)
				ans+=dfs(0,0,1,pos-1,limit&&i==up);
			else
				ans+=dfs(0,1,0,pos-1,limit&&i==up);
		}
		else{
			if(i==0)
				ans+=dfs(num0+1,num1,0,pos-1,limit&&i==up);
			else
				ans+=dfs(num0,num1+1,0,pos-1,limit&&i==up);
		}
	}
	if(!limit&&!first) dp[pos][num0][num1]=ans;
	return ans;
}

ll count(ll val){
	int len=1;
	while(val){
		bit[len++]=val&1;
		val>>=1;
	}
	ll ans=dfs(0,0,1,len-1,1);
	return ans;
}

int main(){
	ios::sync_with_stdio(false);
	memset(dp,-1,sizeof(dp));
	ll a,b;
	while(cin>>a>>b){
		ll ans=count(b)-count(a-1);
		cout<<ans<<endl;	
	}
	return 0;
}