結果

問題 No.1193 Penguin Sequence
ユーザー carrot46carrot46
提出日時 2020-08-22 16:55:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,298 bytes
コンパイル時間 1,944 ms
コンパイル使用メモリ 175,016 KB
実行使用メモリ 111,660 KB
最終ジャッジ日時 2024-04-23 11:03:50
合計ジャッジ時間 69,267 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 789 ms
20,548 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,612 ms
89,948 KB
testcase_12 AC 1,655 ms
90,020 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 AC 749 ms
10,992 KB
testcase_17 AC 13 ms
9,472 KB
testcase_18 AC 184 ms
20,496 KB
testcase_19 TLE -
testcase_20 TLE -
testcase_21 AC 1,738 ms
89,900 KB
testcase_22 AC 221 ms
22,452 KB
testcase_23 AC 1,342 ms
65,172 KB
testcase_24 AC 1,175 ms
60,080 KB
testcase_25 AC 502 ms
34,716 KB
testcase_26 AC 111 ms
16,308 KB
testcase_27 TLE -
testcase_28 AC 1,661 ms
89,996 KB
testcase_29 TLE -
testcase_30 AC 789 ms
49,068 KB
testcase_31 AC 606 ms
37,852 KB
testcase_32 AC 1,914 ms
89,988 KB
testcase_33 AC 1,189 ms
61,772 KB
testcase_34 AC 987 ms
54,464 KB
testcase_35 AC 1,224 ms
62,172 KB
testcase_36 AC 949 ms
53,552 KB
testcase_37 TLE -
testcase_38 AC 14 ms
9,460 KB
testcase_39 AC 13 ms
9,600 KB
testcase_40 AC 13 ms
9,504 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)), memo(MAX_N, 0);
void makefact(){
    reps(i,2,MAX_N) fact[i] = fact[i-1] * mint(i);
}
mint nCm(ll n, ll m){
    if(n == N && memo[m].x != 0) return memo[m];
    if(n == N) return memo[m] = fact[n] / (fact[n-m] * fact[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