POJ 3252 Round Numbers (digital DP)

Source: Internet
Author: User
Tags bitset

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11681 Accepted: 4392

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

Test instructions is the number of round in a range, which is 0 more than 1 in the binary form of this number. Convert the number to binary, and then simply make a bit DP. Found: In the digital DP in the judgment condition and 0, it is necessary to use a flag to prevent the number upside down after the beginning of 0, this 0 is obviously inappropriate.

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<bitset>using namespacestd;#definell Long Longintdig[ -];ll dp[ -][ -][ -];ll DFS (intPosintCnt0,intCnt1,intFlagintLim) {//if (pos = =-1) printf ("%d%d\n", cnt0, cnt1);    if(pos = =-1)returnCnt0 >Cnt1; if(!lim &&!flag && dp[pos][cnt0][cnt1]! =-1)returnDp[pos][cnt0][cnt1]; intEnd = Lim? Dig[pos]:1; LL ret=0;  for(inti =0; I <= End; i++) {        if(flag) {if(i) ret + = DFS (pos-1,0,0,0, Lim && (i = =End)); ElseRET + = DFS (pos-1,0,0,1, Lim && (i = =End)); }        Else {            if(i) ret + = DFS (pos-1, cnt0, Cnt1 +1,0, Lim && (i = =End)); ElseRET + = DFS (pos-1, Cnt0 +1, Cnt1,0, Lim && (i = =End)); }    }    if(!lim &&!flag) dp[pos][cnt0][cnt1] =ret; returnret;} ll func (ll num) {intn =0; //bitset<32> BTT (num);     while(num) {dig[n+ +] = num &1; Num>>=1; }    returnDFS (N-1,0,0,1,1);}intMain () {ll n, m; Memset (DP,-1,sizeof(DP));  while(~SCANF ("%i64d%i64d", &n, &m)) {printf ("%i64d\n", Func (M)-Func (N-1)); }}

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.