Poj3252-Round number combination mathematics

Source: Internet
Author: User

Question:

Round numbers
Time limit:2000 ms   Memory limit:65536 K
Total submissions:8492   Accepted:2963

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

The idea is basically the same as http://zhyu.me/acm/poj-3252.html:

For example,

Number of roundnumbers (RN) in the [] interval: rn [] = rn []-Rn []
That is: rn [start, finish] = rn [0, finish]-Rn [0, start-1]
So the key is to give an X and obtain rn [0, x].
Now suppose x = 10100100
The binary value of X is 8 bits in total, and any binary value smaller than 8 bits is less than X
In the first part, obtain the number of roundnumber in the binary value range of [0, 7 ].
For a binary string with a length of Len (the highest bit is 1), how can we find its roundnumbers (expressed by R (LEN), which can be an odd or even number?
1. Odd Number: in the case of Len = 2 k + 1, the maximum bit is 1, and the remaining 2 k digits, at least k + 1 is 0
Use C (m, n) to represent the number of permutation combinations: the method for selecting n positions from M positions
R (LEN) = C (2 k, k + 1) + C (2 k, K + 2) +... + C (2 k, 2 K ).
Because a: C (2 k, 0) + C (2 k, 1) +... + C (2 k, 2 K) = 2 ^ (2 k)
B: C (2 k, 0) = C (2 k, 2 K), C (2 k, 1) = C (2 k, 2k-1 ),, C (2 k, I) = C (2 k, 2 k-i)
So C (2 k, 0) + C (2 k, 1) +... + C (2 k, 2 K)
= C (2 k, 0) + C (2 k, 1) +... + C (2 k, k) + C (2 k, k + 1) + C (2 k, K + 2) +... + C (2 k, 2 K)
= 2 * R (LEN) + C (2 k, K)
= 2 ^ (2 k)
So R (LEN) = 1/2*{2 ^ (2 k)-C (2 k, k )};
2. For even numbers, Len = 2 * K, which can be pushed to R (LEN) = 1/2*(2 ^ (2k-1 ));
In the second part, for the above example with the length of 8: x = 10100100, if it is roundnumbers, the total number of results in the second part is + 1
The first part has obtained the part with the length less than 8. The number of roundnumbers with the length = 8
The length is 8, so the first 1 cannot be changed
Now to the second 1, if y is a binary prefix such as 100 *****, under this prefix, the values 0 and 1 must be smaller than X, and there are already two zeros, 1. The remaining five digits must contain at least two zeros,
So change the second 1 to 0: C (5, 2) + C (5, 3) + C (5, 4) + C (5, 5)
Now the third 1, that is, the first 101000 **, can also be obtained, at least 0 0 is required, so C () + C) roundnumbers
...
Change all 1 except the first 1 to 0. Calculate the number of roundnumbers and add the result (because the prefix is different, no matter how the combination is followed)


The result of the first part and the second part is the final result.


The only special note is that it is easy to cross-border when calculating the number of combinations. Although the above analysis shows that the calculation result is correct within the int range, however, the intermediate process of calculating the number of combinations is likely to be out of bounds, so pay special attention here.

The solution is to use C (n, m) = C (n-1, m-1) + C (n-1, m) for Recursive calculation, rather than using the traditional multiplication calculation method. To be more efficient, you can calculate n = 1 ~ in advance ~ 32, M = 1 ~ The result of the number of combinations of 32 is saved.

import java.util.*;public class Combinatorics_RoundNumbers3252 {/** * @param args */public static void main(String[] args) {Scanner in=new Scanner(System.in);Init();while(in.hasNext()){int a=in.nextInt();int b=in.nextInt();//System.out.println(roundNumber(a-1)+" " +roundNumber(b));System.out.println(roundNumber(b)-roundNumber(a-1));}}   static int c[][]=new int[35][35];   public static void Init(){    for(int i=0;i<33;i++){        c[i][0]=c[i][i]=1;        for(int j=1;j<i;j++)            c[i][j]=c[i-1][j]+c[i-1][j-1];    }   }public static int roundNumber(int value){char b[]=toBinary(value);int sum=0;for(int len=1;len<b.length;len++){for(int j=(len+1)/2;j<len;j++)sum+=c[len-1][j];}int zeros=0;for(int i=1;i<b.length;i++){if(b[i]=='1'){int k=(b.length+1)/2;int m=Math.max(0, k-(zeros+1));int n=b.length-i-1;for(int j=n;j>=m;j--)sum+=c[n][j];}else{zeros++;}}if(2*zeros>=b.length)sum++;return sum;}private static char[] toBinary(int value) {return Integer.toBinaryString(value).toCharArray();}/*public static int roundNumberOfLength(int len){int k=len/2;if(len%2==0){return (1<<(len-2));}else{return ((1<<(len-1))-choose(len-1,k))/2;}}*/public static int choose(int n, int m) {if(n==0)return 0;if(m==0||m==n)return 1;if(m>n)return 0;return choose(n-1,m-1)+choose(n-1,m);}}



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.