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

poj3252--Round Numbers

时间:2015-01-26 15:14:31      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:poj 3252

Round Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9565   Accepted: 3433

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
题意:统计区间内有几个round number,即将其换成二进制后0的个数大于等于1的个数
分析:两个区间a到b  先求b内的round numbers  再求a内的round numbers
     结果便是b-a
     假设要求m的round numbers 对于二进制长度小于m的二进制长度的个数来说 可以如图所求

技术分享
技术分享
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;int c[35][35]={0};//组合数 之前没有手动清零 wa 加上={0}后就A了 明明没有用while(cin>>...) 纳闷。。。int bit[35];void init()//c数组打表 类似于杨辉三角
{
    int i,j;
    for(int i=0; i<34; i++)
    {
        for(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];
        }
    }
}
void num_bit(int n)//转成二进制 存入bit数组
{
    bit[0]=0;//转换成二进制后的长度
    while(n)
    {
        bit[++bit[0]]=n%2;
        n/=2;
    }
}
int round(int n)
{
    int i,j;
    int sum=0;//统计round number个数
    num_bit(n);
    for(i=1; i<bit[0]-1; i++)//二进制长度小于n的round number个数
    {
        for(j=i/2+1; j<=i; j++)
        {
            sum+=c[i][j];
        }
    }
    //二进制长度等于n的round number个数
int num0=0;//从高位往低位遍历 num0记录遍历过的零的个数 for(i=bit[0]-1; i>=1; i--) { if(bit[i]) { for(j=(bit[0]+1)/2-(num0+1); j<i; j++)//(bit[0]+1)/2表示对于一个数 至少他的一半为0 才能是round number 但他前面已经出现了num个0,所以至少是(bit[0]+1)/2-(num0+1)
{ sum+=c[i-1][j]; } } else num0++; } return sum;}int main(){ init(); int s,e; cin>>s>>e; cout<<round(e+1)-round(s)<<endl; return 0;}

技术分享
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

poj3252--Round Numbers

标签:poj 3252

原文地址:http://blog.csdn.net/bigsungod/article/details/43151979

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