-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathalmostperfect.cc
69 lines (63 loc) · 1.22 KB
/
almostperfect.cc
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
// https://open.kattis.com/problems/almostperfect
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <vector>
using namespace std;
typedef unordered_set<int> si;
typedef vector<int> vi;
typedef vector<si> vsi;
void primes(vi &p) {
p.push_back(2);
for (int i = 3; i <= 31623; i++) {
bool a = true;
for (auto x : p) {
if (x * x > i) break;
if (!(i % x)) {
a = false;
break;
}
}
if (a) p.push_back(i);
}
}
void factors(const vi& p, vi &f, int n) {
int i = 2;
for (auto x : p) {
while (!(n % x)) {
f.push_back(x);
n /= x;
}
}
if (n > 1) f.push_back(n);
}
void divisors(vsi &c, const vi &f, si &d, int n, int i, int k) {
if (i == f.size()) {
if (k < n) d.insert(k);
return;
}
if (c[i].count(k)) return;
c[i].insert(k);
divisors(c, f, d, n, i + 1, k);
divisors(c, f, d, n, i + 1, k * f[i]);
}
int main() {
vi p;
string l;
primes(p);
while (getline(cin, l)) {
stringstream in(l);
int n;
in >> n;
vi f;
factors(p, f, n);
si d;
vsi c(f.size());
divisors(c, f, d, n, 0, 1);
int s = 0;
for (auto k : d) s += k;
if (s == n) cout << n << " perfect\n";
else if (abs(s - n) <= 2) cout << n << " almost perfect\n";
else cout << n << " not perfect\n";
}
}