Submission #21277581


Source Code Expand

Copy
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};


constexpr unsigned MO = 998244353U;
constexpr unsigned MO2 = 2U * MO;
constexpr int FFT_MAX = 23;
using Mint = ModInt<MO>;
constexpr Mint FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 911660635U, 372528824U, 929031873U, 452798380U, 922799308U, 781712469U, 476477967U, 166035806U, 258648936U, 584193783U, 63912897U, 350007156U, 666702199U, 968855178U, 629671588U, 24514907U, 996173970U, 363395222U, 565042129U, 733596141U, 267099868U, 15311432U};
constexpr Mint INV_FFT_ROOTS[FFT_MAX + 1] = {1U, 998244352U, 86583718U, 509520358U, 337190230U, 87557064U, 609441965U, 135236158U, 304459705U, 685443576U, 381598368U, 335559352U, 129292727U, 358024708U, 814576206U, 708402881U, 283043518U, 3707709U, 121392023U, 704923114U, 950391366U, 428961804U, 382752275U, 469870224U};
constexpr Mint FFT_RATIOS[FFT_MAX - 1] = {911660635U, 509520358U, 369330050U, 332049552U, 983190778U, 123842337U, 238493703U, 975955924U, 603855026U, 856644456U, 131300601U, 842657263U, 730768835U, 942482514U, 806263778U, 151565301U, 510815449U, 503497456U, 743006876U, 741047443U, 56250497U, 867605899U};
constexpr Mint INV_FFT_RATIOS[FFT_MAX - 1] = {86583718U, 372528824U, 373294451U, 645684063U, 112220581U, 692852209U, 155456985U, 797128860U, 90816748U, 860285882U, 927414960U, 354738543U, 109331171U, 293255632U, 535113200U, 308540755U, 121186627U, 608385704U, 438932459U, 359477183U, 824071951U, 103369235U};

// as[rev(i)] <- \sum_j \zeta^(ij) as[j]
void fft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = n;
  if (m >>= 1) {
    for (int i = 0; i < m; ++i) {
      const unsigned x = as[i + m].x;  // < MO
      as[i + m].x = as[i].x + MO - x;  // < 2 MO
      as[i].x += x;  // < 2 MO
    }
  }
  if (m >>= 1) {
    Mint prod = 1;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned x = (prod * as[i + m]).x;  // < MO
        as[i + m].x = as[i].x + MO - x;  // < 3 MO
        as[i].x += x;  // < 3 MO
      }
      prod *= FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  for (; m; ) {
    if (m >>= 1) {
      Mint prod = 1;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i + m].x = as[i].x + MO - x;  // < 4 MO
          as[i].x += x;  // < 4 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
    if (m >>= 1) {
      Mint prod = 1;
      for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
        for (int i = i0; i < i0 + m; ++i) {
          const unsigned x = (prod * as[i + m]).x;  // < MO
          as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
          as[i + m].x = as[i].x + MO - x;  // < 3 MO
          as[i].x += x;  // < 3 MO
        }
        prod *= FFT_RATIOS[__builtin_ctz(++h)];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
    as[i].x = (as[i].x >= MO) ? (as[i].x - MO) : as[i].x;  // < MO
  }
}

