POJ 3252 組合數

Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11512   Accepted: 4325

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

Source



Round Numbers 就是一個表示成二進制的時候0比1多或者相等的正數,注意是正數,所以0就肯定不是了。
 題目是給定一個區間,問在這個區間上的Round Numbers有多少個?
    首先是求出小於等於n的Round Numbers有多少個。
    比如22 =  10110b  如果要求 <=22的Round Numbers,也就是找出1-22有多少個二進制的0不少於1的數的個數。
    22的二進制長度是5.
    首先找長度比5小的Round Numbers(長度比5小的數肯定小於22啦)
    長度爲4的話,第一位必須是1,後面三位的話,可以有2個0,3個0
    所以就是C(3,2)+C(3,3);
    長度爲3的Round Numbers,同理有 C(2,2);//注意不要把第一位1忘記了
    長度爲2的Round Numbers,有  C(1,1)
    長度爲1的Round Numbers,有 0個
    下面是找長度和22相同的Round Numbers。
    首先第一位是1.  
    22的第二位是0,所以第二位不能爲1,必須是0
    第三位爲0的話,(前面有了2個0,1個1),後面兩位可以有1個0,2個0
    C(2,1)+C(2,2)
    接下來把第三位恢復爲1,看第四位。假如第四位是0,(前面有2個0,2個1),後面一位必須是0    C(1,1)
    所以大致求的過程就如上面所述。
    首先先推個公式,就是長度爲len的Round Numbers的個數。
    長度爲len,第一位肯定是1了。
    那麼後面剩下 len-1位。
    如果len-1是偶數。
    那麼  C(len-1,(len-1)/2+1)+C(len-1,(len-1)/2+2)+````C(len-1,len-1)
=   ( 2^(len-1)-C(len-1,(len-1)/2) )/2;
    如果len是奇數
    那麼就是 (  2^(len-1) )/2
    所以上面求比N長度小的Round Numbers是很好求的了。
    至於求長度的,則是逐漸把每一位1變爲0,去求後面的,就可以保證比n小了。


#include<stdio.h>
#include<math.h>
int C[33][33];
void init()
{
    C[0][0]=1;
    C[1][0]=1;
    C[1][1]=1;
    for(int i=2; i<33; i++)
    {
        C[i][0]=1;
        for(int j=1; j<i; j++)
            C[i][j]=C[i-1][j-1]+C[i-1][j];
        C[i][i]=1;
    }
}
int s[50];
int round(int n)///求小於等於n的 Round Numbers
{
    if(n<=1)
        return 0;
    int len=0;
    while(n)
    {
        s[len++]=n%2;
        n/=2;
    }
    int ans=0;
    for(int i=len-1; i>0; i--)
        if((i-1)%2==0)
            ans+=(pow(2,i-1)-C[i-1][(i-1)/2])/2;
        else
            ans+=pow(2,i-1)/2;
    int cns0=0,cns1=0;
    for(int i=0; i<len; i++)
    {
        if(s[i]==0)
            cns0++;
        else
            cns1++;
    }
    if(cns0>=cns1) ///n本身是 Round Number
        ans++;
    cns0=0;
    cns1=1;
    for(int i=len-2; i>=0; i--)
    {
        if(s[i]==1) ///後面有i位,第i位當成0
        {
            for(int j=i; j>=0&&j+cns0+1>=cns1+i-j; j--)
                ans+=C[i][j];
            cns1++;
        }
        else
            cns0++;
    }
    return ans;
}
int main()
{
    init();
    int a,b;
    while(~scanf("%d%d",&a,&b))
    {
        printf("%d\n",round(b)-round(a-1));
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章