48pts #13~#33MLE求助
查看原帖
48pts #13~#33MLE求助
1241185
yi105011楼主2024/9/4 20:42
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 10000010;
int n , a[N] , b[100010] , ans;
struct node {
	int x , y , num;
};
bool operator < (node a , node b) {
	return a.num < b.num;
}
vector <node> v;
int father (int x) {
	if (a[x] == x) return x;
	return x = father ( a[x] );
}
signed main() {
	cin >> n;
	while (n --) {
		int m; cin >> m;
		a[m] = 1;
	}
	n = 0;
	for (int i = 1;i <= N - 10;i ++) {
		if ( a[i] ) {
			b[++ n] = i;
			/*cout << i << " ";
			cout << b[n] << " ";
			printf ("a[%lld]=%lld\n" , n , b[n]);
			cout << b[n] << " ";
			/*for (int z = 2;z * i <= N - 10;z ++) {
				a[i * z] = 0;
			}*/
			a[i] = i;
		}
	}
	//cout << n << endl;
	for (int i = n;i >= 1;i --) {
		for (int z = i - 1;z >= 1;z --) {
			//printf ("%lld %lld %lld\n" , b[i] , b[z] , b[i] % b[z]);
			v.push_back ( node {b[i] , b[z] , b[i] % b[z]} );
		}
	}
	sort (v.begin() , v.end());
	for (int i = 0;i < v.size();i ++) {
	}
	for (int i = 0;i < v.size();i ++) {
		int x = v[i].x , y = v[i].y;
		if (father(x) == father(y)) continue;
		a[father(x)] = father(y);
		ans += v[i].num;
	}
	cout << ans;
	return 0;
}

提交记录

2024/9/4 20:42
加载中...