QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#64882#4229. GCD HarmonyThree_Sannin#TL 2ms3788kbC++142.3kb2022-11-25 20:23:442022-11-25 20:23:46

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-25 20:23:46]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:3788kb
  • [2022-11-25 20:23:44]
  • 提交

answer

// Hey there.. I love Eddard <3
 
//#pragma GCC optimize ("Ofast, no-stack-protector, unroll-loops, fast-math, O3")
 
#include <bits/stdc++.h>
 
using namespace std;
 
void fileIO () {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
}
 
void fastIO () {
    std::ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
}
 
int main_ ();
 
int main () {
    fastIO ();
    //fileIO ();
    int t = 1;
    //cin >> t;
    while(t--) {
        main_ ();
    }
    return 0;
}
 
const int N = 5e3 + 5;

vector<int> f[N*2];

int MAX;

int n, val[N];
int u, v;
vector<int> adj[N];

bool gcd(int a, int b){
    if(a==0 || b==0){
        return 1;
    }
    if((a&1)==0 && (b&1)==0){
        return 1;
    }
    int i = 0, j = 0;
    while(i < f[a].size() && j < f[b].size()){
        if(f[a][i] < f[b][j]){
            ++i;
        }
        else if(f[a][i] > f[b][j]){
            ++j;
        }
        else{
            return 1;
        }
    }
    return 0;
}
int dfs(int node, int par, int g){
    int ans = MAX;
    if(gcd(g, val[node])){
        int cur = 0;
        for(int ch:adj[node]){
            if(ch!=par){
                cur += dfs(ch,node,val[node]);
            }
        }
        ans = min(ans, cur);
    }
    
    for(int i=2;i<ans;++i){
        if(!gcd(g,i)){
            continue;
        }
        if(i == val[node]){
            continue;
        }
        int cur = i;
        for(int ch:adj[node]){
            if(ch!=par){
                cur += dfs(ch,node,i);
            }
        }
        ans = min(ans, cur);
    }
    return ans;
}

vector<int> factorize(int x){
    vector<int> factors ;
    while(x%2 == 0){
        x /= 2;
    }
    for(int i = 3; i * i <= x; i += 2){
        if(x%i==0){
            factors.push_back(i);
            while(x%i==0){
                x /= i;
            }
        }
    }
    if(x>2){
        factors.push_back(x);
    }
    return factors ;
}

int main_ () {
    cin >> n;
    MAX = n * 2;
    for(int i=3;i<=MAX;++i){
        f[i] = factorize(i);
    }
    for(int i=1;i<=n;++i){
        cin >> val[i];
    }
    for(int i=1;i<n;++i){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    cout << dfs(1,1,0) << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3696kb

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: 0ms
memory: 3788kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3724kb

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: 2ms
memory: 3664kb

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: 0ms
memory: 3728kb

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: