Poj 3252 round numbers (mathematical problem)

Source: Internet
Author: User
Round numbers
Time limit:2000 ms   Memory limit:65536 K
Total submissions:6051   Accepted:2050

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 Start And 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

Source

Usaco 2006 November silver This week, we are focusing on mathematical problems ~~~~ This question is clear,

Round numbersIt is a positive number that represents more than 1 or equal to 0 when it is binary. Note that it is a positive number, so 0 is definitely not. The question is given a range.How many round numbers are there? First, find the number of round numbers equal to or less than N. Let me give an example to illustrate the general method first. For example, if 22 = 10110b is required to <= 22's round numbers, that is, to find the number of binary numbers from 1 to 22, where 0 is not less than 1. The binary length of 22 is 5. First, find the round numbers with a length smaller than 5 (the number with a length smaller than 5 must be less than 22) If the length is 4, the first digit must be 1, and the last three digits can have 2 0, 3 0 So C () + C ); The round numbers with a length of 3. Similarly, C (2, 2) exists. // do not forget the first 1. Round numbers with a length of 2, with C) Round numbers with a length of 1, with 0 The following shows the round numbers with the same length as 22. The first priority is 1. The second digit of 22 is 0, so the second digit cannot be 1 and must be 0 If the third digit is 0, (there are 2 first 0, 1), the last two can have 1 0, 2 0 C (2, 1) + C (2, 2) Next, we will restore the third digit to 1 and look at the fourth digit. If the fourth digit is 0, (the first two are 0, 2 are 1), the next one must be 0 C) So the general process is as described above. First, a formula is used to calculate the number of round numbers whose length is Len. The length is Len, and the first one must be 1. Then there is a len-1 behind. If the len-1 is an even number. So C (len-1, (len-1)/2 + 1) + C (len-1, (len-1)/2 + 2) + ''' C (len-1, len-1) = (2 ^ (len-1)-C (len-1, (len-1)/2)/2; If Len is an odd number That is (2 ^ (len-1)/2 Therefore, it is easy to find a round numbers with a smaller length than N. As for the length, it is to gradually change the 1 of each digit to 0, while the latter can be smaller than N. ViewCodeRight. It is easy to understand.

# Include <stdio. h> # Include <Iostream> Using   Namespace  STD;  Int C [ 33 ] [ 33  ];  Void  Init () {C [  0 ] [0 ] = 1  ; C [  1 ] [ 0 ] = 1 ; C [ 1 ] [ 1 ] = 1  ;  For ( Int I = 2 ; I < 33 ; I ++ ) {C [I] [ 0 ] = 1  ;  For ( Int J = 1 ; J <I; j ++ ) C [I] [J] = C [I- 1 ] [J- 1 ] + C [I- 1  ] [J]; C [I] [I] = 1  ;}}  Int Bits [33  ];  Int Calc ( Int N) //  Evaluate the round numbers smaller than or equal to n  {  If (N <= 1 ) Return   0 ; //  This condition must be added      Int Len = 0  ; While (N> 0  ){  If (N & 1 ) Bits [Len ++] = 1  ;  Else Bits [Len ++] = 0  ; N >>= 1  ;}  Int Ans = 0  ; For ( Int I = len- 1 ; I> 0 ; I -- ){  If (I % 2 = 0 ) Ans + = (( 1 <(I- 1 )))/ 2  ;  Else Ans + = (( 1 <(I-1 )-C [I- 1 ] [(I- 1 )/ 2 ])/ 2  ;}  Int Cnt0 = 0 , Cnt1 = 0  ;  For ( Int I = 0 ; I <Len; I ++ ){  If (BITS [I] = 0 ) Cnt0 ++ ;  Else Cnt1 ++ ;}  If (Cnt0> = cnt1) ans ++; //  N itself is the round number Cnt0 = 0  ; Cnt1 = 1  ;  For ( Int I = len- 2 ; I> =0 ; I -- ){  If (BITS [I] = 1 ) //  There is an I-bit behind, and the I-bit is regarded as 0  {  For ( Int J = I; j> = 0 & J + cnt0 + 1 > = I-j + cnt1; j --) ans + = C [I] [J]; cnt1 ++ ;} Else Cnt0 ++ ;}  Return  Ans ;}  Int  Main () {Init ();  Int  A, B;  While (Scanf ( "  % D  " , & A, & B )! = EOF) {printf (  "  % D \ n  " , Calc (B)-Calc (- 1  ));}  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.