poj 3252 Round Numbers( 组合的应用)

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11850 Accepted: 4461

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
题意:找出n到m中,所有数字转换成二进制后,0的个数大于等于1的个数的所有情况
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

int c[35][35];
int a[35];
int b[35];
void change(int* p,int n)//   逆序
{
    int i=0;
    int j=n-1;
    for(; i<=j; j--,i++)
    {
        int t;
        t=*(p+i);
        *(p+i)=*(p+j);
        *(p+j)=t;
    }
}
int searc(int n,int x,int y,int s[]) 
{
    if(n<=0)return 0;         //如果只有一位数那么只能是1,因为题目里说>=1
    int sum=0;
    for(int i=1; i<n; i++)           //找出位数小于当前数字的情况,
    {
        for(int j=(i-1)/2+1; j<=i; j++)   //j的初始值为比当前长度减1,的一半大的数,不管几位初始位都为1,所以在剩下的里边选0多的
        {
           sum+=c[i-1][j];
        }

    }
    if(x<=y)sum++;           //如果这个数本身就是一个round number则加入总数
    int zero=0;
    for(int i=1; i<n; i++)
    {
        if(s[i])                        //如果当前数组的数是1,那么可以把它换位0,
        {
            for(int j=(n-1)/2-zero; j<=(n-1-i); j++)
            {
               sum+=c[n-1-i][j];
              // printf("****%d\n",sum);
            }

        }
        else zero++;
    }
    return sum;
}
int main()
{
    c[0][0]=1;                           
    for(int i=1; i<=33; 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];
        }

     int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int x1=0,y1=0;
        int s1=0;
        int sum=0;
        while(n)            //将n转换为二进制
        {
            int k=n%2;
            a[s1++]=k;
            n/=2;
            if(k)x1++;
            else y1++;
        }
        change(a,s1);
        int x2=0,y2=0;
        int  s2=0;
        while(m)
        {
            int k=m%2;
            b[s2++]=k;
            m/=2;
            if(k)x2++;
            else y2++;
        }
        change(b,s2);
        int ki;
        if(x1<=y1) ki=1;   //如果n本身就是round number,那么会减去它本身,所以需要加上1;
        else  ki=0;
        printf("%d\n",searc(s2,x2,y2,b)-searc(s1,x1,y1,a)+ki);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值