結果

問題 No.1193 Penguin Sequence
ユーザー carrot46carrot46
提出日時 2020-08-22 16:52:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,168 bytes
コンパイル時間 1,551 ms
コンパイル使用メモリ 174,860 KB
実行使用メモリ 108,528 KB
最終ジャッジ日時 2024-04-23 11:00:28
合計ジャッジ時間 61,582 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 882 ms
17,592 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 AC 1,447 ms
86,816 KB
testcase_12 AC 1,443 ms
86,820 KB
testcase_13 TLE -
testcase_14 AC 1,948 ms
94,528 KB
testcase_15 TLE -
testcase_16 AC 861 ms
7,936 KB
testcase_17 AC 11 ms
6,400 KB
testcase_18 AC 144 ms
17,488 KB
testcase_19 TLE -
testcase_20 AC 1,875 ms
92,588 KB
testcase_21 AC 1,530 ms
86,788 KB
testcase_22 AC 185 ms
19,320 KB
testcase_23 AC 1,126 ms
62,048 KB
testcase_24 AC 972 ms
56,952 KB
testcase_25 AC 402 ms
31,456 KB
testcase_26 AC 93 ms
13,308 KB
testcase_27 TLE -
testcase_28 AC 1,457 ms
86,848 KB
testcase_29 TLE -
testcase_30 AC 643 ms
45,908 KB
testcase_31 AC 462 ms
34,596 KB
testcase_32 AC 1,601 ms
86,860 KB
testcase_33 AC 987 ms
58,388 KB
testcase_34 AC 816 ms
51,460 KB
testcase_35 AC 1,039 ms
59,040 KB
testcase_36 AC 771 ms
50,416 KB
testcase_37 TLE -
testcase_38 AC 10 ms
6,528 KB
testcase_39 AC 10 ms
6,528 KB
testcase_40 AC 10 ms
6,400 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));
void makefact(){
    reps(i,2,MAX_N) fact[i] = fact[i-1] * mint(i);
}
mint nCm(ll n, ll m){
    return fact[n] / (fact[n-m] * fact[m]);
}

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);
    rep(i, N) comb_mul *= nCm(N, i + 1);

    reps(i, 2, N + 1){
        in_group += nCm(N - 2, i - 2) / nCm(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(N, i);
    bet_group *= temp;
    cout<<(in_group + bet_group)<<endl;
    //cout<<in_group<<' '<<bet_group<<endl;
}
0