QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#64948#4229. GCD HarmonySa3tElSefr#TL 2519ms493092kbC++201.9kb2022-11-25 23:05:482022-11-25 23:05:51

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-25 23:05:51]
  • Judged
  • Verdict: TL
  • Time: 2519ms
  • Memory: 493092kb
  • [2022-11-25 23:05:48]
  • Submitted

answer

#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double


const int N = 5005 + 9, mod = 998244353;
const ll OO = 1e9;

int n, v[N];
bool factors[10005][10005], prime[10005];
ll dp[N][10005];
vector<int> g[N];

ll solve(int u, int p, int last) {
    auto &ret = dp[u][last];
    if (~ret) {
        return ret;
    }
    ret = OO;
    for (int i = 1; i <= 10000; ++i) {
        auto x = i;
        auto y = last;
        if (x > y) {
            swap(x, y);
        }
        if (last != 0 && !factors[x][y]) {
            continue;
        }
        ll cur = 0;
        if (i != v[u]) {
            cur += i;
        }
        for (auto &nxt: g[u]) {
            if (nxt != p) {
                cur += solve(nxt, u, i);
            }
        }
        ret = min(ret, cur);
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    fill(prime + 1, prime + 10001, 1);
    for (int i = 2; i <= 10000; ++i) {
        if (prime[i]) {
            vector<int> nums;
            for (int j = i; j <= 10000; j += i) {
                if (j != i) {
                    prime[j] = false;
                }
                nums.push_back(j);
            }
            for (int j = 0; j < nums.size(); ++j) {
                for (int k = j; k < nums.size(); ++k) {
                    factors[nums[j]][nums[k]] = true;
                }
            }
        }
    }
    memset(dp, -1, sizeof(dp));
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> v[i];
    }
    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    cout << solve(1, 0, 0) << '\n';
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1484ms
memory: 493076kb

input:

6
5
6
3
4
9
12
1 2
1 3
1 4
1 6
3 5

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 846ms
memory: 492604kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 2519ms
memory: 493092kb

input:

13
2
5
5
5
5
5
5
3
3
3
3
3
3
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13

output:

15

result:

ok single line: '15'

Test #4:

score: 0
Accepted
time: 1732ms
memory: 492480kb

input:

9
2
5
5
5
5
3
3
3
3
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9

output:

14

result:

ok single line: '14'

Test #5:

score: 0
Accepted
time: 1527ms
memory: 492388kb

input:

8
13
13
13
2
13
13
13
13
1 2
1 3
1 4
1 5
1 6
1 7
1 8

output:

13

result:

ok single line: '13'

Test #6:

score: -100
Time Limit Exceeded

input:

5000
59
25
51
26
33
99
2
13
29
58
5
47
96
83
63
22
69
89
41
90
20
97
85
90
55
17
54
75
54
24
90
54
74
78
81
77
2
47
68
18
60
81
99
86
7
6
76
43
17
43
92
25
36
26
47
100
63
66
2
16
47
25
48
86
24
2
10
42
44
80
92
48
49
21
49
40
32
85
53
88
15
30
4
27
77
16
6
80
50
56
46
14
3
48
92
50
93
35
69
29
48
4...

output:


result: