POJ - 3252 Round Numbers【数位DP】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/82875685

Time limit 2000 ms
Memory limit 65536 kB

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


题目分析

d p [ i ] [ j ] [ k ] dp[i][j][k] 表示 处理到第 i i 位,当前有 j j 个0和 k k 个1 的满足条件的数字个数
我们DP的过程需要在二进制下填每一位的数码
要特别注意前导0的个数不能计入不要计入状态内

int DP(int len,int st0,int st1,int pre,int judge)//judeg判断上一位是否为前到0(即从最高位到上一位是否全为0)
{
	if(len==0) return (st0-st1)>=0;
	if(!pre&&!judge&&dp[len][st0][st1]!=-1) return dp[len][st0][st1];
	int res=0,mx=pre?dig[len]:1;
	
	for(int i=0;i<=mx;++i)//st0+(i==0&&!judge)中注意加入了前导0不能计入的判断
	res+=DP(len-1,st0+(i==0&&!judge),st1+(i==1),pre&&i==mx,judge&&i==0);
	
	if(!pre&&!judge) dp[len][st0][st1]=res;
	return res;
}

int solve(int x)
{
	int len=0;
	while(x)
	{
		dig[++len]=x&1;//保存二进制下每一位的数码
		x=x>>1;
	}
	return DP(len,0,0,1,1);
}

完整代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long lt;
 
int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=35;
int A,B;
int dp[maxn][maxn][maxn],dig[maxn];

int DP(int len,int st0,int st1,int pre,int judge)
{
	if(len==0) return (st0-st1)>=0;
	if(!pre&&!judge&&dp[len][st0][st1]!=-1) return dp[len][st0][st1];
	int res=0,mx=pre?dig[len]:1;
	
	for(int i=0;i<=mx;++i)
	res+=DP(len-1,st0+(i==0&&!judge),st1+(i==1),pre&&i==mx,judge&&i==0);
	
	if(!pre&&!judge) dp[len][st0][st1]=res;
	return res;
}

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

int main()
{
    A=read();B=read();
    memset(dp,-1,sizeof(dp));
    printf("%d",solve(B)-solve(A-1));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/82875685