結果

問題 No.3270 No Coprime Cycles
ユーザー 遭難者
提出日時 2025-08-04 20:33:47
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 674 ms / 2,000 ms
コード長 5,912 bytes
コンパイル時間 5,942 ms
コンパイル使用メモリ 335,256 KB
実行使用メモリ 93,612 KB
最終ジャッジ日時 2025-08-18 01:20:45
合計ジャッジ時間 33,838 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using std::cerr;
using std::cin;
using std::cout;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using mint = atcoder::modint998244353;
istream &operator>>(istream &is, mint &a) {
	long long t;
	is >> t;
	a = t;
	return is;
}
ostream &operator<<(ostream &os, mint a) { return os << a.val(); }
#endif
typedef long double ld;
#define long long long
#define uint unsigned int
#define ull unsigned long
#define overload3(a, b, c, name, ...) name
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(__i, n)
#define rep(...) overload3(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)
#define per3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define per2(i, n) per3(i, 0, n)
#define per1(n) per2(__i, n)
#define per(...) overload3(__VA_ARGS__, per3, per2, per1)(__VA_ARGS__)
#define all(a) a.begin(), a.end()
#define UNIQUE(a)                                                              \
	sort(all(a));                                                              \
	a.erase(unique(all(a)), a.end())
#define sz(a) (int)a.size()
#define vec vector
#ifndef DEBUG
#define cerr                                                                   \
	if (0)                                                                     \
	cerr
