QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#394126 | #4229. GCD Harmony | kevinshan# | WA | 1ms | 4052kb | C++17 | 1.6kb | 2024-04-20 04:17:06 | 2024-04-20 04:17:07 |
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;
short n, ar[MAXN];
vector<short> adj[MAXN];
short dp[MAXN][MAXM+5];
void debug();
void solve(short x, short p) {
// cout<<x<<"\n";
for(short 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], (short)(2*n));
}
}
for(int i=2; i<=MAXM; i++) {
if(ar[x]%i != 0) dp[x][i] += i;
dp[x][i] = min(dp[x][i], (short)(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++){
short 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);
short x = 2*n;
for(int j=2; j<=MAXM; j++) x = min(x, dp[i][j]);
// debug();
cout<<x;
return 0;
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3908kb
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: 3872kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 4052kb
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'