首页 > 其他 > 详细

HDU 3555 Bomb(数位DP)

时间:2015-03-28 06:27:57      阅读:173      评论:0      收藏:0      [点我收藏+]

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

简单的数位DP我们可以想到0也是符合条件的,但是0不予考虑。

所以我们唯一要考虑的就是第一个不为0的数在哪,以及数的长度,0的个数。

这些都可以记录下来。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
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 )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
LL l,r;
int num[60];
int dp[60][2][60][60],len;
LL dfs(int pos,int first,int len,int sum,int flag)
{
    if(pos==0)
        return sum>=len-sum;
    if(!flag&&dp[pos][first][len][sum]!=-1)
        return dp[pos][first][len][sum];
    LL ans=0;
    int ed=flag?num[pos]:1;
    for(int i=0;i<=ed;i++)
    {
        int s=sum,f=first,l=len;
        if(!first&&i) f=1;
        if(first&&i==0) s++;
        if(f) l++;
        ans+=dfs(pos-1,f,l,s,flag&&i==ed);
    }
    if(!flag)  dp[pos][first][len][sum]=ans;
    return ans;
}
LL solve(LL x)
{
    int pos=0;
    while(x)
    {
        num[++pos]=x%2;
        x/=2;
    }
    return dfs(pos,0,0,0,1)-len+1;
}
int main()
{
    CLEAR(dp,-1);
    while(~scanf("%lld%lld",&l,&r))
    {
        LL ans=solve(r)-solve(l-1);
        printf("%lld\n",ans);
    }
    return 0;
}


HDU 3555 Bomb(数位DP)

原文:http://blog.csdn.net/u013582254/article/details/44689811

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!