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
题目链接
参考题解
题目要求区间内二进制中0的个数大于等于1的个数的数字的个数。
思路:先将区间[a, b]两端点转化为二进制,分别求出0到a之间的和0到b之间的符合要求的数字个数。求0到n中符合条件的数字个数,用到dfs,首先要确定最高位的1放下与否。首位1的确定其实就是从0到n的遍历。(如果一直dfs到最后你的第一个1都没放下,那么这个数字是0,在最后一次递归里面才放下,那么这个数字就是1,以此类推。)
dp[k][i][j],k为长度,i,为0的个数,j为1的个数。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[50][50][50], bit[50]; //dp[i][j][k]表示长度为i位,0的个数为j,1的个数为k
//HighBit代表当前最高位,Flag代表高位是不是访问到上限,Fir表示第一个1是否已经放下
int Dfs(int HighBit, int Num0, int Num1, bool Flag, bool Fir)
{
//已经全部遍历完了,看01个数
if(HighBit == 0) return Num0 >= Num1;
//如果高位还没有遍历完,当前位数又已经记录,直接返回
if(Flag == 0 && Fir == 0 && dp[HighBit][Num0][Num1] != -1) return dp[HighBit][Num0][Num1];
int ans = 0;
//如果前面的已经遍历完,那么当前为作为最高位进行遍历
int End = Flag ? bit[HighBit] : 1;
for(int Digit = 0; Digit <= End; Digit++)
{
if(Fir) //第一位1还没放下的情况
{
if(Digit == 0) //第一个1还没出现
ans += Dfs(HighBit - 1, 0, 0, Flag && Digit == End, 1);
else
ans += Dfs(HighBit - 1, Num0, Num1 + 1, Flag && Digit == End, 0); //在这一位上放下第一个1
}
else
{
if(Digit == 0)
ans += Dfs(HighBit - 1, Num0 + 1, Num1, Flag && Digit == End, 0);
else
ans += Dfs(HighBit - 1, Num0, Num1 + 1, Flag && Digit == End, 0);
}
}
if(!Flag && !Fir) dp[HighBit][Num0][Num1] = ans; //如果高位还没有枚举完,那么低位肯定需要列举各种情况,可以记忆化
return ans;
}
int Solve(int n)
{
int len = 0;
while(n)
{
bit[++len] = n & 1;
n >>= 1;
}
return Dfs(len, 0, 0, true, true);
}
int main()
{
memset(dp, -1, sizeof(dp));
int Left, Right;
while(cin >> Left >> Right)
cout << Solve(Right) - Solve(Left - 1) << endl;
return 0;
}