Test instructions
Enter N to find at least two positive integers, so that the least common multiple of these numbers is n and minimum.
Analysis:
The decomposition of the n is, apparently alone, as an item, and the smallest.
Here are two tips:
- Starting from 2, the constant addition of n, until it cannot be divisible. This eliminates the problem of prime judgment and shortens the amount of code. Because the first 2 of all n is out of the factor, then there will be no n 4 = = 0 of the case, so that all except n is a prime number.
- From 2 in addition to n until sqrt (n), if n is not 1, then the "remaining" is at this time n the largest mass factor. Reduce the number of cycles.
1#include <cstdio>2#include <cmath>3 4typedefLong LongLL;5 6 intMainvoid)7 {8 intN, Kase =0;9 while(SCANF ("%d", &n) = =1&&N)Ten { OneLL ans =0; A intm = sqrt (n +0.5); - intpcnt =0; - the if(n = =1) - { -printf"Case %d:2\n", ++Kase); - Continue; + } - + for(inti =2; I <= m; ++i) A { at if(n% i = =0) - { -pcnt++; - inttemp =1; - while(n% i = =0) - { inN/=i; -Temp *=i; to } + if(Temp >1) ans + =temp; - } the } * if(N >1) $ {Panax Notoginsengpcnt++; -Ans + =N; the } + if(Pcnt <=1) ans++; A theprintf"Case %d:%lld\n", ++Kase, ans); + - } $ $ return 0; -}
code June
UVa 10791 (unique decomposition) Minimum Sum LCM