結果

問題 No.2616 中央番目の中央値
ユーザー maeshunmaeshun
提出日時 2024-01-26 22:30:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 3,482 bytes
コンパイル時間 4,613 ms
コンパイル使用メモリ 269,032 KB
実行使用メモリ 12,300 KB
最終ジャッジ日時 2024-01-26 22:30:37
合計ジャッジ時間 8,659 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 3 ms
6,676 KB
testcase_14 AC 4 ms
6,676 KB
testcase_15 AC 5 ms
6,676 KB
testcase_16 AC 10 ms
6,676 KB
testcase_17 AC 14 ms
6,676 KB
testcase_18 AC 22 ms
6,676 KB
testcase_19 AC 42 ms
6,676 KB
testcase_20 AC 43 ms
6,676 KB
testcase_21 AC 89 ms
8,796 KB
testcase_22 AC 139 ms
11,248 KB
testcase_23 AC 147 ms
11,136 KB
testcase_24 AC 125 ms
12,300 KB
testcase_25 AC 127 ms
12,300 KB
testcase_26 AC 142 ms
12,172 KB
testcase_27 AC 141 ms
11,376 KB
testcase_28 AC 142 ms
11,208 KB
testcase_29 AC 142 ms
12,196 KB
testcase_30 AC 143 ms
11,268 KB
testcase_31 AC 143 ms
12,044 KB
testcase_32 AC 141 ms
12,240 KB
testcase_33 AC 144 ms
11,108 KB
testcase_34 AC 146 ms
11,284 KB
testcase_35 AC 141 ms
11,344 KB
testcase_36 AC 141 ms
11,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i, n) for(int i=0;i<(n);++i)
#define rep1(i, n) for(int i=1;i<=(n);i++)
#define ll long long
using mint = modint998244353;
using P = pair<ll,ll>;
using lb = long double;
using T = tuple<ll, ll, ll>;
#ifdef LOCAL
#  include <debug_print.hpp>
#  define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define dbg(...) (static_cast<void>(0))
#endif

// combination mod prime
// https://youtu.be/8uowVvQ_-Mo?t=6002
// https://youtu.be/Tgd_zLfRZOQ?t=9928
struct modinv {
  int n; vector<mint> d;
  modinv(): n(2), d({0,1}) {}
  mint operator()(int i) {
    while (n <= i) d.push_back(-d[mint::mod()%n]*(mint::mod()/n)), ++n;
    return d[i];
  }
  mint operator[](int i) const { return d[i];}
} invs;
struct modfact {
  int n; vector<mint> d;
  modfact(): n(2), d({1,1}) {}
  mint operator()(int i) {
    while (n <= i) d.push_back(d.back()*n), ++n;
    return d[i];
  }
  mint operator[](int i) const { return d[i];}
} facts;
struct modfactinv {
  int n; vector<mint> d;
  modfactinv(): n(2), d({1,1}) {}
  mint operator()(int i) {
    while (n <= i) d.push_back(d.back()*invs(n)), ++n;
    return d[i];
  }
  mint operator[](int i) const { return d[i];}
} ifacts;
mint comb(int n, int k) {
  if (n < k || k < 0) return 0;
  return facts(n)*ifacts(k)*ifacts(n-k);
}

uint64_t combinations2(uint64_t n, uint64_t k) {
    uint64_t r = 1;
    for (uint64_t d = 1; d <= k; ++d) {
        r *= n--;
        r /= d;
    }
    return r;
}

// vector<vector<mint>> comb(n+1, vector<mint>(n+1));
// rep(i,n+1) comb[i][0] = 1;
// for(int i=1;i<=n;i++){
//     for(int j=1;j<=n;j++){
//         comb[i][j] = comb[i-1][j] + comb[i-1][j-1];
//     }
// }

//Lucasの定理(素数mod)
/* Com:nCk % p の計算のための構造体
    前処理・初期化: O(p^2)
    nCk(n,k): nCk % p の計算。O(log n)
*/
// struct Comb {
//     vector<vector<long long>> com;  // 前計算の結果を保存
//     long long p;                    // p は素数である必要がある
//     Comb(long long _p) : p(_p) {
//         init(p);
//     }
//     void init(long long p) {  // 動的計画法で前処理
//         com.assign(p, vector<long long>(p));
//         com[0][0] = 1;
//         for (int i = 1; i < p; i++) {
//             com[i][0] = 1;
//             for (int j = i; j > 0; j--) {
//                 com[i][j] = (com[i - 1][j - 1] + com[i - 1][j]) % p;
//             }
//         }
//     }
//     long long nCk(long long n, long long k) {
//         long long ret = 1;
//         while (n > 0) {  // 下から一桁ずつ計算する
//             int ni = n % p;
//             int ki = k % p;
//             ret *= com[ni][ki];
//             ret %= p;
//             n /= p;
//             k /= p;
//         }
//         return ret;
//     }
// };

int main()
{
    int n;
    cin >> n;
    vector<int> p(n);
    rep(i,n) cin >> p[i];
    mint ans = 0;
    rep(i,n) p[i]--;
    vector<int> inv(n);
    rep(i,n) inv[p[i]] = i;
    fenwick_tree<int> bit(n);
    rep(v,n){
        int id = inv[v];
        int a = bit.sum(0, id);
        int c = bit.sum(id+1,n);
        int b = id-a;
        int d = n-id-c-1;
        dbg(a,b,c,d);
        mint t1 = facts(2*a+n-id-v-1)*ifacts(a)*ifacts(d);
        mint t2 = facts(2*b+v+1-id-1)*ifacts(b)*ifacts(c);
        ans += t1*t2;
        bit.add(id, 1);
    }
    cout << ans.val() << endl;
    return 0;
}
0