HDU 3555 Bomb (Digital DP)

Source: Internet
Author: User

HDU 3555 Bomb (Digital DP)

 

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 integerNIs said to be a "round number" if the binary representationNHas as primary 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.

Obviusly, 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 should round numbers appear in the random range given by the input (1 ≤Start<Finish≤ 2,000,000,000 ).

Input

Line 1: Two space-separated integers, respectively StartAnd Finish.

Output

Line 1: A single integer that is the count of round numbers in the random range Start.. Finish

Sample Input

2 12

Sample Output

6

 

For a simple digital DP, we can think that 0 is also qualified, but 0 is not considered.

So the only thing we need to consider is where the first number is not 0, and the length of the number, the number of 0.

These can be recorded.

 

#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 pair
           
            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;}
           
          
         
       
      
     
    
   
  
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.