QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#61384 | #4229. GCD Harmony | 2pal1rak# | WA | 578ms | 199924kb | C++14 | 2.2kb | 2022-11-12 19:05:57 | 2022-11-12 19:05:57 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e5;
const int Nmax = 5005;
int n, lim;
vector<int> v[Nmax];
int dp[Nmax][2*Nmax];
vector<int> primes;
vector<int> factors[2*Nmax];
int curr_val[2*Nmax], val[Nmax];
int factor[2*Nmax];
bool good[2*Nmax];
void dfs(int node, int dad = -1)
{
for(auto it : v[node])
if(dad != it)
dfs(it, node);
for(auto p : primes) factor[p] = inf;
int i;
for(i=2; i<=lim; ++i)
if(good[i] || i == val[node])
{
curr_val[i] = 0;
for(auto it : v[node])
if(dad != it)
curr_val[i] += dp[it][i];
if(i != val[node]) curr_val[i] += i;
for(auto p : factors[i])
factor[p] = min(factor[p], curr_val[i]);
}
for(i=1; i<=lim; ++i)
if(good[i])
{
dp[node][i] = inf;
for(auto p : factors[i])
dp[node][i] = min(dp[node][i], factor[p]);
}
}
void precompute(int lim)
{
int i;
for(i=2; i<=lim; ++i)
{
int x = i;
good[i] = 1;
for(auto it : primes)
{
int cnt = 0;
while(x % it == 0)
{
x /= it;
++cnt;
}
if(cnt >= 2) good[i] = 0;
if(cnt >= 1) factors[i].push_back(it);
}
if(factors[i].size() && factors[i].back() > 100) good[i] = 0;
if(factors[i].empty())
{
primes.push_back(i);
factors[i].push_back(i);
}
}
good[1] = 0;
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n;
lim = 2*n;
int i;
for(i=1; i<=n; ++i) cin >> val[i];
precompute(lim);
//for(auto it : primes) cout << it << ' '; cout << "#\n";
//for(auto it : factors[30]) cout << it << ' '; cout << "@\n";
for(i=1; i<n; ++i)
{
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1);
int ans = inf;
for(i=1; i<=lim; ++i)
if(good[i])
ans = min(ans, dp[1][i]);
cout << ans << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 5628kb
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: 5764kb
input:
3 1 2 3 3 1 2 3
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 2ms
memory: 5728kb
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: 2ms
memory: 5928kb
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: 2ms
memory: 5772kb
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
Wrong Answer
time: 578ms
memory: 199924kb
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...
output:
4
result:
wrong answer 1st lines differ - expected: '4778', found: '4'