程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU 3555 Bomb(數位DP)

HDU 3555 Bomb(數位DP)

編輯:關於C++

 

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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 pairpil;
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;
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved