Programming beauty--1 Number __ Programming

Source: Internet
Author: User

This problem is the application of the typical analysis and inductive method. By analyzing each number of each bit to 1 by which factors, so summed up all the number of 1 of the occurrence of the law, very classic. There is no other better way of thinking, it may be aimed at the case analysis, to see whether the law can be found to status quo.

The detailed analysis summary, is not I words can express. Let's just read it directly. But there was a mistake in the book, drawn in the red line.

If the number on the hundred is 1, it can be seen that the number of 1 on the hundred is not only affected by the higher level, but also by the low impact, that is, the higher and lower the common decision. For example, for 12 113, the number of hundreds appearing 1 is 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199, altogether 1 200, as in the first case above, equal to the current digit (100) of the higher digits (x). But it is also affected by the low, 1 of the situation is 12 100~12 113, a total of 114, equal to the low number (123) +1. Should be 14, equal to the low number (13) +1.


The detailed analysis is as follows: "solution one of question 1"

This question does not seem to be a difficult problem, because it does not require much thinking, I think we can all find the simplest way to compute f (n), which is to iterate from 1 to N and add up the number of "1" in each number, and naturally get the number of "1" from 1 to N. The written procedure is as follows:

Code Listing 2-9

Ulonglong Count1inainteger (ulonglong N)

{

Ulonglong inum = 0;

while (n!= 0)

{

Inum + = (n% 10 = 1)? 1:0;

n/= 10;

}

return inum;

}

Ulonglong F (ulonglong N)

{

Ulonglong icount = 0;

for (Ulonglong i = 1; I <= n; i++)

{

Icount + + Count1inainteger (i);

}

Return icount;

}

This method is very simple, as long as you learn a little programming knowledge people can think, implementation is also very simple, easy to understand. But the fatal problem with this algorithm is efficiency, and its time complexity is

O (n) x calculates the complexity of the number of "1" in an integer number = O (n * log2 N)

If the given n is relatively large, it will take a long time to calculate the results. For example, on the author's machine, if a given N=100 000 000, then the calculation of f (n) will take about 40 seconds, the calculated time with the increase of N and linear growth.

It looks like all 1 of the numbers from 1 to n will be counted, at least through all the numbers between 1 and N. Can you find a quicker way to solve the problem? To improve efficiency, you must discard this method of traversing all numbers of 1 to N to compute f (n), and instead use a different approach to solve the problem.

"Solution two of question 1"

With a careful analysis of the problem, given N, it appears that the sum of the "number of times less than n" may appear on each of the 1 "and come to this result." Let's take a look at how to get a rule for a particular N to analyze all the possibilities of 1 on each one, and to sum the last f (n).

Start by looking at some simple situations and see if you can sum up the rules.

Let's look at the 1-digit number first.

If N = 3, then all numbers from 1 to 3 are: 1, 2, 3, only single-digit digits may appear 1, and only 1 times, further can be found if N is Single-digit, if n>=1, then f (n) is equal to 1, if n=0, then f (n) is 0.

Let's look at the 2-digit number.

If n=13, then all numbers from 1 to 13:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, single-digit and 10 digits may have 1, we can separate them to consider, a single-digit 1 times two: 1 and 11, 10 digits appear 1 times 4:10, 11, 12 and 13, so F (N) =2+4=6. Note that the number 11 in both 10 and a bit has appeared 1, but 11 is just in the Single-digit 1 and 10 in 1 is calculated two times, so no special treatment, is right. Then consider the situation of n=23, it and n=13 a bit different, 10-bit 1 times 10 times, from 10 to 19, Single-digit 1 of the number of times 1, 11 and 21, so F (N) =3+10=13. By analyzing the two digits, we found that 1 of the number of single-digit digits is not only related to single-digit digits, also related to the 10-digit number: If n's single-digit number is greater than or equal to 1, the number of digits appearing at 1 is 10 digits plus 1; if n is single-digit 0, a digit of 1 is equal to 10 digits in number. and the number of 1 on the 10-digit number is not only related to the 10-digit number, also related to single-digit digits: If the 10 digits equals 1, the number of 1 on the 10-digit number is single-digit plus 1, or 10 if the 1-digit number is greater than 10.

F (13) = The number of 1 in Single-digit + 10 bits appearing 1 = 2 + 4 = 6;

F (23) = The number of 1 in Single-digit + 10 bits appearing 1 = 3 + 10 = 13;

F (33) = The number of 1 in Single-digit + 10 bits appearing 1 = 4 + 10 = 14;

...

F (93) = the number of 1 in Single-digit + 10 bits appearing 1 = 10 + 10 = 20;

Then analyze the 3-digit number.

If n = 123:

The number of digits appearing at 1 is 13:1, 11, 21, ..., 91, 101, 111, 121

The number of 10-bit occurrences of 1 is 20:10~19, 110~119

The number of hundreds appearing 1 is 24:100~123

F (23) = Single-digit Number of 1 + 10 digits appearing 1 + hundred appearing 1 times = 13 + 20 + 24 = 57;

Similarly, we can analyze the 4-digit, 5-digit number again. Readers can write a summary of what is different.

Based on some of the previous attempts, we derive a general calculation of F (n) from N:

Suppose N=abcde, where a, B, C, D, and e are numbers on each digit of the decimal number n. If you want to calculate the number of 1 on the Hundred, it will be affected by three factors: the number on the hundred, the number below the Hundred (low), and the number above the Hundred (higher).

