結果

問題 No.1621 Sequence Inversions
ユーザー unyon_maru65536unyon_maru65536
提出日時 2021-07-23 19:00:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 238 ms / 3,000 ms
コード長 4,881 bytes
コンパイル時間 5,152 ms
コンパイル使用メモリ 279,144 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-25 17:50:25
合計ジャッジ時間 7,933 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 57 ms
4,376 KB
testcase_09 AC 65 ms
4,380 KB
testcase_10 AC 127 ms
4,380 KB
testcase_11 AC 59 ms
4,380 KB
testcase_12 AC 229 ms
4,376 KB
testcase_13 AC 238 ms
4,380 KB
testcase_14 AC 40 ms
4,380 KB
testcase_15 AC 27 ms
4,380 KB
testcase_16 AC 63 ms
4,376 KB
testcase_17 AC 125 ms
4,376 KB
testcase_18 AC 62 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 41 ms
4,380 KB
testcase_21 AC 128 ms
4,380 KB
testcase_22 AC 40 ms
4,380 KB
testcase_23 AC 133 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)


template<typename T>
struct FormalPowerSeries : vector<T> {
  using vector<T>::vector;
  using vector<T>::operator=;
  using F = FormalPowerSeries;
  F operator-() const {
    F res(*this);
    for (auto &e : res) e = -e;
    return res;
  }
  F &operator*=(const T &g) {
    for (auto &e : *this) e *= g;
    return *this;
  }
  F &operator/=(const T &g) {
    assert(g != T(0));
    *this *= g.inv();
    return *this;
  }
  F &operator+=(const F &g) {
    int n = (*this).size(), m = g.size();
    (*this).resize(max(n, m));
    rep(i, m) (*this)[i] += g[i];
    return *this;
  }
  F &operator-=(const F &g) {
    int n = (*this).size(), m = g.size();
    (*this).resize(max(n, m));
    rep(i, m) (*this)[i] -= g[i];
    return *this;
  }
  F &operator<<=(int d) {
    (*this).insert((*this).begin(), d, 0);
    return *this;
  }
  F &operator>>=(int d) {
    int n = (*this).size();
    (*this).erase((*this).begin(), (*this).begin() + min(n, d));
    return *this;
  }
  F inv(int d = -1) const {
    int n = (*this).size();
    assert(n != 0 && (*this)[0] != 0);
    if (d == -1) d = n;
    assert(d > 0);
    F res{(*this)[0].inv()};
    while (res.size() < d) {
      int m = size(res);
      F f(begin(*this), begin(*this) + min(n, 2*m));
      F r(res);
      f.resize(2*m), internal::butterfly(f);
      r.resize(2*m), internal::butterfly(r);
      rep(i, 2*m) f[i] *= r[i];
      internal::butterfly_inv(f);
      f.erase(f.begin(), f.begin() + m);
      f.resize(2*m), internal::butterfly(f);
      rep(i, 2*m) f[i] *= r[i];
      internal::butterfly_inv(f);
      T iz = T(2*m).inv(); iz *= -iz;
      for (auto &e : f) e *= iz;
      res.insert(res.end(), f.begin(), f.begin() + m);
    }
    return {res.begin(), res.begin() + d};
  }
  F &operator*=(const F &g) {
    *this = convolution(*this, g);
    return *this;
  }
  F &operator/=(const F &g) {
    int n = (*this).size(), m = g.size();
    *this = convolution(*this, g.inv(max(n, m)));
    (*this).resize(n);
    return *this;
  }
  F operator*(const T &g) const { return F(*this) *= g; }
  F operator/(const T &g) const { return F(*this) /= g; }
  F operator+(const F &g) const { return F(*this) += g; }
  F operator-(const F &g) const { return F(*this) -= g; }
  F operator*(const F &g) const { return F(*this) *= g; }
  F operator/(const F &g) const { return F(*this) /= g; }
  F operator<<(const int d) const { return F(*this) <<= d; }
  F operator>>(const int d) const { return F(*this) >>= d; }
  void multiply_naive(const F &g) {
    int n = size(*this), m = size(g);
    (*this).resize(n+m-1);
    drep(i, n+m-1) {
      (*this)[i] *= g[0];
      rep2(j, 1, min(i+1, m)) (*this)[i] += (*this)[i-j] * g[j];
    }
  }
  void divide_naive(const F &g) {
    assert(g[0] != T(0));
    T ig0 = g[0].inv();
    int n = size(*this), m = size(g);
    rep(i, n) {
      rep2(j, 1, min(i+1, m)) (*this)[i] -= (*this)[i-j] * g[j];
      (*this)[i] *= ig0;
    }
  }
  void multiply(vector<pair<int, T>> g) { // sparse
    int n = (*this).size();
    auto [d, c] = g.front();
    if (d == 0) g.erase(g.begin());
    else c = 0;
    drep(i, n) {
      (*this)[i] *= c;
      for (auto &[j, b] : g) {
        if (j > i) break;
        (*this)[i] += (*this)[i-j] * b;
      }
    }
  }
  void divide(vector<pair<int, T>> g) { // sparse, required: "g[0] == (0, c)" and "c != 0"
    int n = (*this).size();
    auto [d, c] = g.front();
    assert(d == 0 && c != T(0));
    T ic = c.inv();
    g.erase(g.begin());
    rep(i, n) {
      for (auto &[j, b] : g) {
        if (j > i) break;
        (*this)[i] -= (*this)[i-j] * b;
      }
      (*this)[i] *= ic;
    }
  }
  void multiply(const int d, const T c) { // multiply (1 + cz^d)
    int n = (*this).size();
    drep(i, n-d) (*this)[i+d] += (*this)[i] * c;
  }
  void divide(const int d, const T c) { // divide by (1 + cz^d)
    int n = (*this).size();
    rep(i, n-d) (*this)[i+d] -= (*this)[i] * c;
  }
  T eval(const T &a) {
    T x(1), res(0);
    for (auto e : *this) res += e * x, x *= a;
    return res;
  }
};

using mint = modint998244353;
using fps = FormalPowerSeries<mint>;
using sfps = vector<pair<int, mint>>;

int main() {
  int n, k; cin >> n >> k;
  vector<int> A(n);
  map<int,int> m;
  rep(i,n){
      cin >> A[i];
      m[A[i]]++;
  }
  fps ans = {1};
  ans.resize(k+1);
  vector<int> cnt(n);
  int now = 0;
  for(auto [x,y]:m){
    rep(j,y){
        cnt[now++]++;
        cnt[j]--;
    }
  }
  fps f = {};
  rep(i,n){
    f.push_back(1);
    if(cnt[i]<0){
      rep(j,-cnt[i])ans/=f;
    }
    else{
      rep(j,cnt[i])ans*=f;
    }
  }
  cout << ans[k].val() << '\n';
}
0