现在的位置: 首页 > 综合 > 正文

POJ 3252 Round Numbers(数学问题)

2012年10月18日 ⁄ 综合 ⁄ 共 3170字 ⁄ 字号 评论关闭
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;
}

 

     

抱歉!评论已关闭.