If the number on the hundred is 0, you can know that the number of hundreds of 1 can be determined by a higher level, such as 12 013, you can know that the hundreds of 1 may be 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199, a total of 1 200. That is, it is determined by a higher number (12) and is equal to the current number of digits (100) of the higher position number (x).

If the number on the hundred is 1, it can be seen that the number of 1 on the hundred is not only affected by the higher level, but also by the low impact, that is, the higher and lower the common decision. For example, for 12 113, the number of hundreds appearing 1 is 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199, altogether 1 200, as in the first case above, equal to the current digit (100) of the higher digits (x). But it is also affected by the low, 1 of the situation is 12 100~12 113, a total of 114, equal to the low number (123) +1.

If the number on the hundred is greater than 1 (that is, 2~9), the number of 1 that may occur on the hundred is determined only by a higher level, such as 12 213, and the probability of a hundred 1 is: 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199,12 199, a total of 1 300, and equal to the higher number +1 (12+1) x current digits (100).

By summarizing and summarizing the above, we can write the following more efficient algorithm to compute F (N):

Code Listing 2-10

Longlong Sum1s (ulonglong N)

{

Ulonglong icount = 0;

Ulonglong ifactor = 1;

Ulonglong ilowernum = 0;

Ulonglong icurrnum = 0;

Ulonglong ihighernum = 0;

while (n/ifactor!= 0)

{

Ilowernum = N-(n/ifactor) * ifactor;

Icurrnum = (n/ifactor)% 10;

Ihighernum = N/(Ifactor * 10);

Switch (icurrnum)

{

Case 0:

Icount + = Ihighernum * Ifactor;

Break

Case 1:

Icount + = Ihighernum * ifactor + ilowernum + 1;

Break

Default

Icount + + (Ihighernum + 1) * ifactor;

Break

}

Ifactor *= 10;

}

Return icount;

}

This method can get f (n) by parsing N, bypassing the traversal from 1 to N, and the time complexity of the number N of the input length len is O (Len), which is O (ln (n)/ln (10) +1). On the author's computer, calculate N=100 000 000, compared to the first method of 40 seconds, this algorithm can return results in less than 1 milliseconds, the speed of at least increased by 40 000 times times.
"solution to question 2"

To determine the maximum number N, satisfy the f (n) =n. We can know by simple analysis (modeled on the methods given above):

9 The following are: one;

99 The following are: 1x10+10x1=20;

999 the following are: 1x100+10x20=300;

9999 the following are: 1x1000+10x300=4000;

...

999999999 the following are: 900 million;

9999999999 below are: 10 billion.

It is easy to generalize from the above equation: f (10n-1) = n * 10n-1. Through this recursive type, it's easy to see when n = 9, the start value of f (n) is greater than n, so we can guess that when n is greater than a certain number n, f (n) is always larger than N, that is, the maximum satisfying condition is between 0~n, i.e. n is an upper bound of the maximum satisfying condition f (n) = N. If you can estimate this n, then just let N go from N to 0, each check for f (n) = n, and the first number that satisfies the condition is the integer we require.

Therefore, the problem is transformed into how to prove that the upper bound n does exist and estimates the upper bound N.

There is an upper bound to the number that satisfies the condition f (n) = n

First of all, we use a similar mathematical inductive method to infer this problem. It's easy to get the following conclusions (reader friends can try to enumerate them yourself):

When n increases 10 o'clock, f (n) increases at least 1;

When n increases by 100, f (n) increases at least 20;

When n increases by 1 000, F (n) increases at least 300;

When n increases by 10 000, F (n) increases at least 4 000;

......

When n increases by 10k, F (n) increases at least k*10k-1.

First, when k>=10, the k*10 k-1> is k, so the increase of F (n) is greater than N.

Secondly, F (1010-1) =1010>1010-1. If n is present, when n = N, when f (n)-n>1010-1 is established, the value of f (n) will always be greater than n regardless of how much n increases.

Specifically, the increment of N is m: when M is less than 1010-1, the value of f (n + M) > F (N) > N + 1010-1 > n + M, that is, f (n) is still greater than the value of n because of f (n)-n>1010-1; when M is greater than or equal to 1010-1 , the increment of f (n) is always greater than the increment of N, that is, f (n + M)-f (n) > (n+m)-N, that is, f (n + M) > F (N) + M > n + 1010-1+ m > N +m, the value of f (n) is still greater than the value of N.

Therefore, the n that satisfies the F (n)-n > 1010-1 must be an upper bound of the number being obtained.

Find the upper bound n

Also because f (1010-1) = n *1010-1, may be set n = 10k-1, there are f (K-1)-(K-1) > 1010-1, that is, k*10 K-1-(K-1) > 1010-1, easy to K > = 11 times are satisfied. So, when k = 11 o'clock, N=1011-1 is the smallest upper bound.

Calculate this maximum number n

Make n = 1011-1=99 999 999 999, so that n is decremented from N to 0, each checking for f (n) = n, and the first satisfying condition is the integer we require. It is easy to solve the N = 1 111 111 110 is the largest integer satisfying f (n) = N. extension Issues

For other forms of expression, you can also try to see what the rules are. For example, binary:

F (1) = 1

F (10) = 10 (Because 01, 10 has two 1)

F (11) = 100 (since 01, 10, 11 have four 1)

The reader friend can imitate our analysis method, give the corresponding

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.