Programmer -- solving the affinity of less than 5 million

Source: Internet
Author: User

Prelude
In this chapter, apart from retaining the original string, array, and other interview questions, we will consciously cut off some interview questions about small and clever numbers, focusing on the clever ideas ", and "wonderful ". This chapter describes the key words of affinity number, "5 million" and "linear complexity ".

Section 1 affinity
Description:
Calculate the number of all affinity values less than 5 million
If the sum of all true factors of two numbers a and B and a is equal to B, and the sum of all true factors of B is equal to a, a and B are a pair of affinity numbers.
For example 220 and 2924 and.

Analysis:
First, we need to clarify what is affinity?

The affinity number problem was first discovered and studied by the bildagos School. When studying the law of numbers, they found two numbers with the following characteristics:
The true factors of 220 are: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110;
The true factors of 284 are: 1, 2, 4, 71, and 142.
And the two numbers are exactly the sum of the true factors of the other side (sum [I] indicates the sum of the true factors of the number I), that is
220 = 1 + 2 + 4 + 71 + 142 = sum [284],
284 = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = sum [220].
Sum [284] = 284, and sum [220] = 220, that is, sum [220] = sum [284] = sum [220] = sum [sum [284] = 284.

Is there any clue?

As shown above, considering that 1 is the factor of each integer, all factors out of the integer itself are called the "true factor" of this number ". If the sum of the two integers, each of which is exactly the same as the sum of the two numbers, then these two numbers constitute a pair of "affinity numbers" (for more information about affinity numbers, see this: http://t.cn/hesH09 ).

Solution:
After learning about affinity, let's take a step-by-step approach to solving the problem (most of the content below comes from the original saying of water ).

What is the first thought after seeing this question? Simulated search + pruning? Backtracking? What is the time complexity? Where bn is the pseudo affinity of an, that is, what is the sum of the true factors of an bn? At least 10 ^ 13. For computers that perform tens of millions of operations per second, this can be done in more than 1000 days, that is, within three years. If you are optimizing based on this base, you cannot get results within one day.
A good algorithm should solve this problem within half an hour. Of course there are many such algorithms. The time-saving method is to generate an adjoint array, that is, to change the space for time. However, the space cost is too large because of the large data size.
In the later algorithm, the companion array is still used, but because of the special nature of the question, it is only convenient and clever to use the subscript as the companion array to save time. At the same time, we will replace the Backtracking idea with the recursive idea (the time complexity of the preprocessing array is logN (harmonic series) * N, and the time complexity of scanning the array is linear O (N ). Therefore, the total time complexity is O (N * logN + N) (where logN is the harmonic level )).

Section 2 linear traversal of adjoint Arrays
Based on the above 3rd ideas, write the following code:

View plaincopy to clipboardprint?
// Solve the affinity Number Problem

// The first for and second for loops are logn (harmonic series) x N traversal, and the third for loop scans O (N ).
// Therefore, the total time complexity is O (n * logn) + O (n) = O (N * logN) (where logN is the harmonic level ).

// Description of the harmonic series in the first for and second for searches:
// For example, if you add 2 to a multiple of 2, it should be n/2, and 3 to a multiple of 3 should be n/3 ,...
// It is actually n * (1 + 1/2 + 1/3 + 1/4 +... 1/(n/2) = n * (harmonic series) = n * logn.

// Copyright @ Shang shanruoshui
// July, updated, 2011.05.24.
# Include <stdio. h>

Int sum [5000010]; // prevents cross-border attacks

Int main ()
{
Int I, j;
For (I = 0; I <= 5000000; I ++)
Sum [I] = 1; // 1 is the true factor of all numbers, so all are set to 1.

For (I = 2; I + I <= 5000000; I ++) // preprocessing. The preprocessing is logN (harmonic level) * N.
// @ Litaoye: The sum of the harmonic series 1/2 + 1/3 + 1/4 ...... is approximately ln (n ),
// So O (n * (1/2 + 1/3 + 1/4 ......)) = O (n * ln (n) = O (N * log (N )).
{
// The maximum true factor below 5000000 is not more than half of it
J = I + I; // because of the true factor, it cannot be regarded as itself, so it starts from 2 times
While (j <= 5000000)
{
// Add the I
Sum [j] + = I;
J + = I;
}
}

For (I = 220; I <= 5000000; I ++) // scan, O (N ).
{
// A traversal starts from 220 because the minimum values are 284 and 220.
If (sum [I]> I & sum [I] <= 5000000 & sum [sum [I] = I)
{
// Deduplicate and not cross-border, satisfying affinity
Printf ("% d", I, sum [I]);
}
}
Return 0;
}


 

Running result:

@ Shang shanruoshui:

1. What you may understand is not very clear. We create an array of 500 000, starting from 1 to 2 000, and add I to the position where each subscript is a multiple of I, so what do we get after the loop ends? It is an array similar to estola's prime number (of course, there is a real affinity number in it), and then you can easily find all the affinity numbers by just one traversal. Time complexity, linear.

2. We can clearly find that the ing of continuous data can be replaced by the characteristics of the array structure itself to save space. This is the art of data structure. In the backtracking of large-scale continuous data, we can convert it into a Recursive Generation Method and reverse thinking operation, which is the art of algorithms.

3. the smartest person to use the simplest things is clearer than the person who solves complicated problems with complicated methods.


Section 3 program construction and interpretation
Let me explain the principle of the above program in detail. OK. For example, if we want to calculate the number of affinity within 10, the solution is as follows:

Because the true factor of all numbers includes 1, first set 1 at the bottom of each number.

Take I = 2, 3, 4, 5 (I <= 10/2), where j corresponds to j = (4, 6, 8, 10), (6, 9 ), (8), (10) the location of each number.
Based on the position found by j, add the true factor I (I = 2, 3, 4, 5) under each number in j ).
The entire process, as shown in (for example, sum [6] = 1 + 2 + 3 = 6, sum [10] = 1 + 2 + 5 = 8 .):
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 1
2 2 2 2
3 3
4
5
And then traverse I from 220 to 5000000. After I traverse each number,
Add each real factor under the number corresponding to I to get a sum [I]. If sum [I] = a certain I 'and sum [I'] = I,
The two numbers I and I are a pair of affinity numbers.
I = 2; sum [4] + = 2, sum [6] + = 2, sum [8] + = 2, sum [10] + = 2, sum [12] + = 2...
I = 3, sum [6] + = 3, sum [9] + = 3...
......
When I = 220, sum [220] = 284, when I = 284, sum [284] = 220; that is, sum [220] = sum [sum [284] = 284,
It is concluded that 220 and 284 are a pair of affinity numbers. Therefore, the final output is 220, 284 ,...

Related Article

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.