結果

問題 No.2635 MST on Line++ 2
ユーザー ecotteaecottea
提出日時 2024-02-20 16:24:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 15,205 bytes
コンパイル時間 6,719 ms
コンパイル使用メモリ 303,204 KB
実行使用メモリ 14,396 KB
最終ジャッジ日時 2024-02-20 16:24:28
合計ジャッジ時間 11,556 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
11,324 KB
testcase_01 AC 13 ms
11,324 KB
testcase_02 AC 14 ms
11,324 KB
testcase_03 AC 13 ms
11,324 KB
testcase_04 AC 15 ms
11,324 KB
testcase_05 AC 107 ms
14,268 KB
testcase_06 AC 15 ms
11,324 KB
testcase_07 AC 100 ms
14,012 KB
testcase_08 AC 15 ms
11,324 KB
testcase_09 AC 74 ms
13,244 KB
testcase_10 AC 15 ms
11,324 KB
testcase_11 AC 90 ms
13,756 KB
testcase_12 AC 16 ms
11,324 KB
testcase_13 AC 109 ms
14,396 KB
testcase_14 AC 15 ms
11,324 KB
testcase_15 AC 73 ms
13,244 KB
testcase_16 AC 16 ms
11,324 KB
testcase_17 AC 84 ms
13,372 KB
testcase_18 AC 15 ms
11,324 KB
testcase_19 AC 107 ms
14,012 KB
testcase_20 AC 15 ms
11,324 KB
testcase_21 AC 111 ms
14,140 KB
testcase_22 AC 16 ms
11,324 KB
testcase_23 AC 78 ms
13,244 KB
testcase_24 AC 14 ms
11,324 KB
testcase_25 AC 14 ms
11,324 KB
testcase_26 AC 69 ms
14,396 KB
testcase_27 AC 48 ms
13,372 KB
testcase_28 AC 71 ms
14,396 KB
testcase_29 AC 15 ms
11,324 KB
testcase_30 AC 15 ms
11,324 KB
testcase_31 AC 158 ms
14,396 KB
testcase_32 AC 171 ms
14,396 KB
testcase_33 AC 140 ms
13,884 KB
testcase_34 AC 128 ms
14,396 KB
testcase_35 AC 159 ms
14,396 KB
testcase_36 AC 126 ms
14,396 KB
testcase_37 AC 150 ms
14,396 KB
testcase_38 AC 106 ms
14,396 KB
testcase_39 AC 14 ms
11,324 KB
testcase_40 AC 13 ms
11,324 KB
testcase_41 AC 14 ms
11,324 KB
testcase_42 AC 14 ms
11,324 KB
testcase_43 AC 14 ms
11,324 KB
testcase_44 AC 13 ms
11,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef HIDDEN_IN_VS // 折りたたみ用

// 警告の抑制
#define _CRT_SECURE_NO_WARNINGS

// ライブラリの読み込み
#include <bits/stdc++.h>
using namespace std;

// 型名の短縮
using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9)
using pii = pair<int, int>;	using pll = pair<ll, ll>;	using pil = pair<int, ll>;	using pli = pair<ll, int>;
using vi = vector<int>;		using vvi = vector<vi>;		using vvvi = vector<vvi>;	using vvvvi = vector<vvvi>;
using vl = vector<ll>;		using vvl = vector<vl>;		using vvvl = vector<vvl>;	using vvvvl = vector<vvvl>;
using vb = vector<bool>;	using vvb = vector<vb>;		using vvvb = vector<vvb>;
using vc = vector<char>;	using vvc = vector<vc>;		using vvvc = vector<vvc>;
using vd = vector<double>;	using vvd = vector<vd>;		using vvvd = vector<vvd>;
template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
using Graph = vvi;

// 定数の定義
const double PI = acos(-1);
const vi DX = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左)
const vi DY = { 0, 1, 0, -1 };
int INF = 1001001001; ll INFL = 4004004003104004004LL; // (int)INFL = 1010931620;

