-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDivisors_Sieve.cpp
More file actions
47 lines (39 loc) · 988 Bytes
/
Divisors_Sieve.cpp
File metadata and controls
47 lines (39 loc) · 988 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
// Find sum of divisor counts up to n, O(sqrt(n))
auto sum_of_divisor_counts = [](const int64_t x) -> int64_t {
int64_t res = 0, i = 1;
for (; i * i <= x; i++) {
res += x / i;
}
res = 2 * res - (i - 1) * (i - 1);
return res;
};
// Find divisors of numbers up to n, O(n*log(n))
vector<vector<int>> divs(n + 1);
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
divs[j].push_back(i);
}
}
cout << sum_of_divisor_counts(n) << '\n';
for (int i = 1; i <= n; i++) {
cout << '#' << i << '\n';
for (auto d : divs[i]) {
cout << d << " \n"[d == divs[i].back()];
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}