QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#100160 | #4229. GCD Harmony | RoomGamadan# | WA | 38ms | 199716kb | C++23 | 2.4kb | 2023-04-24 20:35:49 | 2023-04-24 20:35:51 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define el '\n'
#define F first
#define S second
typedef long long ll;
typedef long double ld;
bool multipleTestCases = 0, sublime = 1;
const int N = 1e4 + 5, INF = 1e9 + 5, mod = 1e9 + 7, LOG = 22, SQ = 500;
int n, a[N], dp[N >> 1][N];
vector<int> g[N], primes[N], initPrimes;
void pre() {
for (int i = 2; i < N; i++) {
if (!primes[i].size()) {
if (i < 100) {
initPrimes.push_back(i);
}
for (int j = i; j < N; j += i) {
primes[j].push_back(i);
}
}
}
}
int solve(int node, int par, int val) {
int &ret = dp[node][val];
if (~ret) {
return ret;
}
ret = 0;
// shof 3yalak
for (auto &ch : g[node]) {
if (ch == par) {
continue;
}
int cur = INF;
for (auto &p : primes[val]) {
if (a[ch] % p == 0) {
cur = min(cur, solve(ch, node, a[ch]));
}
else {
cur = min(cur, solve(ch, node, p) + p);
}
}
ret += cur;
}
// tawar mn zatak
for (auto &p : initPrimes) {
if (val % p == 0 or val * p > N) {
continue;
}
ret = min(ret, solve(node, par, val * p) + (a[node] == val ? val * p : val * p - val));
}
return ret;
}
void doWork() {
cin >> n;
pre();
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
memset(dp, -1, sizeof dp);
int ans = 2 * n;
for (auto &i : initPrimes) {
if (a[1] % i == 0) {
ans = min(ans, solve(1, 1, a[1]));
}
else {
ans = min(ans, solve(1, 1, i) + i);
}
}
cout << ans << el;
}
signed main() {
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#endif
#ifndef ONLINE_JUDGE
if (sublime) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
#endif
int tests = 1;
if (multipleTestCases) {
cin >> tests;
}
pre();
for (int tc = 1; tc <= tests; tc++) {
doWork();
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 38ms
memory: 199716kb
input:
6 5 6 3 4 9 12 1 2 1 3 1 4 1 6 3 5
output:
-294957271
result:
wrong answer 1st lines differ - expected: '6', found: '-294957271'