QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#394133 | #4229. GCD Harmony | kevinshan# | WA | 3ms | 4300kb | C++17 | 1.7kb | 2024-04-20 04:42:40 | 2024-04-20 04:42:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define f first
#define s second
const int MAXN = 5005;
const int MAXM = 10000;
int n, ar[MAXN];
vector<int> adj[MAXN];
int dp[MAXN][MAXM+5];
void debug();
void solve(int x, int p) {
// cout<<x<<"\n";
for(int i:adj[x]) {
if(i == p) continue;
solve(i,x);
for(int j=2; j<=MAXM; j++) {
for(int l=j; l<=MAXM; l+=j) {
dp[i][j] = min(dp[i][j], dp[i][l]);
}
dp[x][j] += dp[i][j];
dp[x][j] = min(dp[x][j], (2*n));
}
}
for(int i=2; i<=MAXM; i++){
for(int j=i; j<=MAXM; j+=i) {
dp[x][j] = min(dp[x][j], dp[x][i]);
}
}
for(int i=2; i<=MAXM; i++) {
if(ar[x]%i != 0) dp[x][i] += i;
dp[x][i] = min(dp[x][i], (2*n));
}
// debug();
}
void debug() {
// cout<<"\n";
// for(int i=0; i<n; i++){
// for(int j=0; j<10; j++) cout<<dp[i][j]<<" ";
// cout<<"\n";
// }
// cout<<"\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
cin>>n;
for(int i=0; i<n; i++) cin>>ar[i];
for(int i=0; i<n-1; i++){
int u,v;
cin>>u>>v;
adj[--u].pb(--v);
adj[v].pb(u);
}
for(int i=0; i<n; i++) {
if(adj[i].size() == 1){
solve(i,-1);
int x = 2*n;
for(int j=2; j<=MAXM; j++) x = min(x, dp[i][j]);
// debug();
cout<<x;
return 0;
}
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4024kb
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: 1ms
memory: 3848kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: -100
Wrong Answer
time: 3ms
memory: 4300kb
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:
21
result:
wrong answer 1st lines differ - expected: '15', found: '21'