QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#104883 | #4229. GCD Harmony | Yanagi_Origami# | WA | 2ms | 3956kb | C++20 | 1.9kb | 2023-05-12 09:54:37 | 2023-05-12 09:54:40 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
int n,a[5005],f[5005][30],pri[55],cnt,sz[5005];
vector<int> e[5005],tb[10005],b;
void dfs(int p,int fa){
sz[p]=1;
for(auto v:e[p]){
if(v==fa) continue;
dfs(v,p);
sz[p]+=sz[v];
}
vector<int> g(2*n+5);
for(auto w: b) g[w]=(gcd(a[p],w)>=2)?w:0;
for(auto v:e[p]){
for(auto w: b){
int res = w*sz[v];
for(auto k: tb[w]){
res=min(res,f[v][k]);
}
g[w]+=res;
}
}
rep(j,1,cnt){
f[p][j]=sz[p]*pri[j];
}
for(auto w: b){
rep(j,0,(int)(tb[w].size()-1)){
f[p][j] = min(f[p][j],g[w]);
}
}
//printf("%d:\n",p);
//rep(i,1,cnt) printf("@%d : %d\n",pri[i],f[p][i]);
}
int main(){
scanf("%d",&n);
rep(i,1,n) scanf("%d",a+i);
rep(i,2,n){
int u,v; scanf("%d%d",&u,&v);
e[u].push_back(v);
e[v].push_back(u);
}
rep(i,2,100){
bool fl=1;
rep(j,2,i-1) if(i%j==0){
fl=0; break;
}
if(fl){
pri[++cnt]=i;
}
}
rep(i,2,2*n){
bool fl=1;
int w=i;
rep(j,1,cnt){
if(i%(pri[j]*pri[j])==0){
fl=0; break;
}
while(w%pri[j]==0) w=w/pri[j];
}
if(!fl) continue;
if(w!=1) continue;
b.push_back(i);
rep(j,1,cnt){
if(i%pri[j]==0) tb[i].push_back(j);
}
}
//for(auto w:b) printf("%d ",w); puts("");
dfs(1,0);
int ans=2*n;
rep(i,1,cnt) ans=min(ans,f[1][i]);
printf("%d\n",ans);
}
/*
6
5
6
3
4
9
12
1 2
1 3
1 4
1 6
3 5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3956kb
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: 3952kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 3956kb
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:
6
result:
wrong answer 1st lines differ - expected: '15', found: '6'