Submission #17629051


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

template <int mod = 998244353>
struct NTT {
  int base, maxb, root;
  vector<int> rv, roots, invr;
  NTT() : base(1), rv({0, 1}), roots({0, 1}), invr({0, 1}) {
    assert(mod >= 3 && mod & 1);
    int tmp = mod - 1;
    maxb = 0;
    while (!(tmp & 1)) tmp >>= 1, ++maxb;
    root = 2;
    while (mpow(root, (mod - 1) >> 1) == 1) ++root;
    assert(mpow(root, mod - 1) == 1);
    root = mpow(root, (mod - 1) >> maxb);
  }

  inline int mpow(int x, int n) {
    int res = 1;
    while (n) {
      if (n & 1) res = mul(res, x);
      x = mul(x, x);
      n >>= 1;
    }
    return res;
  }

  inline int inv(int x) { return mpow(x, mod - 2); }

  inline int add(int x, int y) {
    if ((x += y) >= mod) x -= mod;
    return x;
  }

  inline int mul(int x, int y) { return (int)(1LL * x * y % mod); }

  void ensure_base(int nb) {
    if (nb <= base) return;
    rv.resize(1 << nb);
    roots.resize(1 << nb);
    invr.resize(1 << nb);
    for (int i = 0; i < (1 << nb); ++i)
      rv[i] = (rv[i >> 1] >> 1) + ((i & 1) << (nb - 1));
    assert(nb <= maxb);
    while (base < nb) {
      int z = mpow(root, 1 << (maxb - 1 - base)), invz = inv(z);
      for (int i = 1 << (base - 1); i < (1 << base); ++i) {
        roots[i << 1] = roots[i];
        roots[(i << 1) + 1] = mul(roots[i], z);
        invr[i << 1] = invr[i];
        invr[(i << 1) + 1] = mul(invr[i], invz);
      }
      ++base;
    }
  }
  void ntt(vector<int> &a, int n, bool sg = 0) {
    assert((n & (n - 1)) == 0);
    for (int i = 0; i < n; ++i)
      if (i < rv[i]) swap(a[i], a[rv[i]]);
    for (int k = 1; k < n; k <<= 1)
      for (int i = 0; i < n; i += 2 * k)
        for (int j = 0; j < k; ++j) {
          int z = mul(a[i + j + k], (sg ? roots[j + k] : invr[j + k]));
          a[i + j + k] = add(a[i + j], mod - z);
          a[i + j] = add(a[i + j], z);
        }
    int invn = inv(n);
    if (sg)
      for (int i = 0; i < n; ++i) a[i] = mul(a[i], invn);
  }
  template <class T>
  vector<T> multiply(const vector<T> &a, const vector<T> &b) {
    int need = a.size() + b.size() - 1;
    int nb = 1;
    while ((1 << nb) < need) ++nb;
    ensure_base(nb);
    int sz = 1 << nb;
    vector<int> fa(sz, 0), fb(sz, 0);
    for (int i = 0; i < sz; ++i) {
      if (i < a.size()) fa[i] = a[i];
      if (i < b.size()) fb[i] = b[i];
    }
    ntt(fa, sz);
    ntt(fb, sz);
    for (int i = 0; i < sz; ++i) fa[i] = mul(fa[i], fb[i]);
    ntt(fa, sz, 1);
    vector<T> res(need);
    for (int i = 0; i < need; ++i) res[i] = fa[i];
    return res;
  }
};

template <int mod = (int)(998244353)>
struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(int64_t n) const {
    ModInt res(1), mul(x);
    while (n) {
      if (n & 1) res *= mul;
      mul *= mul;
      n >>= 1;
    }
    return res;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};

