| Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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
Output
Sample Input
2 12
Sample Output
6
Hint
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
int C[35][35];
void init() //打表C(i,j)
{
int i,j;
for(i=0;i<33;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];
}
}
}
int bit[35];
int len;
void d(int n) //转换为二进制
{
len=0;
while(n)
{
bit[++len]=n%2;
n>>=1;
}
}
int solve(int n) //计算小于n的符合题意的个数
{
d(n);
int sum=0,i,j;
for(i=1;i<len-1;i++) //计算长度小于n的二进制数长度的符合条件的个数
{
for(j=i/2+1;j<=i;j++)
{
sum+=C[i][j];
}
}
int cnt0=0;
for(i=len-1;i>=1;i--) //计算长度等于n的二进制数长度的符合条件的个数
{
if(bit[i])
{
for(j=(len+1)/2-(cnt0+1);j<=i-1;j++)
{
sum+=C[i-1][j];
}
}
else
cnt0++;
}
return sum;
}
int main()
{
init();
int n,m;
scanf("%d%d",&n,&m);
printf("%d\n",solve(m+1)-solve(n));
return 0;
}

本文详细介绍了POJ-3252 RoundNumbers问题的算法思路,包括如何通过二进制转换来判断一个整数是否为roundnumber,以及计算指定范围内roundnumber的数量。通过代码实现,帮助读者理解并解决类似问题。

被折叠的 条评论
为什么被折叠?



