QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#510927 | #4229. GCD Harmony | RngBased# | WA | 3ms | 5888kb | C++20 | 1.2kb | 2024-08-09 14:13:33 | 2024-08-09 14:13:33 |
Judging History
answer
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
#define N 5005
using namespace std;
int n, dp[N][105], arr[N];
vector<int> g[N];
vector<int> factor[105];
void dfs(int p, int lp){
for (int i = 2; i <= 100; i++)
dp[p][i] = i;
dp[p][arr[p]] = 0;
for (auto u : g[p]){
if (u != lp){
dfs(u, p);
for (int i = 2; i <= 100; i++){
int mn = 1e9;
for (int j = i; j <= 100; j += i)
mn = min(mn, dp[u][j]);
for (auto j : factor[i])
mn = min(mn, dp[u][j]);
dp[p][i] += mn;
}
}
}
}
int main(){
for (int i = 2; i <= 100; i++)
for (int j = i; j <= 100; j += i)
factor[j].push_back(i);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> arr[i];
for (int i = 1; i < n; i++){
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 0);
int ans = 1e9;
for (int i = 2; i <= 100; i++)
ans = min(ans, dp[1][i]);
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3704kb
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: 3552kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3648kb
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: 0ms
memory: 5676kb
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: 3684kb
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
Wrong Answer
time: 3ms
memory: 5888kb
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:
6275
result:
wrong answer 1st lines differ - expected: '4778', found: '6275'