QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104858 | #4229. GCD Harmony | Yanagi_Origami# | WA | 8ms | 3464kb | C++20 | 1.5kb | 2023-05-12 08:34:30 | 2023-05-12 08:34:33 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define forn(i,a,b) for(int i = a; i < (b); i++)
#define fore(i,a,b) for(int i = a; i <= (b); i++)
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using ll = long long;
using ld = long double;
static constexpr ll kInf = 1'000'000'000'000'000'000;
static constexpr int kMaxA = 220;
void dfs(const vector<vector<int>> &adj, const vector<int>& a,
vector<vector<ll>>& dp, const int u, const int p)
{
for (int v : adj[u])
{
if (v == p) continue;
dfs(adj, a, dp, v, u);
}
for (int j = 1; j <= kMaxA; j++)
{
ll sum = j;
if (j == a[u]) sum = 0; // no change needed
for (int v : adj[u])
{
if (v == p) continue;
ll mn = kInf;
for (int k = 1; k <= kMaxA; k++)
{
if (gcd(k, j) == 1) continue;
mn = min(mn, dp[v][k]);
}
sum += mn;
}
dp[u][j] = sum;
}
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n; cin>>n;
vector<int> a(n);
forn(i,0,n) cin>>a[i];
vector<vector<int>> adj(n);
forn(i,0,n-1)
{
int u,v; cin>>u>>v; u--; v--;
adj[u].pb(v);
adj[v].pb(u);
}
vector<vector<ll>> dp(n, vector<ll>(kMaxA + 1, 0));
dfs(adj, a, dp, 0, -1);
ll ans = kInf;
fore(i, 1, kMaxA) ans = min(ans, dp[0][i]);
cout << ans << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 6ms
memory: 3464kb
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: 3412kb
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: 3440kb
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'