-
Notifications
You must be signed in to change notification settings - Fork 6
/
perfectpowers.cpp
50 lines (43 loc) · 1.04 KB
/
perfectpowers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
while (true) {
long n;
bool negative = false;
map<long,int> factormap;
cin >> n;
if (n == 0) {
break;
} else if (n*n == 1) {
cout << 31 << endl;
continue;
} else if (n < -1) {
negative = true;
n = -n;
}
long i = 2;
while (i*i <= n) {
if (n % i == 0) {
n /= i;
factormap[i]++;
} else {
i++;
}
}
factormap[n]++;
int ans = 100;
for ( const auto &p : factormap ) {
ans = min(ans,p.second);
ans = __gcd(ans,p.second);
}
int ans2 = ans;
while (ans2 % 2 == 0) {
ans2 /= 2;
}
cout << ((negative && (ans % 2 == 0)) ? ans2 : ans) << endl;
}
return 0;
}