QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#100361#4229. GCD HarmonyZuqaWA 44ms199084kbC++202.0kb2023-04-25 18:02:202023-04-25 18:03:33

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-25 18:03:33]
  • Judged
  • Verdict: WA
  • Time: 44ms
  • Memory: 199084kb
  • [2023-04-25 18:02:20]
  • Submitted

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define el '\n'
#define FIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
typedef unsigned long long ull;

template<typename T, typename X>
using hashTable = gp_hash_table<T, X>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

// mt19937_64 for long long
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());

const int N = 5e3 + 5, M = 1e4 + 5;

vector<int> G[N];
int dp[N][M], curVal[N];
int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};

int solve(int node, int par, int val)
{
    if(val > M)
        return INT_MAX;
    int &ret = dp[node][val];
    if(~ret)
        return ret;

    ret = (val == curVal[node] ? 0 : val);
    for(auto &ch: G[node])
    {
        if(ch == par)
            continue;

        int mn = INT_MAX;
        for(int i = 0; i < 25; i++)
        {
            if(val % p[i])
                continue;
            mn = min(mn, solve(ch, node, p[i]));
        }
        ret += mn;
    }

    for(int i = 0; i < 25; i++)
        ret = min(ret, solve(node, par, val * p[i]));
    return ret;
}

void doWork()
{
    int n;
    cin >> n;
    memset(dp, -1, sizeof dp);
    for(int i = 1; i <= n; i++)
        cin >> curVal[i];

    for(int i = 0, u, v; i < n - 1; i++)
        cin >> u >> v, G[u].push_back(v), G[v].push_back(u);

    int ans = 2 * n;
    cout << min(ans, solve(1, -1, 2));
}

signed main()
{
    FIO
    int T = 1;
//    cin >> T;
    for(int i = 1; i <= T; i++)
        doWork();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 35ms
memory: 199084kb

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: 44ms
memory: 199040kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: -100
Wrong Answer
time: 16ms
memory: 199064kb

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:

18

result:

wrong answer 1st lines differ - expected: '15', found: '18'