QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#847904 | #4229. GCD Harmony | shinonome_ena# | WA | 8ms | 32000kb | C++14 | 1.5kb | 2025-01-08 13:02:31 | 2025-01-08 13:02:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll inf=1e18,siz=200;
ll n,m,i,j,k,l,r,x,y,z,w,s,t,a[1100000],dp[5500][220],adj[220][220];
vector<ll> v[1100000];
void f(ll x,ll y)
{
ll i;
for(auto xx:v[x])
{
if(xx==y)
continue;
f(xx,x);
}
for(i=1;i<=siz;i++)
{
if(i!=a[x])
dp[x][i]=i;
else
dp[x][i]=0;
}
ll s;
for(i=1;i<=siz;i++)
{
for(auto xx:v[x])
{
if(xx==y)
continue;
s=inf;
for(j=1;j<=siz;j++)
{
if(adj[i][j])
s=min(s,dp[xx][j]);
}
dp[x][i]+=s;
}
}
return;
}
ll gcd(ll x,ll y)
{
if(x>y)
swap(x,y);
if(x==0)
return y;
return gcd(y%x,x);
}
int main()
{
for(i=1;i<=siz;i++)
for(j=1;j<=siz;j++)
{
if(gcd(i,j)>=2)
adj[i][j]=1;
}
scanf("%lld",&n);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(i=1;i<n;i++)
{
scanf("%lld %lld",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=200;j++)
{
dp[i][j]=inf;
}
}
f(1,0);
s=inf;
for(i=1;i<=200;i++)
{
//printf("(%lld)\n",dp[1][i]);
s=min(s,dp[1][i]);
}
printf("%lld",s);
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 30028kb
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: 3ms
memory: 30036kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: -100
Wrong Answer
time: 8ms
memory: 32000kb
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:
-6446744073709551615
result:
wrong answer 1st lines differ - expected: '15', found: '-6446744073709551615'