QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#800341#4229. GCD Harmonyzeyu#RE 0ms5716kbC++233.0kb2024-12-06 09:16:122024-12-06 09:16:14

Judging History

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

  • [2024-12-06 09:16:14]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:5716kb
  • [2024-12-06 09:16:12]
  • 提交

answer

#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pl pair<ll, ll>
#define pi pair<int, int>
#define minpq priority_queue<ll, vector<ll>, greater<ll>>
using namespace std;

#if 1
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '['; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "]";}
void _print() {cerr << endl << flush;}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "*["<<__LINE__<<"]\t"<< #x << " = "; _print(x)
#endif

const ll mod = 1e9 + 7;

template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
ll gcd(ll a, ll b) {if(b == 0){return a;} return gcd(b, a % b);}

const int maxn = 5001;
int p[maxn];
 
void sieve(){
	for (int i = 0; i < maxn; i ++) p[i] = i;
	for (int i = 2; i < maxn; i ++){
		if (p[i] == i){
			for (int j = i + i; j < maxn; j += i){
				p[j] = i;
			}
		}
	}
}

int dp[maxn][2 * maxn];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	sieve();
	int n; cin >> n;
	vector<int> a(n); for (int i = 0; i < n; i ++) cin >> a[i];
	vector<int> graph[n];
	for (int i = 0; i < n - 1; i ++){
		int x, y; cin >> x >> y; x --; y --;
		graph[x].push_back(y);
		graph[y].push_back(x);
	}
	int maxv = max(2 * n + 1, 101);
	for (int i = 0; i < n; i ++){
		for (int j = 2; j < maxv; j ++){
			if (a[i] == j) dp[i][j] = 0;
			else dp[i][j] = j;
		}
	}

	function<void(int, int)> dfs = [&](int x, int parent){
		for (int to : graph[x]){
			if (to == parent) continue;
			dfs(to, x);
			for (int j = 2; j < maxv; j ++){
				if (p[j] == j){
					for (int k = j + j; k < maxv; k += j){
						chkmin(dp[to][j], dp[to][k]);
					}
				}
			}
			for (int j = 2; j < maxv; j ++){
				int cur = j, minimum = 1 << 30;
				while(cur > 1){
					chkmin(minimum, dp[to][p[cur]]);
					cur /= p[cur];
				}
				dp[x][j] += minimum;
			}
		}
	};

	dfs(0, 0);

	int ans = 1 << 30;
	for (int i = 2; i < maxv; i ++) chkmin(ans, dp[0][i]);
	cout << ans << '\n';
}

/*
Do something instead of nothing!
Print out stuffs / study samples when you cannot find the bug!
*/

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 5716kb

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: 0ms
memory: 3672kb

input:

3
1
2
3
3 1
2 3

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 5704kb

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: 0ms
memory: 3640kb

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: 0ms
memory: 3708kb

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
Runtime Error

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:


result: