QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#848102 | #4229. GCD Harmony | HeyJinhwi# | RE | 5ms | 6408kb | C++14 | 2.3kb | 2025-01-08 15:40:01 | 2025-01-08 15:40:01 |
Judging History
answer
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <vector>
using namespace std;
int dp[5001][101],dp2[5001][101],a[5050];
vector<int> adj[5010];
vector<int> thdlstn[10001];
int gcd(int a,int b)
{
if(!b)return a;
return gcd(b,a%b);
}
int n;
void dfs(int k,int p)
{
for(int i=0;i<adj[k].size();i++)
{
if(adj[k][i]!=p)dfs(adj[k][i],k);
}
for(int i=2;i<=10000;i++)
{
int ans=0;
int cur;
for(int j=0;j<adj[k].size();j++)
{
if(adj[k][j]==p)continue;
cur=100000;
for(int l=0;l<thdlstn[i].size();l++)
{
if(thdlstn[i][l]>100)continue;
cur=min(cur,dp[adj[k][j]][thdlstn[i][l]]);
}
ans+=cur;
}
if(i!=a[k])ans+=i;
for(int j=0;j<thdlstn[i].size();j++)
{
if(thdlstn[i][j]>100)continue;
if(dp2[k][thdlstn[i][j]]==-1)
{
dp2[k][thdlstn[i][j]]=ans;
}
else
{
dp2[k][thdlstn[i][j]]=min(dp2[k][thdlstn[i][j]],ans);
}
}
}
for(int i=2;i<=100;i++)dp[k][i]=dp2[k][i];
}
int main()
{
for(int i=2;i<=10000;i++)
{
int t=i;
for(int j=2;j<=100;j++)
{
if(t%j==0)
{
thdlstn[i].push_back(j);
while(t%j==0)t/=j;
}
}
if(t!=1)thdlstn[i].push_back(t);
}
//freopen("input.txt","r",stdin);
int n;cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=100;j++)dp2[i][j]=-1;
}
for(int i=1;i<=n;i++)
{
cin>>a[i];
for(int j=2;j<=1000;j++)
{
if(a[i]!=j)dp[i][j]=j;
}
for(int j=0;j<thdlstn[a[i]].size();j++)
{
if(thdlstn[a[i]][j]>100)continue;
dp[i][thdlstn[a[i]][j]]=0;
}
}
for(int i=1;i<n;i++)
{
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,0);
int ans=2*n;
for(int i=2;i<=100;i++)if(dp[1][i]!=-1)ans=min(ans,dp[1][i]);
cout<<ans;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 6284kb
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: 4ms
memory: 6356kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 2ms
memory: 6332kb
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: 5ms
memory: 6408kb
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: 5ms
memory: 6344kb
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
Runtime Error
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...