(reproduced) inverse prime numbers -- mathematics

Original post address: https://www.cnblogs.com/liuweimingcprogram/p/5877411.html

An inverse prime is the number with the largest number of divisors in the interval.

In the ACM topic,

Generally, it is the number with the largest number and the smallest number, [1--n]

The second is to find the smallest number whose divisor is exactly equal to n

The third is to find the smallest inverse prime number in the interval [beign, end]

Is there a difference between 1 and 3? Yes, 1 can accelerate, 3 can only violent

Let's talk about ideas

Ideas: Official problem solution: 

(1) The easiest thing to think of for this question is exhaustion, but it must be timed out.

(2) We can know that the number of calculated divisors is closely related to the prime factorization: If the prime factorization of Q is: Q=p1^k1*p2^k2*…*pm^km (p1…pm is a prime number, k1...km≥1), then Q has (k1+1)(k2+1)...(km+1) divisors. But the time complexity of prime factorization is very high, so it also times out.

(3) Through the above formula, we can "improvise": why can't the process of prime factorization be reversed? The algorithm is to enumerate every prime number. Initially let m=1, and then enumerate from the smallest prime number 2. The enumeration factor contains 0 2s, 1 2, 2 2...k 2s, until m*2^k is greater than the upper limit N of the interval. On this basis, enumerate the cases of 3, 5, 7, etc., calculate the number of divisors of m that have been obtained, and compare and replace the original records at the same time. until all cases have been judged. Optimization of this algorithm: If p1*p2*p3*...*pk>N (pi represents the ith prime number), then as long as p k-1 is enumerated, it will neither waste time nor miss it.

(4) The above algorithm is not the best, and can continue to be optimized. Let's look at the following example: 6=2*3 10=2*5 The prime factorization "patterns" of 6 and 10 are exactly the same, so their divisors are the same. But since 3<5, 6<10. 12=2^2*3 18=3^2*2 The prime factorization "modes" of 12 and 18 are exactly the same, so their divisors are the same. But since the exponent of 2 in the prime factorization of 12 is greater than the exponent of 3, and the exponent of 3 in the prime factorization of 18 is greater than the exponent of 2, 12<18. According to the above example, we can also make an improvement to the algorithm in (3): we can perform an optimization during enumeration, so that the exponent of 2 in the enumerated numbers is not less than the exponent of 3, and the exponent of 3 is not less than 5. The exponent of . . . so that we can get the smallest number with the same "pattern" of prime factorization (proof omitted). Then compare and record each of the obtained numbers. The optimization of this algorithm is extremely strong, and the efficiency has almost reached its limit.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326529709&siteId=291194637