[Preface] I just started learning and want to find some simple questions to start with. There are a lot of people who see this question, so I also want to do this.
[Question]
Time limit:2000 ms 

Memory limit:65536 K 
Total submissions:4732 

Accepted:1607 
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 spaceseparated 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
[Idea]
1. The first idea is to input a number N. Can I set F (n) to the number of "round numbers" ≤ n? Then the question can be converted to F (finish)f (START1 ).
2. How can I find this F (n )?
1) before finding this F (N), evaluate the Binary Expression of N: (an) (An1 )... (A1) (A0 ). of course, this an = 1 (the first is 1 ). N = 11,001,001 is used as an example (this is an 8digit number ).
2) individuals who want to identify a respectively) <10,000,000, B) 10,000,000 ~ 11,000,000, c) 11,000,000 ~ 11,001,000, d) 11,001,000 ~ The number of "round numbers" between 11,001,001 and 10,101,001 (left open and right closed) cannot be solved ?!
3) and a) can correspond to a 7bit "0" and "1" string X, XXX, XXX, each digit can be 0 or 1, number of strings that meet Count ("0")> = count ("1. this is C (7,0) + C (7,1) +... + C (7,3 ).
Here we will make a slight modification. a) corresponds to a string with 7 digits + 1 at the first place; a string with 6 digits + 1 at the first place ;......;
B) It can correspond to a 6digit "0" and "1" string of 10,000,000 + XXX, XXX, and any 0 or 1 on each bit, meeting the requirements of Count ("0 ") + 1> = count ("1") + 1 (the first two digits are 10) Number of strings. this is C (6, 0) + C (6, 1) +... + C (6, 3 ).
C) d) Similarly, e) Special Handling of the last digit.
4) A function is needed to solve the problem: a Kbit "0" and "1" string XX... x. The number of strings that meet the requirements of Count ("0") + zeronum> = count ("1") + onenum.
Define a function to solve such a problem.
Int tempfunction (int K, int zeronum, int onenum) {int temp = (K + zeronum + 1onenum) % 2 = 0 )? (K + zeronum + 1onenum)/2): (K + zeronumonenum)/2 + 1); // set the upper bound for count ("1 ") // return C (K, 0) + C (k, 1) +... + C (K, temp1) int sum = 0; If (temp <= 0) {return 0 ;}for (INT I = 0; I <temp; ++ I) {// sum = sum + yanghuitriangle [(k1) * k/2 + I1];} return sum ;}
The value of the Yang Hui triangle can be calculated using a recursive formula.
Void initialyanghuitriangle (int * trianglearray, int N) // n is a parameter of the combination number. When Yang Hui's triangle is used, the number of rows is 1. {for (INT I = 1; I <= n + 1; ++ I) {for (Int J = 1; j <= I; ++ J) {If (j = 1)  (j = I)  (I = 1) {trianglearray [(I1) * I/2 + J1] = 1;} else {trianglearray [(I1) * I/2 + J1] = trianglearray [(I2) * (I1) /2 + J2] + trianglearray [(I2) * (I1)/2 + J1] ;}}}
Based on the above descriptions, the following procedure is obtained:
[Program]
# Include <iostream> using namespace STD; int yanghuitriangle [(1 + 25) * 25/2]; int summatearray (INT array [], int N); void initialyanghuitriangle (int * trianglearray, int N); int tempfunction (INT, Int, INT); int roundnumber (INT); int main () {int startnum; int finishnum; CIN> startnum> finishnum; initialyanghuitriangle (yanghuitriangle, 24); cout <roundnumber (finishnum)roundnumber (StartNum1) <Endl; return 0 ;}/** **************************************** * *********************************** Int roundnumber (int aNumber) * function: used to calculate the number of "round number" smaller than or equal to anumber * function input: A decimal integer anumber (0 <= anumber <= 20,000,000) * function output: number of "round number" less than or equal to anumber * function remarks: no ************************************** ***************************************/ int roundnumber (INT anumber) {int countnumber = 0; If (anumber = 0)  (anumber = 1) {re Turn 0;} int binarynumber [24]; int location = 0; while (anumber! = 1) {binarynumber [location] = anumber % 2; location = location + 1; anumber = anumber/2;} binarynumber [location] = 1; // In this way, the decimal integer is converted into a binary representation, which is placed in the array binarynumber [24]. // The next step is to judge the position of 1: int tempfirst1 = Location1; while (tempfirst1> 0) {countnumber + = tempfunction (tempfirst1,);  tempfirst1;} int next = Location1; // you already know location (> = 1) the corresponding position is 1, and next is used to record the next position int pre = location; while (next> = 0) {If (next = 0) // This indicates This is the last {If (binarynumber [next] = 1) // at this time, the PRE position is 1, and the next position is also 1 {countnumber + = tempfunction (next, preNext, 1); next= 1;} else // indicates that this number is the Binary Expression of anumber, you only need to verify whether the number is a round number. {If (2 * summatearray (binarynumber, location + 1) <= (location + 1) {countnumber + = 1 ;} next= 1 ;}} else {If (binarynumber [next] = 1) // The pre position is 1, the next position is also 1 {countnumber + = tempfunction (next, preNext, 1); Pre = next; next= 1 ;}else {next = 1 ;}}return countnumber;} int tempfunction (int K, int zeronum, int onenum) {If (k = 0) {return 0 ;} int temp = (K + zeronum + 1onenum) % 2 = 0 )? (K + zeronum + 1onenum)/2): (K + zeronumonenum)/2 + 1); // set the upper bound for count ("1 ") // return C (K, 0) + C (k, 1) +... + C (K, temp1) int sum = 0; If (temp <= 0) {return 0 ;}for (INT I = 0; I <temp; ++ I) {// sum = sum + yanghuitriangle [K * (k + 1)/2 + I];} return sum;} void initialyanghuitriangle (int * trianglearray, int N) // n is a parameter of the number of combinations. When Yang Hui triangle is used, the number of rows is 1. {for (INT I = 1; I <= n + 1; ++ I) {for (Int J = 1; j <= I; ++ J) {If (j = 1)  (j = I)  (I = 1) {trianglearray [(I1) * I/2 + J1] = 1;} else {trianglearray [(I1) * I/2 + J1] = trianglearray [(I2) * (I1) /2 + J2] + trianglearray [(I2) * (I1)/2 + J1] ;}}} /*************************************** * ************************************ int summatearray (INT array [], int N) * function: used to calculate all elements of the array and * function input: array with N length * function output: array element and * function remarks: no ************************************** ***************************************/ int summatearray (INT array [], int N) {int sum = 0; For (INT I = 0; I <n; ++ I) {sum = sum + array [I];} return sum ;}
Result: runtime error
[Modification] to be continued
This is a search from the internet. I found that the idea is similar, but I am not good at it.
#include<iostream>using namespace std;int C[33][33];int MAX[33];int B[33];int tobinary(int b){int i=1;while(b) { B[i++]=b%2; b/=2;} return i1;}void init(){ int i,j;C[1][0]=C[1][1]=1;for(i=2;i<=32;i++){ C[i][0]=1; for(j=1;j<=i;j++) C[i][j]=C[i1][j]+C[i1][j1]; }MAX[1]=0; for(i=2;i<=32;i++) {for(j=(i+1)/2;j<i;j++) MAX[i]+=C[i1][j]; }}int howmany(int n,int k,int *p) {if(k>=nk<0) return 0; if((n==1&&k==0)(n==2&&k==1)) return 1; if(p[n1]==0) return howmany(n1,k1,p);elsereturn C[n2][k1]+howmany(n1,k,p);}int compute(int n){int i,ans=0;int len=tobinary(n);for(i=(len+1)/2;i<len;i++)ans+=howmany(len,i,B);for(i=1;i<len;i++)ans+=MAX[i];return ans;}int main(){ init(); int start,finish;cin >> start >> finish; cout<<compute(finish)compute(start1)<<endl;return 0;}