結果
問題 | No.1193 Penguin Sequence |
ユーザー | cn_449 |
提出日時 | 2020-08-22 14:48:39 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 361 ms / 2,000 ms |
コード長 | 4,276 bytes |
コンパイル時間 | 1,763 ms |
コンパイル使用メモリ | 150,152 KB |
実行使用メモリ | 37,096 KB |
最終ジャッジ日時 | 2024-10-15 09:07:20 |
合計ジャッジ時間 | 12,587 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 236 ms
37,096 KB |
testcase_01 | AC | 358 ms
36,988 KB |
testcase_02 | AC | 359 ms
37,092 KB |
testcase_03 | AC | 361 ms
37,092 KB |
testcase_04 | AC | 355 ms
37,092 KB |
testcase_05 | AC | 357 ms
37,092 KB |
testcase_06 | AC | 348 ms
37,004 KB |
testcase_07 | AC | 354 ms
37,068 KB |
testcase_08 | AC | 347 ms
37,032 KB |
testcase_09 | AC | 357 ms
37,088 KB |
testcase_10 | AC | 350 ms
37,016 KB |
testcase_11 | AC | 193 ms
29,148 KB |
testcase_12 | AC | 199 ms
29,392 KB |
testcase_13 | AC | 306 ms
35,240 KB |
testcase_14 | AC | 282 ms
34,392 KB |
testcase_15 | AC | 325 ms
36,272 KB |
testcase_16 | AC | 157 ms
24,592 KB |
testcase_17 | AC | 26 ms
19,624 KB |
testcase_18 | AC | 45 ms
21,180 KB |
testcase_19 | AC | 338 ms
36,812 KB |
testcase_20 | AC | 264 ms
34,124 KB |
testcase_21 | AC | 209 ms
30,092 KB |
testcase_22 | AC | 50 ms
21,888 KB |
testcase_23 | AC | 173 ms
28,724 KB |
testcase_24 | AC | 154 ms
27,832 KB |
testcase_25 | AC | 79 ms
23,524 KB |
testcase_26 | AC | 38 ms
20,736 KB |
testcase_27 | AC | 291 ms
34,900 KB |
testcase_28 | AC | 200 ms
29,556 KB |
testcase_29 | AC | 298 ms
35,216 KB |
testcase_30 | AC | 105 ms
24,728 KB |
testcase_31 | AC | 91 ms
24,028 KB |
testcase_32 | AC | 227 ms
32,756 KB |
testcase_33 | AC | 163 ms
28,064 KB |
testcase_34 | AC | 130 ms
26,872 KB |
testcase_35 | AC | 165 ms
28,168 KB |
testcase_36 | AC | 130 ms
26,720 KB |
testcase_37 | AC | 282 ms
34,716 KB |
testcase_38 | AC | 25 ms
19,612 KB |
testcase_39 | AC | 26 ms
19,616 KB |
testcase_40 | AC | 27 ms
19,624 KB |
ソースコード
#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'; }