QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#626293#4229. GCD Harmonynickbelov#TL 3180ms7940kbC++202.7kb2024-10-10 02:28:042024-10-10 02:28:05

Judging History

This is the latest submission verdict.

  • [2024-10-10 02:28:05]
  • Judged
  • Verdict: TL
  • Time: 3180ms
  • Memory: 7940kb
  • [2024-10-10 02:28:04]
  • Submitted

answer

#include <vector>
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T>
ostream_iterator<T> oit(const string &s = " "){ return ostream_iterator<T>(cout,s.c_str()); }
inline auto rep(int l, int r) { return views::iota(min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }
inline auto per(int l, int r) { return rep(l, r) | views::reverse; }
inline auto per(int n) { return per(0, n); }
inline auto per1(int l, int r) { return per(l, r + 1); }
inline auto per1(int n) { return per(1, n + 1); }
#define A(a) begin(a),end(a)
inline auto len = ranges::ssize;

struct chash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
template<typename T, typename U> using pb_map = gp_hash_table<T, U, chash>;
template<typename T> using pb_set = gp_hash_table<T, null_type, chash>;
#define K first
#define V second

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vii = vector<vector<int>>;
typedef vector<ll> vll;
using pll = pair<ll,ll>;
using pii = pair<int,int>;

constexpr ll NN = 5000,M = 200; 

int dp[NN][M];
vi adj[NN];
int a[NN];
void dfs(int i,int p){
    if(p != -1) adj[i].erase(ranges::find(adj[i],p));
    for(int j : adj[i])
        dfs(j,i);
    
    for(int here : rep(2,M)){
        if(a[i]!=here) dp[i][here]=here;
        for(int j : adj[i]){
            auto &v = dp[j];
            int add = dp[j][here];

            for(int c : rep(2,M)) if(gcd(c,here)>1)
                add=min(add,dp[j][c]);
            
            dp[i][here]+=add;
        }
    }

}

void run()
{
    int n; cin >> n;
    for(int i : rep(n)) cin >> a[i];   
    for(int u,v; int i : rep(n-1)){
        cin >> u >> v,--u,--v;
        adj[u].push_back(v),adj[v].push_back(u);
    } dfs(0,-1);

    int ans = dp[0][2];
    for(int c : rep(2,M)) ans = min(ans,dp[0][c]);

    cout << ans << '\n';
}

int main()
{
    //KING OF THE WORLD...... U.W.T.B
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    run();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3588kb

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

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 4ms
memory: 3596kb

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: 3ms
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: 3ms
memory: 3592kb

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: 0
Accepted
time: 2955ms
memory: 7872kb

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:

4778

result:

ok single line: '4778'

Test #7:

score: 0
Accepted
time: 2949ms
memory: 7780kb

input:

5000
15
8
34
68
69
24
72
65
85
5
11
7
24
50
94
98
99
88
99
31
58
93
94
14
92
17
45
61
85
83
66
97
35
52
33
41
98
10
77
59
33
23
21
49
83
93
23
77
60
49
27
2
73
10
31
23
8
73
3
94
19
74
78
82
86
95
14
18
58
15
5
58
99
60
82
34
82
6
96
31
70
6
8
49
82
51
95
52
95
55
94
21
74
83
19
7
99
74
49
65
25
6
5...

output:

4733

result:

ok single line: '4733'

Test #8:

score: 0
Accepted
time: 2968ms
memory: 7912kb

input:

5000
15
15
14
18
19
15
13
18
16
17
16
18
16
16
17
16
19
17
16
19
15
13
17
15
15
14
15
16
16
14
18
15
19
16
18
15
14
13
15
15
13
19
18
15
17
14
15
14
17
16
13
13
19
16
18
19
14
19
17
13
19
14
15
14
15
13
13
17
18
15
17
17
16
16
18
19
19
16
13
16
18
15
15
17
16
15
14
16
13
18
13
18
19
18
15
13
13
14
1...

output:

5609

result:

ok single line: '5609'

Test #9:

score: 0
Accepted
time: 2945ms
memory: 7860kb

input:

5000
97
96
97
94
96
94
95
95
95
93
95
94
96
96
95
94
93
95
95
97
95
96
94
97
97
97
94
95
93
96
94
94
96
93
93
93
97
94
96
95
94
94
94
97
94
96
93
97
95
97
93
96
93
93
95
97
96
93
94
97
95
94
94
95
96
97
93
94
97
96
97
93
93
93
97
94
94
95
95
95
94
96
95
93
96
97
97
95
94
93
97
93
97
96
94
94
93
93
9...

output:

5497

result:

ok single line: '5497'

Test #10:

score: 0
Accepted
time: 2956ms
memory: 7816kb

input:

5000
79
71
73
97
79
61
61
97
67
61
89
73
67
79
83
97
61
61
61
83
97
83
71
79
97
97
97
71
89
71
83
61
73
89
73
73
73
67
61
97
71
73
89
71
83
67
71
67
73
73
97
79
67
79
89
73
97
97
73
67
61
71
73
83
67
73
71
61
79
61
73
73
89
79
71
89
83
67
71
97
67
71
89
83
71
61
79
89
79
97
83
89
83
79
71
79
89
61
6...

output:

10000

result:

ok single line: '10000'

Test #11:

score: 0
Accepted
time: 2968ms
memory: 7844kb

input:

5000
73
83
73
97
97
73
97
83
83
83
73
83
73
97
97
73
83
73
83
97
97
83
97
73
97
83
97
73
97
73
97
97
83
97
97
83
73
83
97
83
83
73
83
97
73
73
97
83
83
83
97
73
97
97
83
83
97
73
73
97
73
73
97
83
83
97
73
97
83
83
73
97
73
97
73
73
73
73
97
83
97
83
97
73
83
97
83
83
73
73
83
73
83
97
83
83
97
97
8...

output:

10000

result:

ok single line: '10000'

Test #12:

score: 0
Accepted
time: 2940ms
memory: 7848kb

input:

5000
5
5
7
2
2
3
2
7
2
5
5
3
7
7
3
3
7
5
3
3
3
5
2
7
5
2
2
2
3
7
3
5
3
2
3
7
2
2
5
3
7
2
3
7
3
2
7
3
2
5
3
3
5
2
7
5
3
3
5
5
5
5
2
3
2
7
7
2
5
3
3
2
5
7
5
5
7
5
3
5
7
2
7
7
2
7
5
7
5
2
5
7
2
7
2
5
7
2
7
7
5
5
7
7
5
5
3
3
3
7
3
5
7
7
7
7
3
5
3
3
7
5
3
3
2
5
5
3
3
2
7
2
5
7
2
3
2
7
5
7
7
2
7
5
2
3
2
7...

output:

7404

result:

ok single line: '7404'

Test #13:

score: 0
Accepted
time: 2560ms
memory: 7920kb

input:

5000
71
71
71
71
73
71
71
73
73
71
73
73
73
73
73
73
73
73
71
73
71
71
73
73
71
71
73
71
71
71
73
71
71
71
73
73
73
73
71
71
73
71
71
71
71
71
71
73
71
71
71
71
71
73
71
71
71
71
71
73
71
73
73
73
73
73
73
71
71
73
73
71
73
71
71
71
71
71
71
73
73
73
73
71
71
73
73
73
73
71
71
71
73
71
71
71
71
71
7...

output:

10000

result:

ok single line: '10000'

Test #14:

score: 0
Accepted
time: 1050ms
memory: 7812kb

input:

5000
3
5
5
3
5
2
5
3
2
3
5
5
2
5
2
2
2
3
2
5
5
3
3
5
2
2
3
5
2
2
3
3
2
2
2
3
5
5
2
5
2
5
2
3
5
3
3
5
2
3
5
3
3
5
5
5
5
3
3
5
3
2
2
2
3
3
3
5
3
2
5
2
3
2
5
3
2
2
3
2
3
2
5
5
2
2
5
3
2
2
3
5
3
3
2
2
3
5
2
2
3
2
5
3
2
3
3
2
5
5
3
5
5
5
5
3
3
5
3
5
5
3
2
3
5
5
3
3
3
2
3
3
5
3
3
3
2
2
2
3
3
5
2
3
3
3
2
5...

output:

2126

result:

ok single line: '2126'

Test #15:

score: 0
Accepted
time: 971ms
memory: 7816kb

input:

5000
26
17
19
19
11
19
17
11
26
17
19
17
26
19
11
19
17
11
26
26
11
26
11
11
19
11
17
17
26
19
17
11
26
17
17
11
17
19
11
17
11
26
11
17
11
26
19
17
11
17
19
26
11
19
26
26
17
11
19
17
11
17
17
17
11
26
26
17
19
11
11
19
17
26
19
19
17
19
11
17
11
11
19
17
11
11
11
26
11
17
19
17
26
11
26
17
19
11
1...

output:

5268

result:

ok single line: '5268'

Test #16:

score: 0
Accepted
time: 3180ms
memory: 7900kb

input:

5000
13
11
13
11
13
11
11
11
11
13
13
11
11
11
11
11
13
11
11
11
11
13
11
11
11
13
13
11
11
13
13
13
11
13
13
13
11
11
11
13
13
11
11
13
13
11
11
13
11
13
13
11
13
11
13
13
13
13
13
13
11
13
13
11
11
13
11
11
13
11
13
11
11
11
13
13
13
13
13
13
11
11
13
11
11
11
13
11
13
11
11
13
11
13
11
11
13
13
1...

output:

10000

result:

ok single line: '10000'

Test #17:

score: 0
Accepted
time: 2667ms
memory: 7840kb

input:

5000
3
2
5
5
2
2
5
2
2
3
2
3
2
2
2
5
5
5
2
2
5
2
5
2
3
3
3
2
3
2
5
2
3
2
5
2
3
2
5
2
5
3
5
5
5
3
2
5
5
3
2
2
3
5
3
5
2
2
2
2
5
5
5
2
5
2
2
5
2
2
3
2
3
2
2
5
2
5
2
3
3
3
5
5
3
5
3
2
5
5
3
3
2
3
2
2
2
3
2
3
2
3
2
2
2
5
2
5
5
2
2
3
3
2
3
2
2
5
2
2
3
5
3
3
5
2
2
5
5
3
2
3
3
3
3
5
3
3
5
3
5
2
3
2
3
2
3
2...

output:

6330

result:

ok single line: '6330'

Test #18:

score: 0
Accepted
time: 1023ms
memory: 7940kb

input:

5000
6
14
35
14
14
6
14
14
14
6
15
15
10
21
15
6
21
6
6
35
21
21
6
15
35
15
10
14
35
10
15
10
35
6
15
10
14
14
21
15
10
15
14
6
10
21
35
21
21
21
10
21
35
6
35
15
6
35
10
14
15
6
35
14
35
21
35
6
14
10
21
15
10
21
6
14
35
35
15
6
6
10
35
15
21
21
35
10
15
10
14
35
21
21
21
6
6
35
15
21
21
14
14
21
3...

output:

1666

result:

ok single line: '1666'

Test #19:

score: 0
Accepted
time: 965ms
memory: 7808kb

input:

5000
21
15
6
10
6
14
6
14
10
15
10
14
10
15
35
15
14
14
6
6
10
35
21
14
10
35
10
15
10
6
15
14
15
10
21
21
21
15
35
21
21
6
15
21
6
35
10
10
10
15
10
15
15
10
21
15
35
10
6
15
21
35
15
35
15
35
14
15
10
6
35
10
14
10
14
6
21
10
21
35
10
35
21
14
10
15
10
6
10
35
6
14
6
35
15
21
14
15
35
21
10
35
21
...

output:

510

result:

ok single line: '510'

Test #20:

score: -100
Time Limit Exceeded

input:

5000
5
1
1
1
1
5
1
3
1
1
3
1
1
1
3
3
1
1
1
1
3
1
3
1
1
1
5
3
5
1
3
1
1
1
3
3
1
1
5
1
1
1
5
5
3
5
1
1
1
1
5
1
1
1
5
5
1
1
1
1
1
3
3
1
5
1
5
1
1
3
3
1
1
3
1
3
1
1
1
1
1
1
1
3
1
5
3
3
3
1
3
1
1
5
1
1
1
1
3
3
3
5
5
5
5
3
1
5
5
5
1
3
1
1
1
1
1
5
3
1
3
1
3
3
1
3
3
1
1
5
3
3
3
3
3
5
1
1
1
3
1
1
5
1
1
5
3
1...

output:

9450

result: