QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#800366 | #4229. GCD Harmony | zeyu# | RE | 0ms | 0kb | C++23 | 3.0kb | 2024-12-06 09:37:41 | 2024-12-06 09:37:41 |
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pl pair<ll, ll>
#define pi pair<int, int>
#define minpq priority_queue<ll, vector<ll>, greater<ll>>
using namespace std;
#if 1
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '['; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "]";}
void _print() {cerr << endl << flush;}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "*["<<__LINE__<<"]\t"<< #x << " = "; _print(x)
#endif
const ll mod = 1e9 + 7;
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
ll gcd(ll a, ll b) {if(b == 0){return a;} return gcd(b, a % b);}
const int maxn = 5001, maxv = 8001;
int p[maxn];
void sieve(){
for (int i = 0; i < maxn; i ++) p[i] = i;
for (int i = 2; i < maxn; i ++){
if (p[i] == i){
for (int j = i + i; j < maxn; j += i){
p[j] = i;
}
}
}
}
int f(int x, int y){
return x * maxv + y;
}
int dp[maxn * maxv];
vector<int> graph[maxn];
void dfs(int x, int parent){
for (int to : graph[x]){
if (to == parent) continue;
dfs(to, x);
for (int j = 2; j < maxv; j ++){
if (p[j] == j){
for (int k = j; k < maxv; k += j){
chkmin(dp[f(to, j)], dp[f(to, k)]);
}
}
}
for (int j = 2; j < maxv; j ++){
int cur = j, minimum = 1 << 30;
while(cur > 1){
chkmin(minimum, dp[f(to, p[cur])]);
cur /= p[cur];
}
dp[f(x, j)] += minimum;
}
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
sieve();
int n; cin >> n;
vector<int> a(n); for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < n - 1; i ++){
int x, y; cin >> x >> y; x --; y --;
graph[x].push_back(y);
graph[y].push_back(x);
}
for (int i = 0; i < n; i ++){
for (int j = 2; j < maxv; j ++){
if (a[i] == j) dp[f(i, j)] = 0;
else dp[f(i, j)] = j;
}
}
dfs(0, 0);
int ans = 1 << 30;
for (int i = 2; i < maxv; i ++) chkmin(ans, dp[f(0, i)]);
cout << ans << '\n';
}
/*
Do something instead of nothing!
Print out stuffs / study samples when you cannot find the bug!
*/
详细
Test #1:
score: 0
Runtime Error
input:
6 5 6 3 4 9 12 1 2 1 3 1 4 1 6 3 5