-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlargestPrimeDivisor_UVA11466.cpp
84 lines (71 loc) · 1.61 KB
/
largestPrimeDivisor_UVA11466.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("largestPrimeDivisor_UVA11466.in");
ofstream fout("largestPrimeDivisor_UVA11466.out");
const int N = 10000000;
int main()
{
vector<bool> isPrime(N + 1, true);
isPrime[0] = false, isPrime[1] = false;
vector<long long> primes;
for (long long i = 2; i <= N; ++i)
{
if (isPrime[i] == true)
{
primes.push_back(i);
for (long long j = i * i; j <= N; j += i)
{
isPrime[j] = false;
}
}
}
int size0 = primes.size();
while (true)
{
long long n = 0;
fin >> n;
if (n == 0) break;
if (n < 0) n = -1 * n;
long long _count = 0, ans = -1;
for (int i = 0; i <= size0 - 1; ++i)
{
if (n < primes[i])
{
break;
}
else if (n % primes[i] == 0)
{
ans = primes[i];
++_count;
while (n % primes[i] == 0)
{
n /= primes[i];
}
}
}
if (n > 1)
{
ans = n;
++_count;
}
if (_count > 1)
{
fout << ans << '\n';
}
else
{
fout << "-1\n";
}
}
return 0;
}