Question Link
Question: Give you an N, let you find the number with the largest number of factors in the number less than or equal to N, and the number of output factors. If there are multiple answers, the one with the smallest output number
Idea: official question:
(1) The question is most likely to be exhaustive, but it must have timed out.
(2) We can know that the number of calculation approx. is closely related to the prime factor decomposition: If Q is divided into 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) approx. However, the time for prime factor decomposition is very complex, so it also times out.
(3) through the above formula, we can "Create a whim": Why can't we reverse the process of Factorization the prime factor? This algorithm is used to enumerate every prime number. Let M = 1 at the beginning, and then start enumeration from the smallest prime number 2. The enumerated factor contains 0 2, 1 2, 2... K 2 until M * 2 ^ K is greater than the upper limit N of the range. On this basis, enumerate 3, 5, 7 ...... And then compare and replace the existing records. Until all the cases have been determined. Optimization of this algorithm: If P1 * P2 * P3 *...... * PK> N (PI indicates the I-th prime number), so as long as the enumeration to p k-1, no waste of time, will not miss.
(4) The above algorithms are not the best and can be further optimized. Let's take a look at the following example: 6 = 2*3 10 = 2*5 6 and 10, the prime factor decomposition "pattern" is exactly the same, so their approximate numbers are the same. However, because 3 <5, 6 <10. 12 = 2 ^ 2*3 18 = 3 ^ 2*2 12 and 18 the prime factor decomposition "pattern" is exactly the same, so their approximate numbers are the same. However, because the 2 index in the prime factor decomposition of 12 is greater than the 3 index, the 3 index in the 18 prime factor decomposition is greater than the 2 Index, so 12 <18. Based on the above example, we can also improve the algorithm in (3): We can perform an optimization during enumeration, so that the index of 2 in the enumerated number is not less than 3, and the index of 3 is not less than 5 ...... In this way, we can obtain the minimum number (slightly) with the same prime factor decomposition "pattern ). Then compare and record each obtained number. The optimization of this algorithm is very powerful, and the efficiency has almost reached the limit.
1 # include <stdio. h> 2 # include <string. h> 3 # include <iostream> 4 # define ll long 5 using namespace STD; 6 7 ll n, minnum, CNT; 8 const int prime [20] = {1, 2, ,}; 9 10 // num: the number of the enumeration, K: the prime factor of the K; cntt: the approximate number of such numbers; maxxcnt: the maximum number of quality factors; 11 void DFS (LL num, ll K, ll cntt, int maxxcnt) 12 {13 if (k> = 16) return; 14 15 // if there are more or the same number of approx., update the optimal solution to the current number; 16 if (cntt> CNT | (cntt = CNT & num <minnum) 17 {18 CNT = cntt; 19 minnum = num; 20} 21 ll temp = num; 22 For (ll I = 1; I <= maxxcnt; I ++) // start to enumerate the number of each prime factor; 23 {24 if (temp> N/prime [k]) 25 break; 26 temp * = prime [k]; // ride to the current number; 27 DFS (temp, k + 1, cntt * (I + 1), I); 28} 29} 30 int main () 31 {32 int t; 33 scanf ("% d ", & T); 34 while (t --) 35 {36 scanf ("% i64d", & N); 37 minnum = CNT = 1; 38 DFS (1, 1, 50 ); 39 printf ("% i64d % i64d \ n", minnum, CNT); 40} 41 return 0; 42}
View code
Introduction to anti-prime number
Ural 1748. The most complex number (Anti-prime number)