http://codeforces.com/contest/450/problem/E
编号大的优先算入结果,质因数大的优先算入结果
1 public class Main {
2 static final int maxn = (int) 1e5 + 100;
3
4 static int n;
5 static int[] isPrime = new int[maxn];
6
7 public static void main(String[] args) {
8 IO io = new IO();
9 n = io.nextInt();
10 primeTable(n);
11
12 int[] g = new int[maxn];
13 int[] vis = new int[maxn];
14 ArrayList<int[]> ans = new ArrayList<>();
15
16 //质因数大的优先算入结果
17 for (int i = n/2; i>=2; i--) {
18 if (isPrime[i] == 0) continue;
19 g[0]=0;
20 for (int j = i; j <= n; j += i) if (vis[j] == 0) g[++g[0]] = j;
21 //编号大的优先算入结果,保留最小的数字,不能是第一个,因为第一个是素数,所以保留第二个
22 if (g[0]%2==1){int t=g[2];g[2]=g[g[0]];g[g[0]]=t;}
23 for (int j = 1; j + 1 <= g[0]; j += 2) {
24 ans.add(new int[]{g[j], g[j + 1]});
25 vis[g[j]] = vis[g[j + 1]] = 1;
26 }
27 }
28 io.println(ans.size());
29 for (int[]i:ans)io.println(i[0]+" "+i[1]);
30 }
31
32 static void primeTable(int n) {
33 Arrays.fill(isPrime, 1);
34 for (int i = 2; i * i <= n; i++) {
35 if (isPrime[i] == 0) continue;
36 for (int j = i * i; j <= n; j += i) isPrime[j] = 0;
37 }
38 }
39
40
41 }