結果

問題 No.1193 Penguin Sequence
ユーザー carrot46carrot46
提出日時 2020-08-22 17:09:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 5,205 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 183,456 KB
実行使用メモリ 23,364 KB
最終ジャッジ日時 2024-04-23 11:14:45
合計ジャッジ時間 17,371 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 336 ms
23,228 KB
testcase_01 AC 408 ms
23,108 KB
testcase_02 AC 416 ms
23,236 KB
testcase_03 AC 409 ms
23,108 KB
testcase_04 AC 420 ms
23,112 KB
testcase_05 AC 405 ms
23,108 KB
testcase_06 AC 418 ms
23,364 KB
testcase_07 AC 415 ms
23,108 KB
testcase_08 AC 409 ms
23,240 KB
testcase_09 AC 412 ms
23,364 KB
testcase_10 AC 407 ms
23,108 KB
testcase_11 AC 307 ms
16,976 KB
testcase_12 AC 309 ms
17,264 KB
testcase_13 AC 371 ms
20,468 KB
testcase_14 AC 361 ms
19,644 KB
testcase_15 AC 394 ms
22,784 KB
testcase_16 AC 310 ms
14,152 KB
testcase_17 AC 194 ms
9,556 KB
testcase_18 AC 203 ms
10,480 KB
testcase_19 AC 410 ms
22,936 KB
testcase_20 AC 353 ms
19,304 KB
testcase_21 AC 322 ms
17,792 KB
testcase_22 AC 213 ms
10,720 KB
testcase_23 AC 301 ms
16,592 KB
testcase_24 AC 284 ms
15,872 KB
testcase_25 AC 227 ms
12,132 KB
testcase_26 AC 200 ms
10,272 KB
testcase_27 AC 367 ms
20,016 KB
testcase_28 AC 309 ms
17,560 KB
testcase_29 AC 369 ms
20,196 KB
testcase_30 AC 248 ms
13,480 KB
testcase_31 AC 238 ms
12,800 KB
testcase_32 AC 333 ms
18,344 KB
testcase_33 AC 294 ms
16,056 KB
testcase_34 AC 269 ms
14,380 KB
testcase_35 AC 290 ms
16,084 KB
testcase_36 AC 264 ms
14,344 KB
testcase_37 AC 368 ms
20,076 KB
testcase_38 AC 190 ms
9,556 KB
testcase_39 AC 195 ms
9,508 KB
testcase_40 AC 192 ms
9,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <chrono>
#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<58);

class mint{
    public:
        ll x;
        static unsigned long long mod;
        static unsigned long long mod_plus;
        mint(){x = 0;}
        mint(ll _x) : x((_x < 0 ? ((_x += mod_plus) < 0 ? _x + mod_plus : _x) : _x)%mod){}
        /*mint& operator=(const ll &a){
            x = a;
            return *this;
        }*/

        mint operator-(){
            return x == 0 ? 0 : mod - x;
        }
        mint& operator+=(const mint& a){
            if((x += a.x) >= mod) x -= mod;
            return *this;
        }
        mint operator+(const mint& a) const{
            mint res(*this);
            return res += a;
        }
        mint& operator-=(const mint& a){
            if((x -= a.x) < 0) x += mod;
            return *this;
        }
        mint operator-(const mint& a) const{
            mint res(*this);
            return res -= a;
        }
        mint& operator*=(const mint& a){
            (x *= a.x)%=mod;
            return *this;
        }
        /*mint& operator*=(const ll& a){
            mint temp(a);
            return *this *= temp;
        }*/
        mint operator*(const mint& a) const{
            mint res(*this);
            return res *= a;
        }
        /*mint operator*(const ll& a) const{
            mint res(*this);
            return res *= a;
        }*/
        mint pow(unsigned long long pw) const{
            mint res(1), comp(*this);
            while(pw){
                if(pw&1) res *= comp;
                comp *= comp;
                pw >>= 1;
            }
            return res;
        }
        //以下、modが素数のときのみ
        mint inv() const{
            mint res(*this);
            return res.pow(mod - 2);
        }
        mint& operator/=(const mint &a){
            (x *= a.inv().x)%=mod;
            return *this;
        }
        mint operator/(const mint &a) const{
            mint res(*this);
            return res /= a;
        }
};
ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
unsigned long long mint::mod = 998244353;
unsigned long long mint::mod_plus = (LLONG_MAX / mint::mod) * mint::mod;
typedef vector<mint> vm;

const ll MAX_N = ll(4e+5) + 10;
vm fact(MAX_N, mint(1)), fact_inv(MAX_N, mint(1));
void makefact(){
    reps(i,2,MAX_N) {
        fact[i] = fact[i-1] * mint(i);
        fact_inv[i] = fact[i].inv();
    }
}
mint nCm(ll n, ll m){
    return fact[n] * fact_inv[n-m] * fact_inv[m];
}
mint nCm_inv(ll n, ll m){
    return fact[n-m] * fact[m] * fact_inv[n];
}

int main() {
    /*
     * class BIT{
            ll n;
      		unordered_map<int, int> bitree;
        public:
        	BIT(unsigned long _n){
              n = _n;
            }
        	void add(ll id, ll x){
              while(id <= n){
                bitree[id] += x;
                id += id & -id;
              }
              return;
            }
        	ll sum(ll id){
              ll temp = 0;
              while(id > 0){
                temp += bitree[id];
                id -= id & -id;
              }
              return temp;
            }
      };
      */
    class BIT{
            ll n;
      		vector<ll> bitree;
        public:
        	BIT(unsigned long _n) : bitree(_n + 1, 0){
              n = _n;
            }
        	void add(ll id, ll x){
              while(id <= n){
                bitree[id] += x;
                id += id & -id;
              }
              return;
            }
        	ll sum(ll id){
              ll temp = 0;
              while(id > 0){
                temp += bitree[id];
                id -= id & -id;
              }
              return temp;
            }
      };
    makefact();
    cin>>N;
    if(N == 1){
        cout<<0<<endl;
        return 0;
    }
    vec a(N);
    rep(i,N) cin>>a[i];
    vec copy(a);
    sort(ALL(copy));
    copy.erase(unique(ALL(copy)), copy.end());
    unordered_map<int, int> id;
    int rank = 0;
    for(ll temp : copy) id[temp] = ++rank;
    mint tento_sum(0), mini_sum(0), in_group(0), bet_group(1), comb_mul(1), temp(0);
    BIT bit(N + 10);
    rep(i,N){
        tento_sum += i - bit.sum(id[a[i]]);
        bit.add(id[a[i]], 1);
    }
    //rep(i, N) mini_sum += bit.sum(a[i] - 1);
    sort(ALL(a));
    rep(i, N) mini_sum += lower_bound(ALL(a), a[i]) - a.begin();
    rep(i, N) comb_mul *= nCm(N, i + 1);

    reps(i, 2, N + 1){
        in_group += nCm(N - 2, i - 2) * nCm_inv(N, i);
    }
    in_group *= comb_mul * tento_sum;
    (bet_group *= comb_mul * mini_sum) /= N;
    reps(i, 1, N + 1) temp += mint((N * (N + 1) - (ll)i * (i + 1)) / 2) * nCm(N - 1, i - 1) * nCm_inv(N, i);
    bet_group *= temp;
    cout<<(in_group + bet_group)<<endl;
    //cout<<in_group<<' '<<bet_group<<endl;
}
0