-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11688_math.cpp
More file actions
60 lines (59 loc) · 1 KB
/
11688_math.cpp
File metadata and controls
60 lines (59 loc) · 1 KB
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
51
52
53
54
55
56
57
58
59
60
// 210411 #11688 ÃÖ¼Ò°ø¹è¼ö ã±â Gold IV
// math: euclidean, eratosthenes_sieve
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int MAX = 1000001;
ll a, b, L;
bool isPrime[MAX];
vector<int> v;
void sieve(ll num) {
fill(isPrime, isPrime + MAX, true);
for (int i = 2; i*i <= num; i++) {
if (!isPrime[i])
continue;
for (int j = i * 2; j <= num; j += i) {
isPrime[j] = false;
}
}
}
ll gcd(ll a, ll b) {
ll r = a % b;
if (!r)
return b;
return gcd(b, r);
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> a >> b >> L;
ll k = gcd(a, b);
ll n = (a / k) * (b / k) * k;
if (L % n) {
cout << -1 << "\n";
return 0;
}
ll num = L / n;
sieve(num);
while (num != 1) {
for (int i = 2; i <= num; i++) {
if (num == 1)
break;
if (isPrime[i] && !(num % i)) {
v.push_back(i);
num /= i;
}
}
}
ll ans = 1;
for (auto i : v) {
while (!(L % i)) {
ans *= i;
L /= i;
}
}
cout << ans << "\n";
}