結果

問題 No.1193 Penguin Sequence
ユーザー chocoruskchocorusk
提出日時 2020-08-22 14:20:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 374 ms / 2,000 ms
コード長 2,596 bytes
コンパイル時間 1,597 ms
コンパイル使用メモリ 142,384 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2024-04-23 08:40:46
合計ジャッジ時間 12,056 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 227 ms
18,304 KB
testcase_01 AC 366 ms
18,176 KB
testcase_02 AC 368 ms
18,176 KB
testcase_03 AC 358 ms
18,048 KB
testcase_04 AC 359 ms
18,048 KB
testcase_05 AC 366 ms
18,176 KB
testcase_06 AC 362 ms
18,176 KB
testcase_07 AC 361 ms
18,048 KB
testcase_08 AC 357 ms
18,304 KB
testcase_09 AC 370 ms
18,048 KB
testcase_10 AC 374 ms
18,048 KB
testcase_11 AC 191 ms
11,648 KB
testcase_12 AC 192 ms
11,776 KB
testcase_13 AC 315 ms
16,256 KB
testcase_14 AC 287 ms
15,104 KB
testcase_15 AC 330 ms
17,408 KB
testcase_16 AC 167 ms
7,936 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 23 ms
5,376 KB
testcase_19 AC 366 ms
17,920 KB
testcase_20 AC 270 ms
14,976 KB
testcase_21 AC 207 ms
12,544 KB
testcase_22 AC 30 ms
5,376 KB
testcase_23 AC 169 ms
11,264 KB
testcase_24 AC 142 ms
10,112 KB
testcase_25 AC 61 ms
6,324 KB
testcase_26 AC 15 ms
5,376 KB
testcase_27 AC 301 ms
16,000 KB
testcase_28 AC 186 ms
12,032 KB
testcase_29 AC 308 ms
16,256 KB
testcase_30 AC 89 ms
7,936 KB
testcase_31 AC 73 ms
7,168 KB
testcase_32 AC 232 ms
13,312 KB
testcase_33 AC 152 ms
10,240 KB
testcase_34 AC 120 ms
9,088 KB
testcase_35 AC 158 ms
10,368 KB
testcase_36 AC 115 ms
8,704 KB
testcase_37 AC 289 ms
15,744 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=998244353;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll inv(ll a){
    return powmod(a, MOD-2);
}
ll f[1000010], invf[1000010];
void fac(int n){
    f[0]=1;
    for(ll i=1; i<=n; i++) f[i]=f[i-1]*i%MOD;
    invf[n]=inv(f[n]);
    for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD;
}
ll comb(int x, int y){
    if(!(0<=y && y<=x)) return 0;
    return f[x]*invf[y]%MOD*invf[x-y]%MOD;
}
template<typename T>
struct BIT{
    vector<T> bit;
    int size;
    BIT(int n):size(n), bit(n+1, 0){}
    T sum(int i){ //[0, i)
        T s=0;
        while(i>0){
            s+=bit[i];
            i-=(i&(-i));
        }
        return s;
    }
    T sum(int l, int r){ //[l, r)
        return sum(r)-sum(l);
    }
    void add(int i, T x){
        i++;
        while(i<=size){
            bit[i]+=x;
            i+=(i&(-i));
        }
    }
};
int main()
{
    int n; cin>>n;
    int a[200020];
    vector<int> va(n);
    map<int, int> mp;
    for(int i=0; i<n; i++){
        cin>>a[i];
        va[i]=a[i];
        mp[a[i]]++;
    }
    sort(va.begin(), va.end());
    va.erase(unique(va.begin(), va.end()), va.end());
    for(int i=0; i<n; i++) a[i]=lower_bound(va.begin(), va.end(), a[i])-va.begin();
    ll x=0;
    int m=va.size();
    BIT<int> bit(m);
    for(int i=0; i<n; i++){
        x+=bit.sum(a[i]+1, m);
        bit.add(a[i], 1);
    }
    x%=MOD;
    fac(n);
    ll tot=1;
    for(int i=1; i<=n; i++) (tot*=comb(n, i))%=MOD;
    ll y=0;
    for(int i=2; i<=n; i++){
        (y+=comb(n-2, i-2)*inv(comb(n, i)))%=MOD;
    }
    ll ans=x*y%MOD*tot%MOD;
    x=comb(n, 2);
    for(auto p:mp) (x+=MOD-comb(p.second, 2))%=MOD;
    ll s1=0, s2=0;
    for(int i=1; i<=n; i++){
        ll z=comb(n-1, i-1)*inv(comb(n, i))%MOD;
        (s1+=z)%=MOD;
        (s2+=z*z)%=MOD;
    }
    ll s=(s1*s1-s2+MOD)%MOD*((MOD+1)/2)%MOD;
    (ans+=x*s%MOD*tot)%=MOD;
    cout<<ans<<endl;
    return 0;
}
0