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
Output
Sample Input
2 12
Sample Output
6
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;
int c[33][33], n, m;
long long k[33];
long long ff(int x)
{
memset(k, 0, sizeof(k));
int y = x, len = 0;
while(y)
{
k[++len] = y%2;
y/=2;
}
if(!len)return 0;
long long ans = 0;
for(int i = 2;i<len;++i)
for(int j = (i-1)/2+1; j<i;++j) // 长度比x小的round数个数
ans+=c[i-1][j];
//printf("%lld\n", ans);
int zero = 0, one = 1;<span style="font-family: Arial, Helvetica, sans-serif;">// 统计之前0和1的个数</span>
for(int i = len-1; i>=1;--i) // 第一位肯定是1, 跳过
{
if(k[i]==1)
{
one++;
for(int j = 0;j<=i-1;++j)
if(zero+1+j>=one+i-1-j-1) // 之前0的个数加上后面0的个数>=之前1的个数+后面1的个数
ans+=c[i-1][j]; // <span style="font-family: Arial, Helvetica, sans-serif;">如果当前位数是1, 假设它是0, </span><span style="font-family: Arial, Helvetica, sans-serif;">保证都是比x小的数</span>
}
else zero++;
}
return ans;
}
int main()
{
memset(c,0, sizeof(c));
for(int i = 0; i<=32;++i)
for(int j = 0;j<=i;++j)
{
if(j==0||i==j)c[i][j] = 1;
else c[i][j] = c[i-1][j]+c[i-1][j-1];
}
scanf("%d%d", &m, &n);
printf("%lld\n",ff(n+1)-ff(m));
return 0;
}