Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11019 Accepted: 4072
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
题意:
问[m,n]内一共有多少数用二进制表示时0大于等于1.
题解:
定义dp[i][j]为i位二进制数里有j个0的个数,即为C(i-1,j)。
我们可以算出i位二进制数总共有多少个满足题意的数。
计算m是个t1位的二进制数,计算从m到t1位二进制数的最大值之间总共有多少满足题意的数s1。
n是个t2位的二进制数,计算从n到t2位二进制数的最小值之间总共有多少满足题意的数s2。
如果t1 == t2 说明n和m是同样的二进制长度,我们用s1+s2-该位数长度二进制数总共满足题意得数,就是结果。
入股t1!=t2,我们s1+s2再加上m和n之间区间的各个位数长度的总二进制长度。
那么问题就变成了如何计算s1和s2,以s1为例
假设m = 100110(二进制),那么可知11XXXX(X可能为0也可能为1)一定比m要大同时11XXXX也和m是相同的二进制长度。那么我们就只需要求XXXX里有多少数使得11XXXX的0大于等于1即可,也就是求C(4,3)+C(4,4)这里直接用上面的dp数组就可以了。也就是我们从头开始遍历100110,当遇到1时我们记录1的个数加1当遇到0时我们假设把0变成1,然后剩余的位数有多少种可能构造出满足题意的数。这样就求出了s1.
同样的,我们只需要反过来就可以求s2.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#define f(i,a,b) for(i = a;i<=b;i++)
#define fi(i,a,b) for(i = a;i>=b;i--)
using namespace std;
int dp[40][40];
int a[10];
int p;
int b[10000];
void init(){
int i,j,k;
memset(dp,0,sizeof(dp));
f(i,1,35) dp[i][0] = 1;
f(i,2,35)
f(j,1,i)
dp[i][j] = dp[i-1][j] + dp[i-1][j-1];
}
int Cnt(int k){
int sum = 0;
while(k){
k>>=1;
sum++;
}
return sum;
}
int Sum1(int n){
int i,sum = 0;
f(i,(n+1)/2,n-1){
sum+=dp[n][i];
}
return sum;
}
int Sum2(int n,int k){
int i,j,sum = 0,tem = 0;
fi(i,n-1,0){
if((1<<i)&k)
tem++;
else{
f(j,0,n-(n+1)/2-(tem+1))
sum+=dp[i+1][j];
}
}
if(tem<=(n-tem)) sum++;
return sum;
}
int Sum3(int n,int k){
int i,j,sum = 0,tem = 0;
fi(i,n-2,0){
if(!((1<<i)&k))
tem++;
else{
f(j,(n+1)/2-(tem+1),i)
sum+=dp[i+1][j];
}
}
if(tem>=(n-tem)) sum++;
return sum;
}
int main()
{
init();
int i,j;
int m,n,ans;
while(~scanf("%d%d",&m,&n)){
int t1 = Cnt(m), t2 = Cnt(n);
int S1 = Sum2(t1,m);
int S2 = Sum3(t2,n);
if(t1 == t2){
ans = Sum1(t1);
ans = S1+S2-ans;
}else if(abs(t1-t2) == 1)
ans = S1+S2;
else{
ans = S1+S2;
f(i,t1+1,t2-1) ans+=Sum1(i);
}
printf("%d\n",ans);
}
return 0;
}