fork(1) download
  1. //============================================================================
  2. // Name : uva_10622.cpp
  3. // Author : Moaz Rashad
  4. // Version :
  5. // Copyright :
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <algorithm>
  11. #include <math.h>
  12. using namespace std;
  13.  
  14. int getValidPow(int i, long n, bool isNegative) {
  15. int count = 0;
  16. unsigned long long num = i;
  17. while (num <= n) {
  18. count++;
  19. if (num == n) {
  20. if (!isNegative || (isNegative && (count & 1)))
  21. return count;
  22. return -1;
  23. }
  24. num *= i;
  25. }
  26. return -1;
  27. }
  28. int getPerfectPower(long n, bool isNegative) { // sqrt(n)
  29. unsigned long long i = 2;
  30. int maxValidPow = 1;
  31. for (; i * i <= n; i++) {
  32. if (n % i == 0) {
  33. maxValidPow = max(getValidPow(i, n, isNegative), maxValidPow);
  34. }
  35. }
  36. return maxValidPow;
  37. }
  38.  
  39. int main() {
  40. long n;
  41. while (cin >> n) {
  42. if (n == 0)
  43. break;
  44. int res = getPerfectPower(abs(n), (n < 0));
  45. cout << res << endl;
  46. }
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5292KB
stdin
-27
0
stdout
3