QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#182758#6728. To the Parkucup-team004AC ✓50ms5292kbC++201.5kb2023-09-18 15:05:142023-09-18 15:05:14

Judging History

你现在查看的是最新测评结果

  • [2023-09-18 15:05:14]
  • 评测
  • 测评结果:AC
  • 用时:50ms
  • 内存:5292kb
  • [2023-09-18 15:05:14]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
std::vector<int> minp, primes;

void sieve(int n) {
    minp.assign(n + 1, 0);
    primes.clear();
    
    for (int i = 2; i <= n; i++) {
        if (minp[i] == 0) {
            minp[i] = i;
            primes.push_back(i);
        }
        
        for (auto p : primes) {
            if (i * p > n) {
                break;
            }
            minp[i * p] = p;
            if (p == minp[i]) {
                break;
            }
        }
    }
}

void solve() {
    int n;
    std::cin >> n;
    
    sieve(n);
    
    std::vector<std::pair<int, int>> ans;
    std::vector<int> f(n + 1);
    for (int i = 3; i <= n; i += 2) {
        int p = minp[i];
        if (f[p] == 0) {
            f[p] = i;
        } else {
            ans.emplace_back(f[p], i);
            f[p] = 0;
        }
    }
    std::vector<int> even;
    for (int i = 1; i <= n / 2; i++) {
        if (f[i] != 0) {
            ans.emplace_back(f[i], 2 * i);
        } else {
            even.push_back(2 * i);
        }
    }
    for (int i = 1; i < even.size(); i += 2) {
        ans.emplace_back(even[i - 1], even[i]);
    }
    std::cout << ans.size();
    for (auto [x, y] : ans) {
        std::cout << " " << x << " " << y;
    }
    std::cout << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3844kb

input:

3
1
4
6

output:

0
1 2 4
2 3 6 2 4

result:

ok 4 cases

Test #2:

score: 0
Accepted
time: 50ms
memory: 5292kb

input:

1007
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101...

output:

0
0
0
1 2 4
1 2 4
2 3 6 2 4
2 3 6 2 4
2 3 6 2 4
3 3 9 2 4 6 8
4 3 9 5 10 2 4 6 8
4 3 9 5 10 2 4 6 8
4 3 9 5 10 2 4 6 8
4 3 9 5 10 2 4 6 8
5 3 9 5 10 7 14 2 4 6 8
6 3 9 15 6 5 10 7 14 2 4 8 12
6 3 9 15 6 5 10 7 14 2 4 8 12
6 3 9 15 6 5 10 7 14 2 4 8 12
7 3 9 15 6 5 10 7 14 2 4 8 12 16 18
7 3 9 15 6 5...

result:

ok 1008 cases