結果

問題 No.1193 Penguin Sequence
ユーザー cn_449cn_449
提出日時 2020-08-22 14:48:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 4,276 bytes
コンパイル時間 1,601 ms
コンパイル使用メモリ 147,500 KB
実行使用メモリ 36,916 KB
最終ジャッジ日時 2023-08-05 12:03:22
合計ジャッジ時間 12,713 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 227 ms
36,684 KB
testcase_01 AC 359 ms
36,792 KB
testcase_02 AC 352 ms
36,696 KB
testcase_03 AC 356 ms
36,820 KB
testcase_04 AC 367 ms
36,884 KB
testcase_05 AC 364 ms
36,788 KB
testcase_06 AC 361 ms
36,916 KB
testcase_07 AC 339 ms
36,872 KB
testcase_08 AC 354 ms
36,748 KB
testcase_09 AC 351 ms
36,872 KB
testcase_10 AC 349 ms
36,880 KB
testcase_11 AC 189 ms
28,932 KB
testcase_12 AC 199 ms
29,008 KB
testcase_13 AC 300 ms
34,904 KB
testcase_14 AC 279 ms
34,232 KB
testcase_15 AC 336 ms
36,072 KB
testcase_16 AC 153 ms
24,388 KB
testcase_17 AC 26 ms
19,696 KB
testcase_18 AC 43 ms
20,900 KB
testcase_19 AC 345 ms
36,696 KB
testcase_20 AC 274 ms
33,728 KB
testcase_21 AC 214 ms
30,032 KB
testcase_22 AC 48 ms
21,632 KB
testcase_23 AC 177 ms
28,316 KB
testcase_24 AC 158 ms
27,612 KB
testcase_25 AC 80 ms
23,152 KB
testcase_26 AC 38 ms
20,744 KB
testcase_27 AC 295 ms
34,580 KB
testcase_28 AC 197 ms
29,392 KB
testcase_29 AC 308 ms
35,044 KB
testcase_30 AC 108 ms
24,456 KB
testcase_31 AC 92 ms
23,788 KB
testcase_32 AC 240 ms
32,552 KB
testcase_33 AC 161 ms
27,776 KB
testcase_34 AC 134 ms
26,528 KB
testcase_35 AC 164 ms
28,012 KB
testcase_36 AC 132 ms
26,564 KB
testcase_37 AC 286 ms
34,388 KB
testcase_38 AC 25 ms
19,620 KB
testcase_39 AC 26 ms
19,188 KB
testcase_40 AC 26 ms
19,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << x << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

constexpr ll mod = 998244353;
vector<ll> fac, inv, facinv;

void modcalc(int n) {
	fac.resize(n); inv.resize(n); facinv.resize(n);
	fac[0] = 1; fac[1] = 1; inv[1] = 1;
	facinv[0] = 1; facinv[1] = 1;
	for (ll i = 2; i < n; i++) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		facinv[i] = facinv[i - 1] * inv[i] % mod;
	}
}

ll modinv(ll a) {
	a %= mod;
	if (a == 0) abort();
	if (a < (ll)inv.size()) return inv[a];
	ll b = mod, u = 1, v = 0;
	while (b) {
		ll t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= mod;
	if (u < 0) u += mod;
	return u;
}

ll modpow(ll a, ll b, ll m = mod) {
	ll ans = 1;
	a %= m;
	while (b) {
		if (b & 1) ans = ans * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return ans;
}

ll modcomb(ll n, ll k) {
	if (n < 0 || k < 0 || n < k) return 0;
	return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}

ll modperm(ll n, ll k) {
	if (n < 0 || k < 0 || n < k) return 0;
	return fac[n] * facinv[n - k] % mod;
}

ll modhom(ll n, ll k) {
	if (n < 0 || k < 0 || n == 0 && k > 0) return 0;
	if (n == 0 && k == 0) return 1;
	return fac[n + k - 1] * facinv[k] % mod * facinv[n - 1] % mod;
}

template<class T> class segtree {
	int n;
	vector<T> data;
	T id = 0;
	T operation(T a, T b) { return (a + b) % mod; };
public:
	segtree(int _n) {
		n = 1;
		while (n < _n + 2) n <<= 1;
		data = vector<T>(2 * n, id);
	}
	void change(int i, T x) {
		i += n;
		data[i] = x;
		while (i > 1) {
			i >>= 1;
			data[i] = operation(data[i << 1], data[i << 1 | 1]);
		}
	}
	void add(int i, T x) { change(i, data[i + n] + x); }
	T get(int a, int b) {
		T left = id; T right = id;
		a += n; b += n;
		while (a < b) {
			if (a & 1) left = operation(left, data[a++]);
			if (b & 1) right = operation(data[--b], right);
			a >>= 1; b >>= 1;
		}
		return operation(left, right);
	}
	T get_all() { return data[1]; }
	T operator[](int i) { return data[i + n]; }
};

vector<int> shrink(vector<int> vec) {
	int vecsize = vec.size();
	vector<int> tmpvec = vec;
	sort(tmpvec.begin(), tmpvec.end());
	tmpvec.erase(unique(tmpvec.begin(), tmpvec.end()), tmpvec.end());
	vector<int> res(vecsize);
	for (int i = 0; i < vecsize; i++) res[i] = lower_bound(tmpvec.begin(), tmpvec.end(), vec[i]) - tmpvec.begin();
	return res;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);
	modcalc(700010);

	ll n;
	cin >> n;
	vector<int> a(n);
	map<ll, ll> mp;
	rep(i, n) {
		cin >> a[i];
		mp[a[i]]++;
	}
	a = shrink(a);
	segtree<ll> s(n);
	ll inv = 0;
	rep(i, n) {
		inv += s.get(a[i] + 1, n + 1);
		s.add(a[i], 1);
	}
	inv %= mod;
	ll ans = 0;
	ll sum = n * (n - 1) / 2;
	for (auto i : mp) {
		ll v = i.second;
		sum -= v * (v - 1) / 2;
	}
	sum %= mod;
	sum *= modinv(n); sum %= mod;
	for (ll i = 1; i <= n; i++) {
		ll pre = i * (i - 1) / 2; pre %= mod;
		pre *= i; pre %= mod;
		pre *= modinv(n); pre %= mod;
		pre *= sum; pre %= mod;
		ans += pre;
		ans += inv * modcomb(n - 2, i - 2) % mod * modinv(modcomb(n, i)) % mod;
		ans %= mod;
	}
	for (int i = 1; i <= n; i++) {
		ans *= modcomb(n, i);
		ans %= mod;
	}
	cout << ans << '\n';
}
0