QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#90997#4229. GCD Harmonythexin7WA 5ms5932kbC++141.8kb2023-03-26 14:55:272023-03-26 14:55:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-26 14:55:29]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:5932kb
  • [2023-03-26 14:55:27]
  • 提交

answer

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int N = 5010;
int val[N];
vector<int> vec[N];
vector<int> p[110];
int f[N][110];
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
void dfs(int u, int fa) {
  int tmp[110];
  memset(tmp, 0, sizeof(tmp));
  for (int j = 0; j < vec[u].size(); j++) {
    int v = vec[u][j];
    if (v == fa) continue;
    dfs(v, u);
    for (int i = 2; i <= 100; i++) {
      tmp[i] += f[v][i];
    }
  }
  for (int i = 2; i <= 100; i++) {
    for (int j = 2; j <= 100; j++) {
      // 同时满足i j
      // 与j具有相同因子 and 与 %i == 0
      int cost = i * j / gcd(i, j);
      if (val[u] % cost == 0) cost = 0;
      f[u][i] = min(tmp[j] + cost, f[u][i]);
      if (gcd(i, j) > 1) {
        cost = i;
        if (val[u] % cost == 0) cost = 0;
        f[u][i] = min(tmp[j] + cost, f[u][i]);
      }
      for (int k = 0; k < p[j].size(); k++) {
          cost = i * p[j][k];
          if (val[u] % cost == 0) cost = 0;
          f[u][i] = min(tmp[j] + cost, f[u][i]);
      }
    }
  }
}

int main() {
  memset(f, 0x3f, sizeof(f));
  for (int i = 2; i <= 100; i++) {
    int flag = 1;
    int ti = i;
    for (int j = 2; j < i; j++) {
      if (ti % j == 0) {
        flag = 0;
        p[i].push_back(j);
        while(ti%j==0){
          ti/=j;
        }
        break;
      }
    }
  }
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &val[i]);
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    vec[u].push_back(v);
    vec[v].push_back(u);
  }
  dfs(1, 0);
  int ans = 0x3f3f3f3f;
  for (int i = 2; i <= 100; i++) {
    ans = min(ans, f[1][i]);
  }
  printf("%d\n", ans);
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 5744kb

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: 2ms
memory: 5932kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: -100
Wrong Answer
time: 5ms
memory: 5712kb

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'