-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem0124.cpp
69 lines (59 loc) · 1.48 KB
/
problem0124.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
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define MAX 100000
bool less_than(std::pair<int, int> x, std::pair<int, int> y) {
if (x.second < y.second)
return true;
if (x.second > y.second)
return false;
if (x.second == y.second)
return x.first < y.first;
}
int rad(int n, std::vector<int> p);
int next_prime(int p, bool s[]);
void del_mult(int p, bool s[]);
int main() {
bool sieve[MAX];
memset(sieve, true, sizeof(sieve));
sieve[0] = false;
sieve[1] = false;
int prime = 2;
for (int i = 0; i * i < MAX; ++i) {
del_mult(prime, sieve);
prime = next_prime(prime, sieve);
}
std::vector<int> primes;
for (int i = 0; i < MAX; ++i) {
if (sieve[i])
primes.push_back(i);
}
std::vector<std::pair<int, int>> pairs;
for (int i = 1; i <= 100000; ++i) {
pairs.push_back(std::make_pair(i, rad(i, primes)));
}
std::sort(pairs.begin(), pairs.end(), less_than);
std::cout << pairs[9999].first << std::endl;
return 0;
}
int rad(int n, std::vector<int> p) {
int prod = 1;
for (int i = 0; n > 1; ++i) {
if (n % p[i] == 0) {
prod *= p[i];
while (n % p[i] == 0) {
n /= p[i];
}
}
}
return prod;
}
int next_prime(int p, bool s[]) {
while (!s[++p]);
return p;
}
void del_mult(int p, bool s[]) {
for (int i = p * p; i < MAX; i += p)
s[i] = false;
}