QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#36087#4229. GCD Harmonydeneribeiro10WA 7ms30104kbC++1.1kb2022-06-24 07:50:142022-06-24 07:50:15

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-06-24 07:50:15]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:30104kb
  • [2022-06-24 07:50:14]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
// #define endl '\n'
const int INF = 1e9;
const ll INFLL = 1e18;
const int MAX = 1e6+10;
const int MOD = 1000000007;

int gcd(int a, int b) {
    return b ? gcd(b, a%b) : a;
}

int n;
vector<int> adj[MAX];
int v[MAX];
int dp[5050][111];

int DP(int node, int par, int val) {
    int &ans = dp[node][val];
    if(ans != -1) return ans;
    ans = INF;
    for(int i=2; i<=250; ++i) {
        if(gcd(i, val) != 1 or par == 0) {
            int sum = (i != v[node] ? i : 0);
            for(int to : adj[node]) if(to != par) {
                sum += DP(to, node, i);
            }
            ans = min(ans, sum);
        }
    }

    return ans;
}

void test_case() {
    cin >> n;
    for(int i=1; i<=n; ++i) cin >> v[i];
    for(int i=0; i<n-1; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u); 
    }

    memset(dp, -1, sizeof dp);
    cout << DP(1, 0, 0) << endl;
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);

    test_case();

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 7ms
memory: 30104kb

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: -100
Wrong Answer
time: 7ms
memory: 29580kb

input:

3
1
2
3
3 1
2 3

output:

3

result:

wrong answer 1st lines differ - expected: '4', found: '3'