POJ-3252 Round Numbers (digital DP)

Source: Internet
Author: User

Round Numbershttp://poj.org/problem?id=3252
Time Limit: 2000MS Memory Limit: 65536K

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

The main idea: in the statistical interval [sta,des], the binary represents the number of numbers in the lower 0 than the number of 1 (excluding the leading 0)?


After writing to read a lot of the puzzle, found that we use the number of DP are to use DFS, feel the direct loop is also very good understanding ah ...

Set Dp[i][j][0] for the length of I, containing J 0 and the highest 0 of the number of digits; dp[i][j][1] indicates the number of digits with a length of I, J 0 and a maximum of 1

Initial: dp[1][0][1]=1;//length of 1, containing 0 0 and the highest 1 of the number is 1
The dp[1][1][0]=1;//length is 1, with 1 0 and a maximum of 0 for the number 1

And then through the state transfer, preprocess all the results

Finally, bitwise statistics from high to low in the function

Almost a bit of feeling, seemingly digital DP are advanced pretreatment, and then by the bit statistics can be


#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int num[35],len,sta,des , Ans,cnt[2];//cnt[i] Indicates the number of occurrences of the binary from high to current bit I (i=0,1) int dp[35][35][2];//dp[i][j][0] representing the length of I, containing J 0 and the number of the highest 0; dp[i][j    ][1] Represents the length of I, containing J 0 and the number of the highest 1 is the number of void Init () {memset (dp,0,sizeof (DP)); The dp[1][0][1]=1;//length is 1, contains 0 0 and the highest 1 number is 1 dp[1][1][0]=1;//length 1, 1 0 and the highest number of 0 is 1 for (int i=2;i<=31;++i) {D p[i][0][1]=1;//length is I, contains 0 0 and the maximum number of 1 is 1 for (int j=1;j<=i;++j) {dp[i][j][0]=dp[i-1][j-1][0]+dp[i-1][j- 1][1];//the first place takes 0, the latter I-1 bits contain j-1 0, the i-1 is preferable to 0, also 1 dp[i][j][1]=dp[i-1][j][0]+dp[i-1][j][1];//i-bit 1, the latter I-1 bits contain J 0, and the i-1-bit is desirable    0, also desirable 1}}}int getcnt (int x) {///return interval [1,x] in the binary representation, 0 numbers greater than or equal to 1 digits total number cnt[0]=cnt[1]=ans=len=0;        while (x>0) {num[++len]=x%2;    x/=2; } for (int i=len;i>1;--i) {for (int j= ((i-1) >>1) + ((i-1) &1), j<i;++j) {//from the high of I to take 0, the i-1 bit to 1, the latter I-1 bits 0       Number of numbers greater than or equal to 1 digits ans+=dp[i-1][j][1]; } if (num[i]==1) {if (cnt[1]!=0) {for (int j=max (0, (len>>1) + (len&1) -1-cnt[0]); j                &LT;I;++J) {//from i+1 the same as the original number, the first I-bit take 0, the latter I-1 bits in 0 number + has appeared 0 number is greater than 1 number + has appeared 1 number of ans+=dp[i-1][j][0]+dp[i-1][j][1];        }} ++cnt[1];        } else {//current bit is 0 ++cnt[0]; }} if (num[1]==1) {//If the lowest bit is 1 if (cnt[1]!=0) {///High has occurred 1 (that is, the number is not 1) {//high in 0 occurrences more than 1 out            The current number is 0, 1 o'clock is legal ans+=2;            } else if (Cnt[0]==cnt[1]) {//High 0 occurrences of the number of occurrences equal to 1 occurrences of the number of times, the current bit fetch 0 o'clock legal ++ans;        }}}} else if (len>0) {//If the number is not 0 and the current bit is 0 ++cnt[0];        if (Cnt[0]>=cnt[1]) {//The number of all 0 is greater than the number of 1, then this number is legal ++ans; }} return ans;    int main () {Init ();    while (2==SCANF ("%d%d", &sta,&des)) {printf ("%d\n", getcnt (DES)-getcnt (sta-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.