結果

問題 No.1193 Penguin Sequence
ユーザー やむなくやむなく
提出日時 2020-08-22 16:15:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,092 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 178,248 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-23 10:28:27
合計ジャッジ時間 13,625 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 22 ms
5,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 28 ms
5,376 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 62 ms
6,656 KB
testcase_26 AC 15 ms
5,376 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// Created by yamunaku on 2020/08/22.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(ll i = 0; i < (n); i++)
#define repl(i, l, r) for(ll i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

ll modpow(ll x, ll a){
    ll ans = 1;
    while(a){
        if(a & 1) ans = ans * x % MOD;
        a >>= 1;
        x = x * x % MOD;
    }
    return ans;
}

ll inv(ll x){
    return modpow(x, MOD - 2);
}


struct SegmentTree{
    int ide = 0;

    inline void updatef(int &x, int y){
        x += y;
    }

    inline int queryf(int x, int y){
        return x + y;
    }

    int n;
    vector<int> node;

    SegmentTree(int sz){
        n = 1;
        while(n < sz) n <<= 1;
        node.resize(2 * n - 1, ide);
    }

    void update(int k, int x){
        k += n - 1;
        updatef(node[k], x);
        while(k){
            k = (k - 1) / 2;
            node[k] = queryf(node[2 * k + 1], node[2 * k + 2]);
        }
    }

    int get(int a, int b, int k = 0, int l = 0, int r = -1){
        if(r < 0)r = n;
        if(r <= a || b <= l) return ide;
        if(a <= l && r <= b) return node[k];
        int xl = get(a, b, 2 * k + 1, l, (l + r) / 2);
        int xr = get(a, b, 2 * k + 2, (l + r) / 2, r);
        return queryf(xl, xr);
    }
};

int main(){
    //CFS;
    ll n;
    cin >> n;
    if(n == 1){
        cout << 0 << endl;
    }
    vi a(n);
    map<int, ll> c;
    rep(i, n){
        cin >> a[i];
        c[a[i]]++;
    }
    vi idx(n);
    iota(all(idx), 0);
    sort(all(idx), [&](int l, int r){
        return a[l] < a[r];
    });
    ll ans1 = 0;
    SegmentTree seg(n);
    per(i, n){
        ans1 += seg.get(0, idx[i]);
        seg.update(idx[i], 1);
    }
    ll ans2 = 0;
    ll sum = 0;
    for(auto &p : c){
        ans2 += sum * p.second;
        sum += p.second;
    }
    ll k = 1;
    ll cm = 1;
    repl(i, 1, n + 1){
        cm = cm * (n - i + 1) % MOD * inv(i) % MOD;
        k = k * cm % MOD;
    }
    ll sum1 = 0;
    repl(i, 2, n + 1){
        sum1 = (sum1 + i * (i - 1)) % MOD;
    }
    sum1 = sum1 * k % MOD * inv(n * (n - 1) % MOD) % MOD;
    ll sum2 = 0;
    rep(i, n){
        sum2 = (sum2 + (i + 1) * (i + 1) * i / 2) % MOD;
    }
    sum2 = sum2 * k % MOD * inv(n * n % MOD) % MOD;
//    cout << ans1 SP sum1 SP ans2 SP sum2 << endl;
    cout << (ans1 * sum1 % MOD + ans2 * sum2 % MOD) % MOD << endl;
    return 0;
}
0