結果

問題 No.1193 Penguin Sequence
ユーザー carrot46carrot46
提出日時 2020-08-22 17:03:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,696 ms / 2,000 ms
コード長 4,420 bytes
コンパイル時間 2,010 ms
コンパイル使用メモリ 178,704 KB
実行使用メモリ 105,520 KB
最終ジャッジ日時 2024-04-23 11:06:49
合計ジャッジ時間 46,775 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 406 ms
20,480 KB
testcase_01 AC 1,639 ms
105,512 KB
testcase_02 AC 1,663 ms
105,520 KB
testcase_03 AC 1,646 ms
105,388 KB
testcase_04 AC 1,647 ms
105,132 KB
testcase_05 AC 1,671 ms
105,260 KB
testcase_06 AC 1,670 ms
105,264 KB
testcase_07 AC 1,670 ms
105,260 KB
testcase_08 AC 1,669 ms
105,256 KB
testcase_09 AC 1,696 ms
105,388 KB
testcase_10 AC 1,676 ms
105,132 KB
testcase_11 AC 920 ms
64,856 KB
testcase_12 AC 953 ms
65,384 KB
testcase_13 AC 1,506 ms
96,212 KB
testcase_14 AC 1,408 ms
92,412 KB
testcase_15 AC 1,635 ms
101,324 KB
testcase_16 AC 361 ms
11,200 KB
testcase_17 AC 207 ms
9,600 KB
testcase_18 AC 280 ms
20,108 KB
testcase_19 AC 1,650 ms
104,212 KB
testcase_20 AC 1,385 ms
90,832 KB
testcase_21 AC 1,259 ms
89,916 KB
testcase_22 AC 299 ms
21,804 KB
testcase_23 AC 918 ms
62,104 KB
testcase_24 AC 800 ms
57,260 KB
testcase_25 AC 432 ms
33,304 KB
testcase_26 AC 256 ms
16,056 KB
testcase_27 AC 1,494 ms
95,032 KB
testcase_28 AC 1,011 ms
66,428 KB
testcase_29 AC 1,518 ms
96,204 KB
testcase_30 AC 596 ms
49,068 KB
testcase_31 AC 468 ms
36,316 KB
testcase_32 AC 1,296 ms
90,116 KB
testcase_33 AC 850 ms
58,572 KB
testcase_34 AC 715 ms
52,156 KB
testcase_35 AC 905 ms
58,968 KB
testcase_36 AC 733 ms
51,244 KB
testcase_37 AC 1,499 ms
93,784 KB
testcase_38 AC 209 ms
9,600 KB
testcase_39 AC 211 ms
9,424 KB
testcase_40 AC 212 ms
9,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <chrono>
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;
            }
      };
    makefact();
    cin>>N;
    if(N == 1){
        cout<<0<<endl;
        return 0;
    }
    vec a(N);
    rep(i,N) cin>>a[i];
    mint tento_sum(0), mini_sum(0), in_group(0), bet_group(1), comb_mul(1), temp(0);
    BIT bit(ll(1e+9) + 10);
    rep(i,N){
        tento_sum += i - bit.sum(a[i]);
        bit.add(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