// #undef assert
// #define assert(...) void(0)
#undef endl
#define endl '\n'
#endif
template <typename T> ostream &operator<<(ostream &os, vector<T> a) {
	const int n = a.size();
	rep(i, n) {
		os << a[i];
		if (i + 1 != n)
			os << " ";
	}
	return os;
}
template <typename T, size_t n>
ostream &operator<<(ostream &os, array<T, n> a) {
	rep(i, n) {
		os << a[i];
		if (i + 1 != n)
			os << " ";
	}
	return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &a) {
	for (T &i : a)
		is >> i;
	return is;
}
template <typename T, typename S> bool chmin(T &x, S y) {
	if ((T)y < x) {
		x = (T)y;
		return true;
	}
	return false;
}
template <typename T, typename S> bool chmax(T &x, S y) {
	if (x < (T)y) {
		x = (T)y;
		return true;
	}
	return false;
}
template <typename T> void operator++(vector<T> &a) {
	for (T &i : a)
		++i;
}
template <typename T> void operator--(vector<T> &a) {
	for (T &i : a)
		--i;
}
template <typename T> void operator++(vector<T> &a, int) {
	for (T &i : a)
		i++;
}
template <typename T> void operator--(vector<T> &a, int) {
	for (T &i : a)
		i--;
}
vec<long> solve(vec<int> uu, vec<int> vv) {
	const int n = sz(uu) + 1;
	vec<vec<int>> g(n);
	vec<vec<pair<int, int>>> gi(n);
	rep(i, n - 1) {
		g[uu[i]].push_back(vv[i]);
		g[vv[i]].push_back(uu[i]);
		gi[uu[i]].emplace_back(vv[i], i);
		gi[vv[i]].emplace_back(uu[i], i);
	}
	constexpr int INF = 1e9;
	auto bfs = [&](int to) {
		queue<int> q;
		q.push(to);
		vec<int> ans(n, INF);
		ans[to] = 0;
		while (!q.empty()) {
			int x = q.front();
			q.pop();
			for (int i : g[x]) {
				if (ans[i] != INF)
					continue;
				ans[i] = ans[x] + 1;
				q.push(i);
			}
		}
		return ans;
	};
	auto res0 = bfs(0);
	int t1 = 0;
	rep(i, n) if (res0[i] > res0[t1]) t1 = i;
	auto res1 = bfs(t1);
	int t2 = 0;
	rep(i, n) if (res1[i] > res1[t2]) t2 = i;
	auto res2 = bfs(t2);
	int di = res1[t2];
	long ans0 = 0;
	rep(i, n) ans0 += max(res1[i], res2[i]);
	assert(di % 2 == 0);

	cerr << res1 << endl << res2 << endl;
	int c = -1;
	rep(i, n) if (res1[i] == di / 2 && res2[i] == di / 2) {
		c = i;
		break;
	}
	cerr << c << endl;
	auto resc = bfs(c);
	vec<int> toi;
	rep(i, n) if (resc[i] == di / 2) toi.push_back(i);
	assert(sz(toi) >= 2);
	vec<bool> cri_ty(n);
	queue<int> q;
	for (int st : toi) {
		cri_ty[st] = true;
		q.push(st);
	}
	vec<vec<pair<int, int>>> gg(n);
	while (!q.empty()) {
		int x = q.front();
		q.pop();
		for (auto [i, idx] : gi[x]) {
			if (resc[i] > resc[x])
				continue;
			gg[i].emplace_back(x, idx);
			if (cri_ty[i])
				continue;
			cri_ty[i] = true;
			q.push(i);
			break;
		}
	}
	cerr << "cri_ty : " << cri_ty << endl;
	vec<int> cri(n - 1, -1); // 親が gg[c] のどちらの頂点か
	assert(sz(gg[c]) >= 2);
	if (sz(gg[c]) == 2) {
		auto f = [&](auto f, int to, int v) -> void {
			if (sz(gg[to]) >= 2)
				return;
			for (auto [i, idx] : gg[to]) {
				cri[idx] = v;
				f(f, i, v);
			}
		};
		for (auto [i, idx] : gg[c]) {
			cri[idx] = i;
			f(f, i, i);
		}
	}
	vec<int> buc(n, 1); // c を根とした部分木の大きさ
	auto f = [&](auto f, int to, int no) -> void {
		for (int i : g[to]) {
			if (i == no)
				continue;
			f(f, i, to);
			buc[to] += buc[i];
		}
	};
	f(f, c, -1);
	vec<long> rans(n - 1);
	rep(i, n - 1) {
		int u = uu[i], v = vv[i];
		if (resc[u] > resc[v])
			swap(u, v);
		long ans = ans0;
		ans -= max(res1[v], res2[v]);
		ans -= buc[v] - 1;
		if (cri[i] != -1) {
			assert(gg[c][0].first == cri[i] || gg[c][1].first == cri[i]);
			int x = gg[c][0].first + gg[c][1].first - cri[i];
			ans -= buc[x];
		}
		rans[i] = ans;
	}
	return rans;
}
void solve() {
	constexpr int INF = 1e6 + 1;
	vec<bool> is_prime(INF, true);
	vec<vec<int>> pr(INF);
	vec<int> prime;
	for (long i = 2; i < INF; i++) {
		if (!is_prime[i])
			continue;
		for (long k = i; k < INF; k += i) {
			is_prime[k] = false;
			pr[k].push_back(sz(prime));
		}
		prime.push_back(i);
	}
	int n;
	cin >> n;
	vec<int> a(n);
	cin >> a;
	vec<tuple<long, int, int>> li;
	rep(i, n) {
		auto prai = pr[a[i]];
		for (int pi : prai) {
			int v = 0;
			const int p = prime[pi];
			while (a[i] % p == 0) {
				a[i] /= p;
				v += p;
			}
			li.emplace_back(v, i, n + pi);
		}
	}
	sort(all(li), greater<tuple<long, int, int>>());
	atcoder::dsu dsu(n + sz(pr));
	int ans = 0;
	for (auto [v, i, j] : li) {
		if (dsu.same(i, j)) {
			ans += v;
			continue;
		}
		dsu.merge(i, j);
	}
	cout << ans << endl;
	return;
}
int main() {
	// srand((unsigned)time(NULL));
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);
	int t = 1;
	// cin >> t;
	while (t--)
		solve();
}
0