码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3252 组合计数

时间:2014-10-16 20:52:43      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:des   io   os   ar   for   sp   2014   art   on   

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\n",ans1,ans2);
        printf("%d\n",ans2-ans1);
    }
    return 0;
}


POJ 3252 组合计数

标签:des   io   os   ar   for   sp   2014   art   on   

原文地址:http://blog.csdn.net/yuanchang_best/article/details/40151989

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!