-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcf449c.cpp
More file actions
64 lines (59 loc) · 1.73 KB
/
cf449c.cpp
File metadata and controls
64 lines (59 loc) · 1.73 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
61
62
63
64
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
const int MAX_N = int(1e5) + 10;
bool isP[MAX_N];
int minP[MAX_N];
int n;
vector<int> pa, pb;
bool done[MAX_N];
int main() {
cin >> n;
for (int i = 2; i <= n; ++i) {
isP[i] = true;
minP[i] = i;
}
for (int i = 2; i <= n; ++i)
if (isP[i])
for (int j = i + i; j <= n; j += i)
isP[j] = false, minP[j] = min(minP[j], i);
for (int i = n; i >= 2; --i) {
if (isP[i]) {
vector<int> good, bad;
for (int j = i; j <= n; j += i) {
if (!done[j]) {
if (minP[j] == i) {
bad.push_back(j);
} else {
good.push_back(j);
}
}
}
if ((good.size() + bad.size()) % 2 == 0) {
vector<int> e = good;
e.insert(e.end(), bad.begin(), bad.end());
for (int i = 0; i + 1 < e.size(); i += 2) {
done[e[i]] = true;
done[e[i + 1]] = true;
pa.push_back(e[i]), pb.push_back(e[i + 1]);
}
} else {
vector<int> e = bad;
e.insert(e.end(), good.begin(), good.end());
for (int i = 0; i + 1 < e.size(); i += 2) {
done[e[i]] = true;
done[e[i + 1]] = true;
pa.push_back(e[i]), pb.push_back(e[i + 1]);
}
}
}
}
cout << pa.size() << endl;
for (int i = 0; i < pa.size(); ++i) {
cout << pa[i] << " " << pb[i] << endl;
}
}