QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#36087 | #4229. GCD Harmony | deneribeiro10 | WA | 7ms | 30104kb | C++ | 1.1kb | 2022-06-24 07:50:14 | 2022-06-24 07:50:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define endl '\n'
const int INF = 1e9;
const ll INFLL = 1e18;
const int MAX = 1e6+10;
const int MOD = 1000000007;
int gcd(int a, int b) {
return b ? gcd(b, a%b) : a;
}
int n;
vector<int> adj[MAX];
int v[MAX];
int dp[5050][111];
int DP(int node, int par, int val) {
int &ans = dp[node][val];
if(ans != -1) return ans;
ans = INF;
for(int i=2; i<=250; ++i) {
if(gcd(i, val) != 1 or par == 0) {
int sum = (i != v[node] ? i : 0);
for(int to : adj[node]) if(to != par) {
sum += DP(to, node, i);
}
ans = min(ans, sum);
}
}
return ans;
}
void test_case() {
cin >> n;
for(int i=1; i<=n; ++i) cin >> v[i];
for(int i=0; i<n-1; ++i) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dp, -1, sizeof dp);
cout << DP(1, 0, 0) << endl;
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
test_case();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 30104kb
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: -100
Wrong Answer
time: 7ms
memory: 29580kb
input:
3 1 2 3 3 1 2 3
output:
3
result:
wrong answer 1st lines differ - expected: '4', found: '3'