poj 3252 RoundNumber

  题意是求把十进制数转化成二进制数,0的个数大于等于1 的数,给定一个闭区间求出区间的这样的数有多少个。

 

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

这是一个找规律的模拟数学题,虽然WA了三次不过经过不懈的努力还是AC了。(*^__^*) 嘻嘻……

一定要注意细心,耐心。

代码:

#include<iostream>
using namespace std;
int num[35],sum[35],numa[35],numb[35];
int dp[35][35];
int main()
{
    int a,b,i,j,la,lb,len,suma,sumb,cnt,k;
    dp[0][0]=1;
    for(i=1;i<=34;i++){dp[i][0]=1;dp[0][i]=0;}
    for (i=1;i<34;++i)
        for (j=1;j<=i;++j)
            dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
    memset(sum,0,sizeof(sum));
    for(i=2;i<=34;i++)
    {
      for(j=0;j<=i/2-1;j++)
         sum[i]+=dp[i-1][j];
    }
    while(cin>>a>>b)
    {
      la=0;
      int p=a;
      while(p)
      {
        numa[la++]=p%2;
        p=p/2;
      }    
      suma=0;
      k=0;
      for(i=0;i<=la-1;i++) suma+=sum[i];
      for(i=la-2;i>=0;i--)
      {
         if(numa[i]==1)
         {
           for(j=(la+1)/2-k-1;j<=i;j++)
            suma+=dp[i][j];
         }
         else k++;
      }
      p=b+1;lb=0;
      while(p)
      {
        numb[lb++]=p%2;
        p=p/2;
      }
      sumb=0;k=0;
      for(i=0;i<=lb-1;i++) sumb+=sum[i];
      for(i=lb-2;i>=0;i--)
      {
          if(numb[i]==1)
          {
             for(j=(lb+1)/2-k-1;j<=i;j++)
             {
              sumb+=dp[i][j];
             }
          }
          else k++;
      }
      cout<<sumb-suma<<endl;
    }
    return 0;
}
      
      
      


 

你可能感兴趣的:(poj 3252 RoundNumber)