結果
問題 | No.1193 Penguin Sequence |
ユーザー | yukudo |
提出日時 | 2020-09-05 23:12:03 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 243 ms / 2,000 ms |
コード長 | 4,056 bytes |
コンパイル時間 | 1,811 ms |
コンパイル使用メモリ | 183,532 KB |
実行使用メモリ | 41,744 KB |
最終ジャッジ日時 | 2024-11-29 06:17:31 |
合計ジャッジ時間 | 11,736 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 237 ms
41,744 KB |
testcase_01 | AC | 239 ms
41,740 KB |
testcase_02 | AC | 242 ms
41,692 KB |
testcase_03 | AC | 234 ms
41,744 KB |
testcase_04 | AC | 231 ms
41,744 KB |
testcase_05 | AC | 241 ms
41,744 KB |
testcase_06 | AC | 238 ms
41,616 KB |
testcase_07 | AC | 233 ms
41,740 KB |
testcase_08 | AC | 231 ms
41,728 KB |
testcase_09 | AC | 234 ms
41,644 KB |
testcase_10 | AC | 243 ms
41,612 KB |
testcase_11 | AC | 156 ms
38,672 KB |
testcase_12 | AC | 153 ms
38,548 KB |
testcase_13 | AC | 207 ms
40,568 KB |
testcase_14 | AC | 199 ms
40,336 KB |
testcase_15 | AC | 225 ms
41,228 KB |
testcase_16 | AC | 207 ms
41,744 KB |
testcase_17 | AC | 50 ms
34,836 KB |
testcase_18 | AC | 65 ms
35,288 KB |
testcase_19 | AC | 234 ms
41,564 KB |
testcase_20 | AC | 188 ms
40,076 KB |
testcase_21 | AC | 164 ms
39,056 KB |
testcase_22 | AC | 69 ms
35,344 KB |
testcase_23 | AC | 144 ms
38,288 KB |
testcase_24 | AC | 129 ms
37,776 KB |
testcase_25 | AC | 86 ms
36,112 KB |
testcase_26 | AC | 59 ms
35,092 KB |
testcase_27 | AC | 201 ms
40,552 KB |
testcase_28 | AC | 151 ms
38,796 KB |
testcase_29 | AC | 216 ms
40,632 KB |
testcase_30 | AC | 111 ms
36,624 KB |
testcase_31 | AC | 102 ms
36,240 KB |
testcase_32 | AC | 180 ms
39,312 KB |
testcase_33 | AC | 133 ms
37,900 KB |
testcase_34 | AC | 117 ms
37,392 KB |
testcase_35 | AC | 136 ms
38,032 KB |
testcase_36 | AC | 114 ms
37,260 KB |
testcase_37 | AC | 197 ms
40,428 KB |
testcase_38 | AC | 49 ms
34,828 KB |
testcase_39 | AC | 49 ms
34,964 KB |
testcase_40 | AC | 49 ms
34,956 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i) #define ALL(v) (v).begin(),(v).end() #define CLR(t,v) memset(t,(v),sizeof(t)) template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";} template<class T>void chmin(T&a,const T&b){if(a>b)a=b;} template<class T>void chmax(T&a,const T&b){if(a<b)a=b;} #ifdef LOCAL template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cerr<<(*i)<<" ";cerr<<endl;} #else template<class T>void pv(T a,T b){} #endif ll nextLong() { ll x; scanf("%lld", &x); return x;} const ll MOD = 998244353; struct mint { ll x; mint(ll x=0):x((x%MOD+MOD)%MOD){} mint& operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;} mint& operator-=(const mint a) {if ((x += MOD-a.x) >= MOD) x -= MOD;return *this;} mint& operator*=(const mint a) {(x *= a.x) %= MOD;return *this;} mint operator+(const mint a) const {mint res(*this);return res+=a;} mint operator-(const mint a) const {mint res(*this);return res-=a;} mint operator*(const mint a) const {mint res(*this);return res*=a;} mint pow(ll b) const { mint res(1), a(*this); while (b) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } // for prime MOD mint inv() const {return pow(MOD-2);} mint& operator/=(const mint a) {return (*this) *= a.inv();} mint operator/(const mint a) const {mint res(*this);return res/=a;} }; ostream& operator<<(ostream& os, const mint& a) {os << a.x; return os;} vector<int> normalize(const vector<ll> v) { const int N = v.size(); vector<int> res(N); vector<pair<ll,int> > t(N); REP(i, N) t[i] = { v[i], i }; sort(ALL(t)); int r = 0; REP(i, N) { r += (i > 0 && t[i - 1].first != t[i].first); res[t[i].second] = r; } return res; } struct BIT { using Val = int; int n; vector<Val> tree; BIT(int n):n(n),tree(n+1) {} void fill(Val val) { REP(i, n+1) tree[i] = (i&-i) * val; } void add(int idx, Val val) { for (int x=idx+1; x<=n; x+=x&-x) tree[x] += val; } // sum[0, idx] Val range(int idx) { Val sum=0; for (int x=idx+1; x>0; x-=x&-x) sum += tree[x]; return sum; } // sum[a, b] Val range(int a, int b) { return range(b) - range(a - 1); } }; const int MAX_N = 212345; mint P[MAX_N]; mint M[MAX_N]; mint L[MAX_N]; const int SZ = 1123456; ll inv[SZ]; ll fact[SZ]; ll fact_inv[SZ]; mint choose(int n, int r) { if (n < 0 || r < 0 || n < r) return 0; ll res = fact[n] * fact_inv[n-r] % MOD * fact_inv[r] % MOD; return mint(res); } int main2() { int N = nextLong(); vector<ll> A_(N); REP(i, N) A_[i] = nextLong(); vector<int> A = normalize(A_); const int R = *max_element(ALL(A)) + 1; pv(ALL(A)); mint ans = 0; P[N+1] = 0; for (int n = N; n >= 1; n--) { P[n] = P[n+1] + (mint(n) / mint(N)); } { REP(i, MAX_N) M[i] = 0; vector<int> sorted = A; sort(ALL(sorted)); REP(i, R) M[i] = lower_bound(ALL(sorted), i) - sorted.begin(); pv(M, M+R); } { mint sm = 0; for (int i = 0; i < N; i++) sm += M[A[i]]; mint sp = 0; for (int n = 1; n <= N; n++) sp += mint(n) / mint(N) * P[n+1]; ans += sm * sp; } { REP(i, MAX_N) L[i] = 0; BIT bit(N+1); for (int i = 0; i < N; i++) { L[i] = i - bit.range(A[i]); bit.add(A[i], 1); } } { mint sn = 0; for (int n = 1; n <= N; n++) sn += mint(n-1) / mint(N-1) * mint(n) / mint(N); mint sl = 0; for (int i = 0; i < N; i++) sl += L[i]; ans += sn * sl; } for (int n = 1; n <= N; n++) ans *= choose(N, n); cout << ans << endl; return 0; } int main() { inv[1] = 1; for (int i = 2; i < SZ; i++) inv[i] = inv[(int) (MOD % i)] * (MOD - MOD / i) % MOD; fact[0] = 1; for (int i = 1; i < SZ; i++) fact[i] = fact[i-1] * i % MOD; fact_inv[0] = 1; for (int i = 1; i < SZ; i++) fact_inv[i] = fact_inv[i-1] * inv[i] % MOD; #ifdef LOCAL for (;!cin.eof();cin>>ws) #endif main2(); return 0; }