poj3252——组合数学

poj3252——组合数学

Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9790   Accepted: 3517

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
题意:求L和R区间中二进制数中0比1多或0等于1的数的个数
思路:转化为round(R)-round(L-1) ,转化为二进制用数组保存,先求长度小于len的,在求长度等于len的数量
  全靠自己调试出来的,庆祝一下!
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn=1000100;
const int INF=(1<<28);

int L,R;
int bin[maxn],len;
int C[1200][1200];

void playC()
{
    for(int i=0;i<1100;i++){
        for(int j=0;j<=i;j++){
            if(j==0||j==i) C[i][j]=1;
            else C[i][j]=C[i-1][j-1]+C[i-1][j];
        }
    }
}

int round(int n)
{
    if(n==0) return 0;
    len=0;
    memset(bin,0,sizeof(bin));
    while(n){
        bin[len++]=n%2;
        n>>=1;
    }
    int res=0;
    ///计算长度小于len的所有符合条件的二进制数的个数
    for(int i=1;i<len-1;i++){ ///枚举长度但不包括1
        for(int j=0;j+1<=(i+1)/2;j++){ ///枚举1的个数
            res+=C[i][j];
            //cout<<"i="<<i<<" "<<"j="<<j<<endl;
        }
    }
    ///  1 0 0 0   1 1 1
    //cout<<"res1="<<res<<endl;
    ///计算长度等于n且值小于等于bin的所有符合条件的二进制数的个数
    int k=1;///记录已出现的1的个数
    int resbug=0;
    if(len>1) res++;
    bool flag=0;
    for(int i=len-2;i>=0;i--){
        if(bin[i]){
            //if(k+1<=len/2) res++;
            for(int j=1;j+k<=len/2&&j<=i;j++){
                res+=C[i][j];
                resbug+=C[i][j];
            }
            if(k+1<=len/2) res++;
            //flag=1;
            k++;
        }
    }
    //cout<<"res="<<res<<endl;
    //cout<<"flag="<<flag<<endl;
    //if((!flag)&&len>=2) res++;
    //cout<<"resbug="<<resbug<<endl;
    return res;
}

int main()
{
    playC();
    //while(cin>>L) cout<<round(L)<<endl;
    /*
    while(cin>>L>>R){
        cout<<round(R)-round(L-1)<<endl;
    }
    */
    scanf("%d%d",&L,&R);
    printf("%d\n",round(R)-round(L-1));
    return 0;
}
View Code

 

posted @ 2015-03-25 11:32  __560  阅读(226)  评论(0编辑  收藏  举报