struct Combination {
  vector<ModInt<>> _fact, _rfact, _inv;
  Combination(long long nsize = 5000000)
      : _fact(nsize + 1), _rfact(nsize + 1), _inv(nsize + 1) {
    _fact[0] = _rfact[nsize] = _inv[0] = 1;
    for (int i = 1; i <= nsize; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[nsize] /= _fact[nsize];
    for (int i = nsize - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for (int i = 1; i <= nsize; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
  inline ModInt<> fact(int k) const { return _fact[k]; }

  inline ModInt<> rfact(int k) const { return _rfact[k]; }

  inline ModInt<> inv(int k) const { return _inv[k]; }

  ModInt<> P(int n, int r) const {
    if (r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }

  ModInt<> C(int p, int q) const {
    if (q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }

  ModInt<> largeC(long long p, long long q) const {
    if (q < 0 || p < q) return 0;
    ModInt<> res = rfact(q);
    for (int i = 0; i < q; ++i) res *= p - i;
    return res;
  }

  // n types,choose r
  ModInt<> H(int n, int r) const {
    if (n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }

  ModInt<> Catalan(int n) {
    // C(2n,n) / (n + 1)
    return fact(2 * n) * rfact(n + 1) * rfact(n);
  }
};
using mint = ModInt<>;

int n, k;
vector<long long> a;
vector<vector<mint>> apow;
Combination com;
NTT ntt;

int main() {
  cin >> n >> k;
  a.resize(n);
  apow.resize(k + 1, vector<mint>(n, 1));
  for (auto &p : a) cin >> p;
  vector<long long> p(k + 1, 0);
  p[0] = n;
  for (int i = 1; i <= k; ++i)
    for (int j = 0; j < n; ++j) {
      apow[i][j] = apow[i - 1][j] * a[j];
      p[i] = ntt.add(p[i], (apow[i][j] * com.rfact(i)).x);
    }
  auto res = ntt.multiply(p, p);
  for (int i = 0; i <= k; ++i) p[i] = com.rfact(i).x;
  auto v = ntt.multiply(p, p);
  for (int i = 1; i <= k; ++i) {
    mint now = res[i];
    for (int j = 0; j < n; ++j) now -= apow[i][j] * v[i];
    now *= com.fact(i);
    cout << now / 2 << endl;
  }
  return 0;
}

Submission Info

Submission Time
Task D - Powers
User m_tsubasa
Language C++ (GCC 9.2.1)
Score 600
Code Size 6455 Byte
Status AC
Exec Time 695 ms
Memory 300248 KiB

Compile Error

./Main.cpp: In instantiation of ‘std::vector<T> NTT<mod>::multiply(const std::vector<T>&, const std::vector<T>&) [with T = long long int; int mod = 998244353]’:
./Main.cpp:214:31:   required from here
./Main.cpp:81:13: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   81 |       if (i < a.size()) fa[i] = a[i];
./Main.cpp:82:13: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   82 |       if (i < b.size()) fb[i] = b[i];

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 30
Set Name Test Cases
Sample 00_sample_01, 00_sample_02, 00_sample_03
All 00_sample_01, 00_sample_02, 00_sample_03, 10_small_01, 10_small_02, 10_small_03, 10_small_04, 10_small_05, 10_small_06, 10_small_07, 10_small_08, 10_small_09, 10_small_10, 20_random_01, 20_random_02, 20_random_03, 20_random_04, 20_random_05, 20_random_06, 20_random_07, 20_random_08, 20_random_09, 20_random_10, 30_max_01, 30_max_02, 30_max_03, 30_max_04, 30_max_05, 31_max_01, 31_max_02
Case Name Status Exec Time Memory
00_sample_01 AC 113 ms 61532 KiB
00_sample_02 AC 107 ms 61624 KiB
00_sample_03 AC 110 ms 61532 KiB
10_small_01 AC 108 ms 62040 KiB
10_small_02 AC 106 ms 61988 KiB
10_small_03 AC 108 ms 61908 KiB
10_small_04 AC 108 ms 61904 KiB
10_small_05 AC 110 ms 61580 KiB
10_small_06 AC 110 ms 61988 KiB
10_small_07 AC 106 ms 61600 KiB
10_small_08 AC 109 ms 61580 KiB
10_small_09 AC 109 ms 61556 KiB
10_small_10 AC 109 ms 62120 KiB
20_random_01 AC 292 ms 134568 KiB
20_random_02 AC 319 ms 146916 KiB
20_random_03 AC 122 ms 64496 KiB
20_random_04 AC 339 ms 152980 KiB
20_random_05 AC 212 ms 93452 KiB
20_random_06 AC 475 ms 208996 KiB
20_random_07 AC 218 ms 100776 KiB
20_random_08 AC 448 ms 197596 KiB
20_random_09 AC 174 ms 73652 KiB
20_random_10 AC 258 ms 108140 KiB
30_max_01 AC 691 ms 299456 KiB
30_max_02 AC 692 ms 298584 KiB
30_max_03 AC 691 ms 299228 KiB
30_max_04 AC 689 ms 296944 KiB
30_max_05 AC 678 ms 293752 KiB
31_max_01 AC 695 ms 300164 KiB
31_max_02 AC 691 ms 300248 KiB