题解:P12375 「LAOI-12」MST?
观察样例可知,最优构造方案是:维护一个集合
二分一个最小的
对于前
稍作观察,这个数列可以写成
对于第
最后贴一下答案为:
#include <iostream>
using namespace std;
using LL = long long;
using i128 = __int128;
int main () {
cin.tie(0)->sync_with_stdio(0);
int T;
cin >> T;
while (T--) {
LL n, m;
cin >> n >> m;
LL L = 1, R = min(n - 1, LL(2e9));
while (L <= R) {
LL mid = (L + R) >> 1;
if (m < mid * (mid - 1) / 2 + (n - mid)) R = mid - 1;
else L = mid + 1;
}
LL x = L;
i128 ans = i128(x * x - x * 2 + 6) * (x - 1) / 6 + i128(n - x) * (m * 2 - n + x + 1) / 2;
cout << int(ans % 998244353) << '\n';
}
return 0;
}