POJ 3252 组合计数

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9149 Accepted: 3248
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

USACO 2006 November Silver


/******************************
    author   : Grant Yuan
    time     : 2014/10/16 17:37
    algorithm: 组合计数
    source   : POJ 3252
    explain  : 按照G++交的,C++会跪
*******************************/
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int a[35],len;
int sum[40][40];
void slove(int num)
{
    a[0]=0;len=0;
    while(num){
        a[++len]=num&1;
        num=num>>1;
    }
}
void Cal_sum()
{
    memset(sum,0,sizeof(sum));
    for(int i=0;i<=32;i++)
        for(int j=0;j<=i;j++)
    {
        if(j==0||i==j) sum[i][j]=1;
        else sum[i][j]=sum[i-1][j]+sum[i-1][j-1];
    }
}
int Get_sum1()
{
    int l=len,res=0;
    for(int i=1;i<len-1;i++)
    {
        for(int j=i/2+1;j<=i;j++)
        {
            res+=sum[i][j];
        }
    }
    return res;
}
int Get_sum2()
{
    int res=0,zero=0;
    for(int i=len-1;i>=1;i--)
    {
        if(a[i]){
            for(int j=(len+1)/2-zero-1;j<i;j++)
                res+=sum[i-1][j];
        }
        else zero++;
    }
    return res;
}
int main()
{
    int ans1,ans2,s,f;
    Cal_sum();
    while(~scanf("%d%d",&s,&f)){
        ans1=0;ans2=0;
        slove(s);
        ans1+=Get_sum1();ans1+=Get_sum2();
        slove(f+1);
        ans2+=Get_sum1();ans2+=Get_sum2();
        printf("%d %d
",ans1,ans2);
        printf("%d
",ans2-ans1);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/codeyuan/p/4254412.html