// 入出力高速化
struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp;

// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((int)(x).size())
#define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), x))
#define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), x))
#define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");}
#define YES(b) {cout << ((b) ? "YES\n" : "NO\n");}
#define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順
#define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順
#define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順
#define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能)
#define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能)
#define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順)
#define repis(i, set) for(int i = lsb(set), bset##i = set; i >= 0; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順)
#define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順)
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
#define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了
#define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 矩形内判定

// 汎用関数の定義
template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; }
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す)
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す)
template <class T> inline T get(T set, int i) { return (set >> i) & T(1); }

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; }
template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; }

#endif // 折りたたみ用


#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;

#ifdef _MSC_VER
#include "localACL.hpp"
#endif

//using mint = modint1000000007;
using mint = modint998244353;
//using mint = modint; // mint::set_mod(m);

namespace atcoder {
	inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; }
	inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; }
}
using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>;
#endif


#ifdef _MSC_VER // 手元環境(Visual Studio)
#include "local.hpp"
#else // 提出用(gcc)
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(ll n) { return __builtin_popcountll(n); }
inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : -1; }
inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : -1; }
inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; }
inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; }
#define dump(...)
#define dumpel(v)
#define dump_list(v)
#define dump_mat(v)
#define input_from_file(f)
#define output_to_file(f)
#define Assert(b) { if (!(b)) while (1) cout << "OLE"; }
#endif


//【階乗など(法が大きな素数)】
/*
* Factorial_mint(int N) : O(n)
*	N まで計算可能として初期化する.
*
* mint fact(int n) : O(1)
*	n! を返す.
*
* mint fact_inv(int n) : O(1)
*	1/n! を返す(n が負なら 0 を返す)
*
* mint inv(int n) : O(1)
*	1/n を返す.
*
* mint perm(int n, int r) : O(1)
*	順列の数 nPr を返す.
*
* mint bin(int n, int r) : O(1)
*	二項係数 nCr を返す.
*
* mint mul(vi rs) : O(|rs|)
*	多項係数 nC[rs] を返す.(n = Σrs)
*
* mint hom(int n, int r) : O(1)
*	重複組合せの数 nHr = n+r-1Cr を返す(0H0 = 1 とする)
*/
class Factorial_mint {
	int n_max;

	// 階乗と階乗の逆数の値を保持するテーブル
	vm fac, fac_inv;

public:
	// n! までの階乗とその逆数を前計算しておく.O(n)
	Factorial_mint(int n) : n_max(n), fac(n + 1), fac_inv(n + 1) {
		// verify : https://atcoder.jp/contests/dwacon6th-prelims/tasks/dwacon6th_prelims_b

		fac[0] = 1;
		repi(i, 1, n) fac[i] = fac[i - 1] * i;

		fac_inv[n] = fac[n].inv();
		repir(i, n - 1, 0) fac_inv[i] = fac_inv[i + 1] * (i + 1);
	}
	Factorial_mint() : n_max(0) {} // ダミー

	// n! を返す.
	mint fact(int n) const {
		// verify : https://atcoder.jp/contests/dwacon6th-prelims/tasks/dwacon6th_prelims_b

		Assert(0 <= n && n <= n_max);
		return fac[n];
	}

	// 1/n! を返す(n が負なら 0 を返す)
	mint fact_inv(int n) const {
		// verify : https://atcoder.jp/contests/abc289/tasks/abc289_h

		Assert(n <= n_max);
		if (n < 0) return 0;
		return fac_inv[n];
	}

	// 1/n を返す.
	mint inv(int n) const {
		// verify : https://atcoder.jp/contests/exawizards2019/tasks/exawizards2019_d

		Assert(0 < n && n <= n_max);
		return fac[n - 1] * fac_inv[n];
	}

	// 順列の数 nPr を返す.
	mint perm(int n, int r) const {
		// verify : https://atcoder.jp/contests/abc172/tasks/abc172_e

		Assert(n <= n_max);

		if (r < 0 || n - r < 0) return 0;
		return fac[n] * fac_inv[n - r];
	}

