题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6623
题目大意为求一个数的唯一分解的最小幂次。即120=23*31*51则答案为1。
因为数字太大不能直接分解,所以可以先分解1e4内的素因子,这样所有幂次可能>=5的数都被分解了,然后判断剩余的数是否是一个数的4次方如果是的话ans=min(ans,4),
如果不是的话再判断是不是一个数的3次方,如果是的话ans=min(ans,3)。
如果不是的话就判断是不是一个数的平方,如果是ans=min(ans,2),如果不是那么ans=1。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 using namespace std; 7 typedef long long ll; 8 const int maxn = 10000; 9 int a[10005], tot; 10 ll b[10005]; 11 void init() { 12 tot = 0; 13 for (int i = 2; i <= 10000; i++) 14 if (a[i] == 0) { 15 b[tot++] = (ll)i; 16 for (int j = i * 2; j <= 10000; j += i) 17 a[j] = 1; 18 } 19 } 20 int check(ll n) { 21 ll l = 1, r = pow(n*1.0, 1.0 / 3) + 1; 22 while (l <= r) { 23 ll mid = (l + r) >> 1; 24 ll y = mid * mid*mid; 25 if (y == n) return 1; 26 else if (y > n) r = mid - 1; 27 else l = mid + 1; 28 } 29 return 0; 30 } 31 int main() { 32 init(); 33 int t; 34 scanf("%d", &t); 35 while (t--) { 36 ll n; 37 scanf("%lld", &n); 38 int x, ans = 100; 39 for (int i = 0; i < tot; i++) { 40 if (n < b[i]) break; 41 x = 0; 42 if (n%b[i] == 0) { 43 while (n%b[i] == 0) { 44 n /= b[i]; 45 x++; 46 } 47 ans = min(ans, x); 48 } 49 } 50 if (n == 1 || ans == 1) printf("%d\n", ans); 51 else { 52 ll m1 = (ll)sqrt(sqrt(n*1.0)); 53 ll m2 = (ll)sqrt(n*1.0); 54 if ((m1*m1*m1*m1) == n) ans = min(ans, 4); 55 else if (check(n) == 1) ans = min(ans, 3); 56 else if (m2*m2 == n) ans = min(ans, 2); 57 else ans = 1; 58 printf("%d\n", ans); 59 } 60 } 61 return 0; 62 }