POJ - 3252

Round Numbers


Time Limit:                                                        2000MS                       

 

Memory Limit: 65536KB

 

64bit IO Format:                            %I64d & %I64u                       


SubmitStatus


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


Hint



Source


USACO 2006 November Silver



//题意:说明:round number数为,它的二进制数中的0的个数大于等于1的个数。



输入两个数n,m,表示一个区间[n,m],问在这个区间里面有几个round number的数



//思路:



要计算小于等于n的round number得分两部分:



假设经计算的n的二进制数的长度为len :



1、比n的二进制位的长度小的数(l<len);



因为二进制表示的数的第一位都是1,所以除了第一位数是确定的以外,其他的都是不确定的,要让0的个数>=1的个数,那么长度为  l  的数的符合条件的数的个数为C(l-1,(l-1)/2+1)+C(l-1,(l-1)/2+2)+......+C(l-1,l-1);



2、与n的二进制数的长度相同且比n小的符合条件的数的个数为:



这个得逐个位进行判断,第一位肯定为1,所以从第二位开始往后遍历,如果第i位上是1的话,那么将它变为0,后面的len-i位数就和情况1的一样了。



具体看代码:



#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int C[35][35];
void init()      //打表C(i,j) 
{
	int i,j;
	for(i=0;i<33;i++)
	{
		for(j=0;j<=i;j++)
		{
			if(!j||i==j)
				C[i][j]=1;
			else
				C[i][j]=C[i-1][j-1]+C[i-1][j];
		}
	}
}
int bit[35];
int len;
void d(int n)   //转换为二进制 
{
	len=0;
	while(n)
	{
		bit[++len]=n%2;
		n>>=1;
	}
}
int solve(int n)   //计算小于n的符合题意的个数 
{
	d(n);
	int sum=0,i,j;
	for(i=1;i<len-1;i++)   //计算长度小于n的二进制数长度的符合条件的个数 
	{
		for(j=i/2+1;j<=i;j++)
		{
			sum+=C[i][j];
		}
	}
	int cnt0=0;
	for(i=len-1;i>=1;i--)   //计算长度等于n的二进制数长度的符合条件的个数 
	{
		if(bit[i])
		{
			for(j=(len+1)/2-(cnt0+1);j<=i-1;j++)
			{
				sum+=C[i-1][j];
			}
		}
		else
			cnt0++;
	}
	return sum;
}
int main()
{
	init();
	int n,m;
	scanf("%d%d",&n,&m);
	printf("%d\n",solve(m+1)-solve(n));
	return 0;
}