// as[i] <- (1/n) \sum_j \zeta^(-ij) as[rev(j)]
void invFft(Mint *as, int n) {
  assert(!(n & (n - 1))); assert(1 <= n); assert(n <= 1 << FFT_MAX);
  int m = 1;
  if (m < n >> 1) {
    Mint prod = 1;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
    m <<= 1;
  }
  for (; m < n >> 1; m <<= 1) {
    Mint prod = 1;
    for (int h = 0, i0 = 0; i0 < n; i0 += (m << 1)) {
      for (int i = i0; i < i0 + (m >> 1); ++i) {
        const unsigned long long y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
        as[i].x += as[i + m].x;  // < 4 MO
        as[i].x = (as[i].x >= MO2) ? (as[i].x - MO2) : as[i].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      for (int i = i0 + (m >> 1); i < i0 + m; ++i) {
        const unsigned long long y = as[i].x + MO - as[i + m].x;  // < 2 MO
        as[i].x += as[i + m].x;  // < 2 MO
        as[i + m].x = (prod.x * y) % MO;  // < MO
      }
      prod *= INV_FFT_RATIOS[__builtin_ctz(++h)];
    }
  }
  if (m < n) {
    for (int i = 0; i < m; ++i) {
      const unsigned y = as[i].x + MO2 - as[i + m].x;  // < 4 MO
      as[i].x += as[i + m].x;  // < 4 MO
      as[i + m].x = y;  // < 4 MO
    }
  }
  const Mint invN = Mint(n).inv();
  for (int i = 0; i < n; ++i) {
    as[i] *= invN;
  }
}

void fft(vector<Mint> &as) {
  fft(as.data(), as.size());
}
void invFft(vector<Mint> &as) {
  invFft(as.data(), as.size());
}

vector<Mint> convolve(vector<Mint> as, vector<Mint> bs) {
  if (as.empty() || bs.empty()) return {};
  const int len = as.size() + bs.size() - 1;
  int n = 1;
  for (; n < len; n <<= 1) {}
  as.resize(n); fft(as);
  bs.resize(n); fft(bs);
  for (int i = 0; i < n; ++i) as[i] *= bs[i];
  invFft(as);
  as.resize(len);
  return as;
}


// polyWork0: operator*, inv, div, divAt, log, exp, pow, sqrt
// polyWork1: inv, div, divAt, log, exp, pow, sqrt
// polyWork2: divAt, exp, pow, sqrt
// polyWork3: exp, pow, sqrt
static constexpr int LIM_POLY = 1 << 20;  // @
static_assert(LIM_POLY <= 1 << FFT_MAX);
Mint polyWork0[LIM_POLY];
// polyWork1[LIM_POLY], polyWork2[LIM_POLY], polyWork3[LIM_POLY];

struct Poly : public vector<Mint> {
  Poly() {}
  explicit Poly(int n) : vector<Mint>(n) {}
  Poly(const vector<Mint> &vec) : vector<Mint>(vec) {}
  Poly(std::initializer_list<Mint> il) : vector<Mint>(il) {}
  int size() const { return vector<Mint>::size(); }
  Mint at(long long k) const { return (0 <= k && k < size()) ? (*this)[k] : 0U; }
  int ord() const { for (int i = 0; i < size(); ++i) if ((*this)[i]) return i; return -1; }
  Poly take(int n) const { return Poly(vector<Mint>(data(), data() + min(n, size()))); }
  friend std::ostream &operator<<(std::ostream &os, const Poly &fs) {
    os << "[";
    for (int i = 0; i < fs.size(); ++i) { if (i > 0) os << ", "; os << fs[i]; }
    return os << "]";
  }

  Poly &operator+=(const Poly &fs) {
    if (size() < fs.size()) resize(fs.size());
    for (int i = 0; i < fs.size(); ++i) (*this)[i] += fs[i];
    return *this;
  }
  Poly &operator-=(const Poly &fs) {
    if (size() < fs.size()) resize(fs.size());
    for (int i = 0; i < fs.size(); ++i) (*this)[i] -= fs[i];
    return *this;
  }
  // 3 E(|t| + |f|)
  Poly &operator*=(const Poly &fs) {
    if (empty() || fs.empty()) return *this = {};
    const int nt = size(), nf = fs.size();
    int n = 1;
    for (; n < nt + nf - 1; n <<= 1) {}
    assert(n <= LIM_POLY);
    resize(n);
    fft(data(), n);  // 1 E(n)
    memcpy(polyWork0, fs.data(), nf * sizeof(Mint));
    memset(polyWork0 + nf, 0, (n - nf) * sizeof(Mint));
    fft(polyWork0, n);  // 1 E(n)
    for (int i = 0; i < n; ++i) (*this)[i] *= polyWork0[i];
    invFft(data(), n);  // 1 E(n)
    resize(nt + nf - 1);
    return *this;
  }
  Poly &operator*=(const Mint &a) {
    for (int i = 0; i < size(); ++i) (*this)[i] *= a;
    return *this;
  }
  Poly &operator/=(const Mint &a) {
    const Mint b = a.inv();
    for (int i = 0; i < size(); ++i) (*this)[i] *= b;
    return *this;
  }
  Poly operator+() const { return *this; }
  Poly operator-() const {
    Poly fs(size());
    for (int i = 0; i < size(); ++i) fs[i] = -(*this)[i];
    return fs;
  }
  Poly operator+(const Poly &fs) const { return (Poly(*this) += fs); }
  Poly operator-(const Poly &fs) const { return (Poly(*this) -= fs); }
  Poly operator*(const Poly &fs) const { return (Poly(*this) *= fs); }
  Poly operator*(const Mint &a) const { return (Poly(*this) *= a); }
  Poly operator/(const Mint &a) const { return (Poly(*this) /= a); }
  friend Poly operator*(const Mint &a, const Poly &fs) { return fs * a; }
};


constexpr int LIM = 500'010;
Mint inv[LIM], fac[LIM], invFac[LIM];

void prepare() {
  inv[1] = 1;
  for (int i = 2; i < LIM; ++i) {
    inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
  }
  fac[0] = invFac[0] = 1;
  for (int i = 1; i < LIM; ++i) {
    fac[i] = fac[i - 1] * i;
    invFac[i] = invFac[i - 1] * inv[i];
  }
}
Mint binom(Int n, Int k) {
  if (0 <= k && k <= n) {
    assert(n < LIM);
    return fac[n] * invFac[k] * invFac[n - k];
  } else {
    return 0;
  }
}


void experiment() {
  for (int n = 3; n <= 8; ++n) {
    vector<int> fac(n + 1);
    fac[0] = 1;
    for (int i = 1; i <= n; ++i) {
      fac[i] = fac[i - 1] * i;
    }
    vector<int> cnt(1 << n, 0), good(1 << n, 0);
    for (int p = 0; p < fac[n - 1]; ++p) {
      vector<int> par(n, -1);
      for (int u = 1; u < n; ++u) {
        par[u] = p / fac[u - 1] % u;
      }
      vector<int> subs(n);
      for (int u = 0; u < n; ++u) {
        subs[u] = 1 << u;
      }
      for (int u = n - 1; u >= 1; --u) {
        subs[par[u]] |= subs[u];
      }
      for (int u = 0; u < n; ++u) {
        ++cnt[subs[u]];
      }
      for (int u = n; u >= 0; --u) {
        if ((subs[u] & 1 << (n - 2)) && subs[u] & 1 << (n - 1)) {
          ++good[subs[u]];
          break;
        }
      }
    }
    cerr << "n = " << n << endl;
    cerr << "cnt = "; pv(cnt.begin(), cnt.end());
    cerr << "good = "; pv(good.begin(), good.end());
    for (int q = 0; q < 1 << n; ++q) {
      int num;
      const int u0 = __builtin_ctz(q);
        if (u0 == 0) {
        if (q == (1 << n) - 1) {
          num = fac[n - 1];
        } else {
          num = 0;
        }
      } else {
        const int k = __builtin_popcount(q);
        num = u0 * fac[k - 1] * fac[n - k - 1];
      }
      assert(cnt[q] == num);
    }
    for (int q = 0; q < 1 << n; ++q) {
      int num;
      if ((q & 1 << (n - 2)) && (q & 1 << (n - 1))) {
        const int u0 = __builtin_ctz(q);
        if (u0 == 0) {
          if (q == (1 << n) - 1) {
            num = fac[n - 1] / 2;
          } else {
            num = 0;
          }
        } else if (u0 == n - 2) {
          num = fac[n - 2];
        } else {
          const int k = __builtin_popcount(q);
          num = u0 * fac[k - 1] * fac[n - k - 1] / 2;
        }
      } else {
        num = 0;
      }
      assert(good[q] == num);
    }
  }
}


int N, K;
vector<int> A;

// (prod, prod with root)
pair<Poly, Poly> dfs(int l, int r) {
  if (r - l == 1) {
    return make_pair(Poly{1, Mint(A[l]).inv()}, Poly{0, 1});
  } else {
    const int mid = (l + r) / 2;
    const auto resL = dfs(l, mid);
    const auto resR = dfs(mid, r);
    return make_pair(resL.first * resR.first, resL.second * resR.first + resR.second);
  }
}

int main() {
  prepare();
  
  for (; ~scanf("%d%d", &N, &K); ) {
    A.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%d", &A[u]);
    }
    sort(A.begin(), A.end());
    
    const auto res = dfs(0, N);
// cerr<<res.first<<" "<<res.second<<endl;
    Mint ans = res.second[K];
    ans /= binom(N, K);
    printf("%u\n", ans.x);
  }
  return 0;
}

