poj3252 Round Numbers

Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11387   Accepted: 4263

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

思路:先枚举2的次方,如果这个数小于目标tar,那么统计它之内的round number个数
2的次方数大于tar时,将tar转成2进制,向后枚举每一位,对于每个是1的位,先取0,向后统计,再把他回复成1
注意的细节:
get(n-1)和get(m+1),因为get函数并没有算边界


代码没有过,runtime error,不过对拍是对的,还望高手赐教

代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int c[333][333];
int a,b,d,n,m;
int comb()
{
        for (int i=0;i<=32;i++)
                for (int j=0;j<=32;j++)
                        c[i][j]=0;
                c[0][0]=1;
        for (int i=1;i<=32;i++)
                for (int j=0;j<=i;j++)
                {
                        if (!j || i==j)
                                c[i][j]=1;
                        else
                        {
                         c[i][j]=c[i-1][j-1]+c[i-1][j];
                        }
                }
        return 0;
}
int j2[503];
int get(int aa)
{
        if (aa==0) return 0;
        if (aa==1) return 0;
        if (aa==2) return 1;
        if (aa==3) return 1;
        int f=0;
        int ans=0;
        int w=1;
        ans=0;
        for (int i=4;i<=aa;i*=2)
        {
                w++;
                for (int k=w-1;k>=(w+1)/2;k--)
                        {
                        ans+=c[w-1][k];
                        }
                if (i==aa)
                        f=1;
        }
        if (f==1) return ans+1;
        a=1;
        for (int i=0;i<=40;i++)
                j2[i]=0;


        int bb=aa;
        int biao=0;
        int cc=2;
        while(bb!=0)
        {
                j2[++biao]=bb%2;
                bb/=2;
        }


        int num0,num1;
        num0=0;
        num1=0;
        for (int i=1;i<=biao;i++)
                if (j2[i]==1)
                        num1++;
                else num0++;
        if (num0>=num1)
                ans++;
        num1=0;
        num0=0;

        int sta;
        for (int i=40;i>=1;i--)
                if (j2[i]!=0)
                {
                        sta=i;
                        break;
                }
        num1=1;
        w++;
        for (int i=sta-1;i>=1;i--)
        {
                if (j2[i]==0)
                {
                        num0++;
                        continue;
                }
                num0++;
                for (int k=(w+1)/2-num0;k<=w-num1-num0;k++)
                {
                        ans+=c[w-num1-num0][k];
                }
                num0--;
                num1++;
        }
        return ans;
}
int main()
{
        comb();

        scanf("%d%d",&n,&m);
        int aa,bb;
        aa=get(n-1);

        bb=get(m);
        cout<<bb-aa<<endl;
}




猜你喜欢

转载自blog.csdn.net/williamcode/article/details/51023340