POJ 3252 Round Numbers (digital DP)

Source: Internet
Author: User

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9889 Accepted: 3566

Description

The cows, as you know, has no fingers or thumbs and thus is 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 has thus resorted to "round number" matching. The first cow picks an integer less than and the billion. The second cow does the same. If The numbers is both "round numbers", the first cow wins,
Otherwise the second cow wins.

A positive integer n is said to being a "round number" if the binary representation of N has as many or mor E zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has zeroes and ones; Thus, 9 is a round number. The integer is 11010 in binary; Since it has both 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 and to determine. Bessie wants to cheat and thinks she can doing if she knows how many "round numbers" is in a given range.

Help she 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 Startand Finish.

Output

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

Sample Input

2 12

Sample Output

6

Source


Test instructions: The number of binary 0 numbers in the interval [l,r] is greater than or equal to 1.


Puzzle: Digital DP. Now we know the power of this set of templates ...


Code

#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include < Cmath> #define LL Long longusing namespace Std;ll l,r;ll dp[40][40][40];///dp[i][j][k]:  i:  the number of length I j:1   k : 0 of the number of int num[50];///template, plus Lze to determine if it is a leading 0ll dfs (int i,int one,int zero,bool e,bool lze) {    if (i<=0) return Zero>=one ;    if (!e&&dp[i][one][zero]!=-1) return Dp[i][one][zero];    ll Res=0;    int u=e?num[i]:1;    for (int d=0; d<=u; d++) {        Res+=dfs (i-1,one+ (d==1), zero+ (D==0&&!lze), e&&d==u,lze&&d= =0);    }    return e?res:dp[i][one][zero]=res;} ll solve (ll N) {    int len=1;    while (n) {        num[len++]=n&1;        n>>=1;    }    Return Dfs (len-1,0,0,1,1);} int main () {    //freopen ("In.txt", "R", stdin);    memset (dp,-1,sizeof dp);    while (cin>>l>>r) {        printf ("%lld\n", Solve (R)-solve (L-1));    }    return 0;}


Poj 3252 Round Numbers (digital DP)

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.