	// 二項係数 nCr を返す.
	mint bin(int n, int r) const {
		// verify : https://atcoder.jp/contests/abc034/tasks/abc034_c

		Assert(n <= n_max);
		if (r < 0 || n - r < 0) return 0;
		return fac[n] * fac_inv[r] * fac_inv[n - r];
	}

	// 多項係数 nC[rs] を返す.
	mint mul(const vi& rs) const {
		// verify : https://yukicoder.me/problems/no/2141

		if (*min_element(all(rs)) < 0) return 0;
		int n = accumulate(all(rs), 0);
		Assert(n <= n_max);

		mint res = fac[n];
		repe(r, rs) res *= fac_inv[r];

		return res;
	}

	// 重複組合せの数 nHr = n+r-1Cr を返す(0H0 = 1 とする)
	mint hom(int n, int r) {
		// verify : https://mojacoder.app/users/riantkb/problems/toj_ex_2

		if (n == 0) return (int)(r == 0);
		Assert(n + r - 1 <= n_max);
		if (r < 0 || n - 1 < 0) return 0;
		return fac[n + r - 1] * fac_inv[r] * fac_inv[n - 1];
	}
};
Factorial_mint fm((int)1e6);


//【重み付きグラフの辺】
/*
* to : 行き先の頂点番号
* cost : 辺の重み
*/
struct WEdge {
	// verify : https://judge.yosupo.jp/problem/shortest_path

	int to; // 行き先の頂点番号
	ll cost; // 辺の重み

	WEdge() : to(-1), cost(-INFL) {}
	WEdge(int to, ll cost) : to(to), cost(cost) {}

	// プレーングラフで呼ばれたとき用
	operator int() const { return to; }

#ifdef _MSC_VER
	friend ostream& operator<<(ostream& os, const WEdge& e) {
		os << '(' << e.to << ',' << e.cost << ')';
		return os;
	}
#endif
};


//【重み付きグラフ】
/*
* WGraph g
* g[v] : 頂点 v から出る辺を並べたリスト
*
* verify : https://judge.yosupo.jp/problem/shortest_path
*/
using WGraph = vector<vector<WEdge>>;


//【最小全域森】O(m log n)
/*
* 重み付き無向グラフ g の最小全域森を求め,そのコストを返す.
* 最小全域森を msf に構成し,各最小全域木の代表元を rs に格納する.
*/
ll kruskal(const WGraph& g, WGraph* msf = nullptr, vi* rs = nullptr) {
	// verify : https://judge.yosupo.jp/problem/minimum_spanning_tree

	int n = sz(g);
	if (msf != nullptr)	*msf = WGraph(n);

	// 辺を集めてコスト昇順にソートする.
	priority_queue_rev<tuple<ll, int, int>> q;
	rep(s, n) repe(e, g[s]) q.push({ e.cost, s, e.to });

	ll cost = 0; // 最小コスト
	dsu d(n); // 連結判定用
	while (!q.empty()) {
		auto [c, s, t] = q.top(); q.pop();

		// もし辺の両端が既に連結なら繋がない.
		if (d.same(s, t)) continue;

		// そうでないならコスト最小の辺なのでそれで繋ぐ.
		cost += c;
		d.merge(s, t);

		if (msf != nullptr) {
			(*msf)[s].push_back({ t, c });
			(*msf)[t].push_back({ s, c });
		}
	}

	// 連結成分のそれぞれが最小全域木なので,その代表元を記録.
	if (rs != nullptr) {
		rs->clear();
		repe(tmp, d.groups()) rs->push_back(tmp[0]);
	}

	return cost;
}


mint naive(int n, int k, vl a) {
	vi p(n);
	iota(all(p), 0);

	mint res = 0;

	repp(p) {
		WGraph g(n);
		rep(i, n) repi(j, i + 1, n - 1) {
			if (j - i <= k) {
				g[i].push_back({ j, min(a[p[i]], a[p[j]]) });
			}
		}

		res += kruskal(g);
	}

	return res;
}


