201412022200-hd-Largest prime factor
Largest prime factorTime Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission (s): 7195 Accepted Submission (s): 2554
Problem DescriptionEverybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF (1) = 0.
InputEach line will contain one integer n (0 <n <1000000 ).
OutputOutput the LPF (n ).
Sample Input
12345
Sample Output
01213 the question is intended to give any number in 1--000000, and the maximum number of prime factors is the number of prime numbers. In order to avoid time-out, tables must be created. My original logic is to perform a step-by-step query. The first step is to set the prime number in Table 1--000000, and the second step is to set the prime number in Table 1--000000, the third part is the maximum quality factor of all numbers in Table 1--000000, and the two arrays are combined with the output result. It is a pity that this method is too troublesome and time-out. After thinking for a long time, I still used the tabulation method, which is similar to that of the prime number, but not because of the prime number, but if it is a prime number, then all of them and their multiples are marked by the position of this prime number. Because I is constantly changing, if it is 6, it is marked as 2 for the first time, and can be marked with 3 for replacement later. Code# Include
# Include
Int a [1100000]; int main () {int n, I, j, now; memset (a, 0, sizeof (a); // initialization, mark all of them as 0 a [1] = 0; now = 0; // mark the position, that is, the first few for (I = 2; I <= 1000000; I ++) {if (a [I] = 0) // if it is a prime number {now ++; for (j = I; j <= 1000000; j + = I) a [j] = now; // all the multiples in the range are marked with now,} // I is increasing, the maximum factor is constantly replaced. } While (scanf ("% d", & n )! = EOF) {printf ("% d \ n", a [n]) ;}return 0 ;}