POJ 3252 Round Numbers (数位dp)

Round Numbers
Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 12326
Accepted: 4698

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

题解:数位时最重要的是处理前导零,因为这题前导零不算数,所以增加一个标记变量first,标志着当前这意味是不是首位,不是首位的话1和0的个数都为0,否则根据枚举的1或0进行记忆化搜索。用一个bool的变量less表示之前枚举的此数位上是否已经有过数字比给定的上界的对应数位上的数字大。真就枚举到当位,否则就枚举到1。典型的数位dp。

AC代码:
//#include
#include
#include
#include
using namespace std;
typedef long long ll;

int dig[61];
int dp[61][61][61]; //dp[i][j][k]表示 到 i位时 0的个数为j,1的个数为 k

int dfs(int pos, int num0, int num1, int less, int first)
{
	if(pos < 0)
	{
		if(num0 >= num1)
			return 1;
		return 0;
	}
	if(less==0 && first==0 && dp[pos][num0][num1] != -1)
		return dp[pos][num0][num1];

	int n = less ? dig[pos] : 1;
	int res = 0;
	for(int i = 0; i <= n; i++)
	{
		if(first==1)//当前是首位
		{
			if(i == 0) //如果 i 取 0 那么下一位是首位,到 i 位 为止 0 和 1 的个数都是0
				res += dfs(pos-1,0,0, less&&i==n, 1);
			else
				res += dfs(pos-1,num0,num1+1,less&&i==n, 0);//这一位是首位,1的个数 +1
		}
		else //不是首位的话,分别对 0 或 1 的个数 + 1
		{
			if(i == 0)
				res += dfs(pos-1,num0+1,num1,less&&i==n, 0);
			else
				res += dfs(pos-1,num0,num1+1,less&&i==n,0);
		}
	}
	if(less==0 && first==0)
		dp[pos][num0][num1] = res;
	return res;
}

int solve(int x)
{
	int len = 0;
	while(x)
	{
		dig[len++] = x & 1;
		x>>=1;
	}
	return dfs(len-1,0,0,1,1);
}

int main()
{
	int x,y;
	memset(dp,-1,sizeof(dp));
	while(~scanf("%d %d",&x,&y))
	{
		printf("%d\n",solve(y) - solve(x-1) );
	}
	return 0;
}


你可能感兴趣的:(POJ,ACM_数位DP)