mint TLE(int n, int k, vl a) {
	sort(all(a));

	vi p(n);
	iota(all(p), 0);

	vvi cnt(n, vi(n));

	repp(p) {
		WGraph g(n);
		rep(i, n) repi(j, i + 1, n - 1) {
			if (j - i <= k) {
				g[i].push_back({ j, min(p[i], p[j]) });
			}
		}

		WGraph mst;
		kruskal(g, &mst);

		rep(s, n) repe(t, mst[s]) {
			if (s < t) {
				int u = p[s];
				int v = p[t];
				if (u > v) swap(u, v);
				cnt[u][v]++;
			}
		}
	}
	dumpel(cnt);

	mint res = 0;
	rep(i, n) rep(j, n) res += cnt[i][j] * a[i];
		
	return res;
}


// 0 2 4 3 1 のときに 2-3 が繋がれないのをうっかり見落としていた.
mint WA(int n, int k, vl a) {
	sort(all(a));

	mint res = 0;

	vvm cnt(n, vm(n));

	rep(i, n) repi(j, i + 1, n - 1) {
		int wmax = min({ 2 * k, n - 2, n - i });
		repi(w, k + 1, wmax) repi(l, 1, n - 1 - w) {
			mint c = 2 * fm.perm(n - w, i) * fm.fact(n - 2 - i);
			dump("(i,j,w,l):", i, j, w, l, "->", c);
			res += c * a[i];

			cnt[i][j] += c;
		}
	}

	rep(i, n) repi(j, i + 1, n - 1) {
		int wmax = min({ 2 * k, n - 1, n - i });
		repi(r, k + 1, wmax) {
			mint c = (2 * k - r + 1) * 2 * fm.perm(n - r, i) * fm.fact(n - 2 - i);
			dump("(i,j,r):", i, j, r, "->", c);
			res += c * a[i] * 2;

			cnt[i][j] += c * 2;
		}
	}

	rep(i, n) repi(j, i + 1, n - 1) {
		repi(s, n - k - 1, k - 1) repi(t, s + 1, k) {
			mint c = 2 * fm.perm(n - n, i) * fm.fact(n - 2 - i);
			dump("(i,j,s,t):", i, j, s, t, "->", c);
			res += c * a[i];

			cnt[i][j] += c;
		}
	}

	dumpel(cnt);

	return res;
}


void zikken() {
	int k = 2;

	repi(n, 1, 7) {
		vi p(n);
		iota(all(p), 0);

		vi cnt(n);

		repp(p) {
			WGraph g(n);
			rep(i, n) repi(j, i + 1, n - 1) {
				if (j - i <= k) {
					g[i].push_back({ j, min(p[i], p[j]) });
				}
			}

			WGraph mst;
			kruskal(g, &mst);

			rep(s, n) repe(t, mst[s]) {
				if (s < t) {
					int u = p[s];
					int v = p[t];
					if (u > v) swap(u, v);
					cnt[u]++;
				}
			}
		}
		dump(n, ":", cnt);
	}

	exit(0);
}
/*
1 : 0
2 : 2 0
3 : 12 0 0
4 : 60 12 0 0
5 : 336 120 24 0 0
6 : 2160 1008 360 72 0 0
7 : 15840 8640 4032 1440 288 0 0
*/


