QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#90997 | #4229. GCD Harmony | thexin7 | WA | 5ms | 5932kb | C++14 | 1.8kb | 2023-03-26 14:55:27 | 2023-03-26 14:55:29 |
Judging History
answer
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int N = 5010;
int val[N];
vector<int> vec[N];
vector<int> p[110];
int f[N][110];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
void dfs(int u, int fa) {
int tmp[110];
memset(tmp, 0, sizeof(tmp));
for (int j = 0; j < vec[u].size(); j++) {
int v = vec[u][j];
if (v == fa) continue;
dfs(v, u);
for (int i = 2; i <= 100; i++) {
tmp[i] += f[v][i];
}
}
for (int i = 2; i <= 100; i++) {
for (int j = 2; j <= 100; j++) {
// 同时满足i j
// 与j具有相同因子 and 与 %i == 0
int cost = i * j / gcd(i, j);
if (val[u] % cost == 0) cost = 0;
f[u][i] = min(tmp[j] + cost, f[u][i]);
if (gcd(i, j) > 1) {
cost = i;
if (val[u] % cost == 0) cost = 0;
f[u][i] = min(tmp[j] + cost, f[u][i]);
}
for (int k = 0; k < p[j].size(); k++) {
cost = i * p[j][k];
if (val[u] % cost == 0) cost = 0;
f[u][i] = min(tmp[j] + cost, f[u][i]);
}
}
}
}
int main() {
memset(f, 0x3f, sizeof(f));
for (int i = 2; i <= 100; i++) {
int flag = 1;
int ti = i;
for (int j = 2; j < i; j++) {
if (ti % j == 0) {
flag = 0;
p[i].push_back(j);
while(ti%j==0){
ti/=j;
}
break;
}
}
}
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &val[i]);
}
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
vec[u].push_back(v);
vec[v].push_back(u);
}
dfs(1, 0);
int ans = 0x3f3f3f3f;
for (int i = 2; i <= 100; i++) {
ans = min(ans, f[1][i]);
}
printf("%d\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 5744kb
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: 5932kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: -100
Wrong Answer
time: 5ms
memory: 5712kb
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:
21
result:
wrong answer 1st lines differ - expected: '15', found: '21'