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
Source
USACO 2006 November Silver
网上方法很多
我的思路看起来偏偏要复杂
设dp[pos][e][zero][one]表示处理到pos位,前一位为e,有zero个0,one个1的数的个数
/************************************************************************* > File Name: poj3252.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年02月23日 星期一 13时04分13秒 ************************************************************************/
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
LL dp[50][50][50][50];
int bit[50];
int cnt;
LL dfs (int cur, int e, int zero, int one, bool flag, bool is_zero)
{
if (cur == -1)
{
if (is_zero)
{
return 0;
}
return zero >= one;
}
if (!flag && ~dp[cur][e][zero][one])
{
return dp[cur][e][zero][one];
}
LL ans = 0;
int end = flag ? bit[cur] : 1;
for (int i = 0; i <= end; ++i)
{
if (is_zero && i == 0)
{
ans += dfs (cur - 1, 0, 0, 0, flag && (i == end), 1);
}
else if (is_zero && i)
{
ans += dfs (cur - 1, i, 0, 1, flag && (i == end), 0);
}
else
{
ans += dfs (cur - 1, i, zero + (!i), one + i, flag && (i == end), 0);
}
}
if (!flag)
{
dp[cur][e][zero][one] = ans;
}
return ans;
}
LL calc (LL n)
{
cnt = 0;
while (n)
{
bit[cnt++] = n % 2;
n >>= 1;
}
return dfs (cnt - 1, 0, 0, 0, 1, 1);
}
int main ()
{
memset (dp, -1, sizeof(dp));
LL l, r;
while (~scanf("%lld%lld", &l, &r))
{
printf("%lld\n", calc (r) - calc (l - 1));
}
return 0;
}