// MST の作り方が複数ある場合にダブルカウントしてしまっている.
mint WA2(int n, int k, vl a) {
	sort(all(a));
		
	vvm cnt(n, vm(n));

	rep(i, n) repi(j, i + 1, n - 1) {
		rep(l, n) repi(r, l + 1, n - 1) {
			int w = r - l;
			if (w > k) continue;

			rep(l2, l) repi(r2, r + 1, n - 1) {
				if (max(l2 + k, l) >= min(r2 - k, r)) continue;

				mint c = fm.perm(i, 2) * fm.perm((l2 - 0) + (n - 1 - r2), i - 2) * fm.fact(n - 2 - i);
				if (c == 0) continue;

				dump("i,j,l,r,l2,r2:", i, j, l, r, l2, r2, "->", c);

				cnt[i][j] += c * 2;
			}

			repi(r2, r + 1, n - 1) {
				if (l >= min(r2 - k, r)) continue;

				mint c = fm.perm(i, 1) * fm.perm(n - 1 - r2, i - 1) * fm.fact(n - 2 - i);
				if (c == 0) continue;
				
				dump("i,j,l,r,   r2:", i, j, l, r, r2, "->", c);

				cnt[i][j] += c * 2;
			}

			rep(l2, l) {
				if (max(l2 + k, l) >= r) continue;

				mint c = fm.perm(i, 1) * fm.perm(l2 - 0, i - 1) * fm.fact(n - 2 - i);
				if (c == 0) continue;
				
				dump("i,j,l,r,l2   :", i, j, l, r, l2, "->", c);

				cnt[i][j] += c * 2;
			}

			mint c = fm.perm(0, i) * fm.fact(n - 2 - i);
			if (c == 0) continue;
			
			dump("i,j,l,r      :", i, j, l, r, "->", c);

			cnt[i][j] += c * 2;
		}
	}

	dumpel(cnt);

	mint res = 0;
	rep(i, n) rep(j, n) res += cnt[i][j] * a[i];

	return res;
}


mint solve(int n, int k, vl a) {
	sort(all(a));

	mint res = 0;

	if (n - 1 - 2 * (k - 1) > 0) {
		repi(x, 0, n - k - 1) {
			int wmax = min(2 * k - 1, n - x);
			mint c = -2 * mint(n - wmax) * (1 + n + wmax + mint(wmax) * x) / (1 + x) / (2 + x) * fm.perm(-1 + n - wmax, x);
			res += c * fm.fact(n - 1 - x) * a[x];
		}
		dump(res);

		repi(x, 0, n - k - 1) {
			int wmax = min(2 * k - 1, n - x);
			mint c = -2 * mint(k - n) * (1 + k + n + mint(k) * x) / (1 + x) / (2 + x) * fm.perm(-1 - k + n, x);
			res += c * fm.fact(n - 1 - x) * a[x];
		}
		dump(res);

		repi(x, 0, n - 1) {
			res += mint(n - 1 - 2 * (k - 1)) * 2 * k * fm.perm(n - 2 * k, x) * fm.fact(n - 1 - x) * a[x];
		}
		dump(res);
	}
	else {
		repi(x, 0, n - k - 1) {
			int wmax = min(n - 1, n - x);
			mint c = -2 * mint(n - wmax) * (1 + n + wmax + mint(wmax) * x) / (1 + x) / (2 + x) * fm.perm(-1 + n - wmax, x);
			res += c * fm.fact(n - 1 - x) * a[x];
		}
		dump(res);

		repi(x, 0, n - k - 1) {
			int wmax = min(n - 1, n - x);
			mint c = -2 * mint(k - n) * (1 + k + n + mint(k) * x) / (1 + x) / (2 + x) * fm.perm(-1 - k + n, x);
			res += c * fm.fact(n - 1 - x) * a[x];
		}
		dump(res);

		repi(x, 0, n - 1) {
			res += mint((n - 1) - 2 * ((n - 1) - (k + 1) + 1)) * n * fm.perm(n - n, x) * fm.fact(n - 1 - x) * a[x];
		}
		dump(res);
	}

	return res;
}


int main() {
//	input_from_file("input.txt");
//	output_to_file("output.txt");

//	zikken();

	int n, k;
	cin >> n >> k;

	vl a(n);
	cin >> a;

//	dump(TLE(n, k, a)); dump("-----");
//	dump(TLE2(n, k, a)); exit(0);

	cout << solve(n, k, a) << endl;	
}
0