您的位置:首页 > 其它

POJ 3252 Round Numbers(数位DP)

2015-07-12 21:03 190 查看
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

题意:问你[a,b]区间中满足其二进制表达式中0的个数大于1的个数的数字个数
分析:数位DP直接搞,需要注意的就是由于前导0的存在,要记录前导0是否确定
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
LL a,b;
LL dp[60][60][60][2];
int num[35];
LL dfs(int pos,int sum1,int sum2,int first,int flag)//sum1:0   sum2:1
{
    if(!pos&&!first)
        return 0;
    if(!pos&&first)
        return sum1>=sum2;
    if(!flag&&dp[pos][sum1][sum2][first]!=-1)
        return dp[pos][sum1][sum2][first];
    int ed=flag?num[pos]:1;
    LL res=0;
    for(int i=0;i<=ed;i++)
    {
        if(!first&&i)
            res+=dfs(pos-1,sum1,sum2+1,1,flag&&i==ed);
        else if(!first&&!i)
            res+=dfs(pos-1,sum1,sum2,0,flag&&i==ed);
        else if(!i)
            res+=dfs(pos-1,sum1+1,sum2,first,flag&&i==ed);
        else
            res+=dfs(pos-1,sum1,sum2+1,first,flag&&i==ed);
    }
    if(!flag) dp[pos][sum1][sum2][first]=res;
    return res;
}
LL solve(LL x)
{
    int pos=0;
    while(x)
    {
        num[++pos]=x%2;
        x/=2;
    }
    return dfs(pos,0,0,0,1)+1;//dfs(pos,sum1,sum2,flag)
}
int main()
{
    CLEAR(dp,-1);
    while(~scanf("%lld%lld",&a,&b))
    {
        LL ans=solve(b)-solve(a-1);
        printf("%lld\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: