POJ 3252 Round Numbers(數學問題)


Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6051   Accepted: 2050

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<iostream>
using namespace std;

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 bits[33];
int calc(int n)//求小於等於n的 Round Numbers
{
    if(n<=1)return 0;//這個條件必須加
    int len=0;
    while(n>0)
    {
        if(n&1)bits[len++]=1;
        else bits[len++]=0;
        n>>=1;
    }
    int ans=0;
    for(int i=len-1;i>0;i--)
    {
        if(i%2==0)ans+=((1<<(i-1)))/2;
        else  ans+=((1<<(i-1))-C[i-1][(i-1)/2])/2;
    }
    int cnt0=0,cnt1=0;
    for(int i=0;i<len;i++)
    {
        if(bits[i]==0)cnt0++;
        else cnt1++;
    }
    if(cnt0>=cnt1) ans++;//n本身是 Round Number
    cnt0=0;
    cnt1=1;
    for(int i=len-2;i>=0;i--)
    {
        if(bits[i]==1)//后面有i位,第i位當成0
        {
            for(int j=i;j>=0&&j+cnt0+1>=i-j+cnt1;j--)ans+=C[i][j];
            cnt1++;
        }
        else cnt0++;
    }
    return ans;
}

int main()
{
    init();
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        printf("%d\n",calc(b)-calc(a-1));
    }
    return 0;
}

 

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM