QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#55393 | #4229. GCD Harmony | Kieray# | TL | 226ms | 3832kb | C++ | 1.5kb | 2022-10-13 15:30:24 | 2022-10-13 15:30:24 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<vector>
#include<numeric>
#include<cmath>
using namespace std;
#define ll long long
#define ge getchar
#define pun putchar('\n')
#define pu putchar
#define puk putchar(' ')
#define pb push_back
typedef vector<int> vint;
typedef array<int,2> pii;
const int nn=5006,mm=nn*2;
int n,a[nn],sx,p[25]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int z[nn],x[mm],t[mm];
int ss=0,pr[1400];
void ff(int n,int i){
if(n>10000)return;
if(i==25){
//cout<<++ss<<' '<<n<<endl;
if(n>1)pr[++ss]=n;
return;
}
ff(n,i+1);
ff(n*p[i],i+1);
}
vector<pii> dfs(int u,int fu){
vector<pii> f;
for(int k=1;k<=ss;k++)f.pb({pr[k],(a[u]==pr[k])?0:pr[k]});
for(int i=z[u];i;i=x[i]){
int v=t[i];
if(v!=fu){
vector<pii> g=dfs(v,u),r;
for(auto [x,xx]:f){
int zx=sx+1;
for(auto [y,yy]:g)if(__gcd(x,y)>1){
zx=min(zx,yy);
}
if(xx+zx<=sx)r.pb({x,xx+zx});
}
f=r;
}
}
return f;
}
int main(){
ff(1,0);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
for(int j=0;j<25;j++)while(a[i]/p[j]/p[j]*p[j]*p[j]==a[i])a[i]/=p[j];
}
int ki=1;
for(int i=2;i<=n;i++){
int u,v;scanf("%d%d",&u,&v);
x[++ki]=z[u],z[u]=ki,t[ki]=v;
x[++ki]=z[v],z[v]=ki,t[ki]=u;
}
sx=n*2;
vector<pii> f=dfs(1,0);
int zx=sx+1;
for(auto [x,xx]:f)zx=min(zx,xx);
cout<<zx<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 226ms
memory: 3808kb
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: 116ms
memory: 3832kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 120ms
memory: 3672kb
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:
15
result:
ok single line: '15'
Test #4:
score: 0
Accepted
time: 123ms
memory: 3732kb
input:
9 2 5 5 5 5 3 3 3 3 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9
output:
14
result:
ok single line: '14'
Test #5:
score: 0
Accepted
time: 119ms
memory: 3672kb
input:
8 13 13 13 2 13 13 13 13 1 2 1 3 1 4 1 5 1 6 1 7 1 8
output:
13
result:
ok single line: '13'
Test #6:
score: -100
Time Limit Exceeded
input:
5000 59 25 51 26 33 99 2 13 29 58 5 47 96 83 63 22 69 89 41 90 20 97 85 90 55 17 54 75 54 24 90 54 74 78 81 77 2 47 68 18 60 81 99 86 7 6 76 43 17 43 92 25 36 26 47 100 63 66 2 16 47 25 48 86 24 2 10 42 44 80 92 48 49 21 49 40 32 85 53 88 15 30 4 27 77 16 6 80 50 56 46 14 3 48 92 50 93 35 69 29 48 4...