QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#257365#7749. A Simple MST Problemucup-team484#WA 165ms24408kbC++172.0kb2023-11-19 03:05:012023-11-19 03:05:02

Judging History

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

  • [2023-11-19 03:05:02]
  • 评测
  • 测评结果:WA
  • 用时:165ms
  • 内存:24408kb
  • [2023-11-19 03:05:01]
  • 提交

answer

#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define st first
#define nd second
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int lp[N], pr[N], cnt = 0, sm[N];

void solve() {
	int l, r; cin >> l >> r;
	ll ans = 0;
	vector<int> par(r + 1);
	iota(all(par), 0);
	function<int(int)> qry = [&](int x) {
		return x == par[x] ? x : par[x] = qry(par[x]);
	};
	vector<vector<int>> comp(r + 1);
	for (int i = l; i <= r; i++)
		comp[i].push_back(i);
	int cnt = r - l + 1;
	while (cnt > 1) {
		vector<pair<int, int>> cand(r + 1, make_pair(mod, -1));
		auto upd = [&](int x, int y) {
			int val = sm[x] + sm[y] - sm[gcd(x, y)];
			cand[x] = min(cand[x], make_pair(val, y));
		};
		for (int g = r; g >= 1; g--) {
			vector<pair<int, int>> arr;
			for (int x = g; x <= r; x += g) {
				if (x >= l)
					arr.push_back({sm[x], x});
			}
			sort(all(arr));
			int j = 0;
			while (j < sz(arr) && qry(arr[j].nd) == qry(arr[0].nd))
				j++;
			if (j == sz(arr))
				continue;
			for (int i = 0; i < sz(arr); i++) {
				if (qry(arr[i].nd) == qry(arr[0].nd))
					upd(arr[i].nd, arr[j].nd);
				else
					upd(arr[i].nd, arr[0].nd);
			}
		}
		for (int i = l; i <= r; i++) {
			int u = i, v = cand[i].nd;
			if (v == -1)
				continue;
			if (qry(u) != qry(v))
				ans += sm[u] + sm[v] - sm[gcd(u, v)];
			u = qry(u);
			v = qry(v);
			if (u != v) {
				if (sz(comp[u]) > sz(comp[v]))
					swap(u, v);
				comp[v].insert(comp[v].end(), comp[u].begin(), comp[u].end());
				comp[u].clear();
				cnt--;
				par[u] = v;
			}
		}
	}
	cout << ans << "\n";
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	for (int i = 2; i < N; i++) {
		if (!lp[i])
			pr[cnt++] = lp[i] = i, sm[i] = 1;
		for (int j = 0; j < cnt && pr[j] * i < N && pr[j] <= lp[i]; j++) {
			sm[pr[j] * i] = sm[i] + (pr[j] != lp[i]);
			lp[pr[j] * i] = pr[j];
		}
	}
	int t; cin >> t; while (t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 11628kb

input:

5
1 1
4 5
1 4
1 9
19 810

output:

0
2
3
9
1812

result:

ok 5 lines

Test #2:

score: -100
Wrong Answer
time: 165ms
memory: 24408kb

input:

2
27 30
183704 252609

output:

8
223175

result:

wrong answer 2nd lines differ - expected: '223092', found: '223175'