Perfect PTH powers
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:16746 |
|
Accepted:3799 |
Description
We say that X is a perfect square if, for some integer B, x = B2. Similarly, X is a perfect cube if, for some integer B, x = B3. more generally, X is a perfect PTH power if, for some integer B, x = bp. given an integer x you are to determine the largest p Such that X is a perfect P
ThPower.
Input
Each test case is given by a line of input containing X. the value of X will have magn=at least 2 and be within the range of A (32-bit) int in C, C ++, and Java. A line containing 0 follows the last test case.
Output
For each test case, output a line giving the largest integer p Such that X is a perfect P
ThPower.
Sample Input
171073741824250
Sample output
1302
Q: an integer x is given, and X is written as X = a ^ P. What is the maximum value of P?
Analysis: X is used to decompose the prime factor. x = A1 ^ B1 * A2 ^ B2... AK ^ BK, the final result is b1, b2 ,... BK. Note that X may be a negative number.
If X is a negative number, divide the obtained answer by 2 until the result is an odd number, because the power of an even number cannot be a negative number.
# Include <cstdio> # include <cmath> # include <cstring> const int n = 66700; Int Is [N]; int prime [7000], prime_cnt; void get_prime () {for (INT I = 0; I <n; I ++) is [I] = 1; is [0] = is [1] = 0; prime_cnt = 0; int M = (INT) SQRT (n + 0.5); For (INT I = 2; I <n; I ++) {If (is [I]) {Prime [prime_cnt ++] = I; if (I <= m) {for (Int J = I * I; j <n; j + = I) is [J] = 0 ;}}} int gcd (int A, int B) {if (a <B) Return gcd (B, A); If (B = 0) return a; return gcd (B, A % B);} int main () {get_prime (); long N; // I don't know why TLE while (~ Scanf ("% LLD", & N) {int flag = 0; If (n <0) {flag = 1; n =-N ;} int ans = 0; For (INT I = 0; I <prime_cnt & n> 1; I ++) {If (N % prime [I] = 0) {int CNT = 0; while (N % prime [I] = 0) {n/= prime [I]; CNT ++;} ans = gcd (ANS, CNT) ;}} if (n> 1) ans = gcd (ANS, 1); If (FLAG) {While (ANS % 2 = 0) ans/= 2 ;} printf ("% d \ n", ANS);} return 0 ;}
Poj 1730 perfect PTH powers (decomposition prime factor)