## poj3252-Round Number 组合数学

2017年05月14日 算法 ⁄ 共 3785字 ⁄ 字号 评论关闭

Round Numbers
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8492 Accepted: 2963

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`

[2,12]区间的RoundNumbers（简称RN）个数:Rn[2,12]=Rn[0,12]-Rn[0,1]

1、奇数情况：在Len=2k+1的情况下，最高位为1，剩下2k位，至少需要k+1为0

R(len)=C(2k,k+1)+C(2k,k+2)+...+C(2k,2k).

B:C(2k,0)=C(2k,2k), C(2k,1)=C(2k,2k-1) ,,C(2k,i)=C(2k,2k-i)

= C(2k,0)+C(2k,1)+...+C(2k,k)+C(2k,k+1)+C(2k,K+2)+...+C(2k,2k)
= 2*R(len)+C(2k,k)
=2^(2k)

2. 偶数情况 len=2*k，类似可以推到 R(len)=1/2*(2^(2k-1));

。。。

```import java.util.*;

public class Combinatorics_RoundNumbers3252 {

/**
* @param args
*/
public static void main(String[] args) {

Scanner in=new Scanner(System.in);
Init();
while(in.hasNext())
{
int a=in.nextInt();
int b=in.nextInt();
//System.out.println(roundNumber(a-1)+" " +roundNumber(b));
System.out.println(roundNumber(b)-roundNumber(a-1));
}

}

static int c[][]=new int[35][35];
public static void Init(){
for(int i=0;i<33;i++){
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
c[i][j]=c[i-1][j]+c[i-1][j-1];
}

}

public static int roundNumber(int value)
{
char b[]=toBinary(value);
int sum=0;
for(int len=1;len<b.length;len++)
{
for(int j=(len+1)/2;j<len;j++)
sum+=c[len-1][j];
}
int zeros=0;
for(int i=1;i<b.length;i++)
{
if(b[i]=='1')
{
int k=(b.length+1)/2;
int m=Math.max(0, k-(zeros+1));
int n=b.length-i-1;
for(int j=n;j>=m;j--)
sum+=c[n][j];
}
else
{
zeros++;
}
}
if(2*zeros>=b.length)
sum++;
return sum;
}

private static char[] toBinary(int value) {
return Integer.toBinaryString(value).toCharArray();
}

/*public static int roundNumberOfLength(int len)
{
int k=len/2;
if(len%2==0)
{
return (1<<(len-2));
}
else
{
return ((1<<(len-1))-choose(len-1,k))/2;
}
}*/

public static int choose(int n, int m) {

if(n==0)
return 0;
if(m==0||m==n)
return 1;
if(m>n)
return 0;
return choose(n-1,m-1)+choose(n-1,m);
}

}
```