poj 3252 RoundNumber

來源:互聯網
上載者:User

  題意是求把十進位數轉化成位元,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;}                  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.