Submission Info

Submission Time
Task D - Idol Group Costume
User hos_lyric
Language C++ (GCC 9.2.1)
Score 100
Code Size 14574 Byte
Status AC
Exec Time 223 ms
Memory 12724 KB

Compile Error

./Main.cpp: In member function ‘Poly& Poly::operator*=(const Poly&)’:
./Main.cpp:245:54: warning: ‘void* memset(void*, int, size_t)’ clearing an object of non-trivial type ‘using Mint = struct ModInt<998244353>’ {aka ‘struct ModInt<998244353>’}; use assignment or value-initialization instead [-Wclass-memaccess]
  245 |     memset(polyWork0 + nf, 0, (n - nf) * sizeof(Mint));
      |                                                      ^
./Main.cpp:34:31: note: ‘using Mint = struct ModInt<998244353>’ {aka ‘struct ModInt<998244353>’} declared here
   34 | template <unsigned M_> struct ModInt {
      |                               ^~~~~~
./Main.cpp: In function ‘int main()’:
./Main.cpp:394:12: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  394 |       scanf("%d", &A[u]);
      |       ~~~~~^~~~~~~~~~~~~

Judge Result

Set Name sample small All
Score / Max Score 0 / 0 50 / 50 50 / 50
Status
AC × 2
AC × 19
AC × 44
Set Name Test Cases
sample 00_sample_01, 00_sample_02
small 00_handmade_00, 00_handmade_01, 00_handmade_02, 00_handmade_03, 00_handmade_04, 00_handmade_05, 00_handmade_06, 00_sample_01, 00_sample_02, 00_small_00, 00_small_01, 00_small_02, 00_small_03, 00_small_04, 00_small_05, 00_small_06, 00_small_07, 00_small_08, 00_small_09
All 00_handmade_00, 00_handmade_01, 00_handmade_02, 00_handmade_03, 00_handmade_04, 00_handmade_05, 00_handmade_06, 00_sample_01, 00_sample_02, 00_small_00, 00_small_01, 00_small_02, 00_small_03, 00_small_04, 00_small_05, 00_small_06, 00_small_07, 00_small_08, 00_small_09, 01_handmade_00, 01_handmade_01, 01_handmade_02, 01_handmade_03, 01_handmade_04, 01_largeRandom_00, 01_largeRandom_01, 01_largeRandom_02, 01_largeRandom_03, 01_largeRandom_04, 01_largeRandom_05, 01_largeRandom_06, 01_largeRandom_07, 01_largeRandom_08, 01_largeRandom_09, 01_random_00, 01_random_01, 01_random_02, 01_random_03, 01_random_04, 01_random_05, 01_random_06, 01_random_07, 01_random_08, 01_random_09
Case Name Status Exec Time Memory
00_handmade_00 AC 22 ms 9644 KB
00_handmade_01 AC 15 ms 9564 KB
00_handmade_02 AC 18 ms 9636 KB
00_handmade_03 AC 18 ms 9640 KB
00_handmade_04 AC 22 ms 9612 KB
00_handmade_05 AC 17 ms 9708 KB
00_handmade_06 AC 20 ms 9696 KB
00_sample_01 AC 20 ms 9664 KB
00_sample_02 AC 16 ms 9616 KB
00_small_00 AC 19 ms 9708 KB
00_small_01 AC 14 ms 9568 KB
00_small_02 AC 20 ms 9664 KB
00_small_03 AC 24 ms 9708 KB
00_small_04 AC 18 ms 9680 KB
00_small_05 AC 18 ms 9668 KB
00_small_06 AC 16 ms 9524 KB
00_small_07 AC 17 ms 9580 KB
00_small_08 AC 17 ms 9668 KB
00_small_09 AC 16 ms 9588 KB
01_handmade_00 AC 215 ms 12724 KB
01_handmade_01 AC 217 ms 12680 KB
01_handmade_02 AC 215 ms 12712 KB
01_handmade_03 AC 221 ms 12532 KB
01_handmade_04 AC 221 ms 12688 KB
01_largeRandom_00 AC 219 ms 12724 KB
01_largeRandom_01 AC 222 ms 12688 KB
01_largeRandom_02 AC 221 ms 12640 KB
01_largeRandom_03 AC 220 ms 12540 KB
01_largeRandom_04 AC 223 ms 12676 KB
01_largeRandom_05 AC 222 ms 12712 KB
01_largeRandom_06 AC 220 ms 12676 KB
01_largeRandom_07 AC 221 ms 12688 KB
01_largeRandom_08 AC 221 ms 12672 KB
01_largeRandom_09 AC 221 ms 12712 KB
01_random_00 AC 118 ms 10808 KB
01_random_01 AC 199 ms 12080 KB
01_random_02 AC 123 ms 11408 KB
01_random_03 AC 67 ms 10180 KB
01_random_04 AC 23 ms 9796 KB
01_random_05 AC 135 ms 11516 KB
01_random_06 AC 109 ms 10752 KB
01_random_07 AC 207 ms 12400 KB
01_random_08 AC 117 ms 11232 KB
01_random_09 AC 118 ms 11036 KB