結果

問題 No.1549 [Cherry 2nd Tune] BANning Tuple
ユーザー ei1333333ei1333333
提出日時 2021-06-11 21:54:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 4,000 ms
コード長 16,537 bytes
コンパイル時間 3,524 ms
コンパイル使用メモリ 248,344 KB
実行使用メモリ 6,016 KB
最終ジャッジ日時 2024-05-08 17:35:06
合計ジャッジ時間 8,406 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
5,248 KB
testcase_01 AC 17 ms
5,248 KB
testcase_02 AC 227 ms
5,376 KB
testcase_03 AC 227 ms
5,376 KB
testcase_04 AC 231 ms
5,376 KB
testcase_05 AC 230 ms
5,376 KB
testcase_06 AC 224 ms
5,376 KB
testcase_07 AC 229 ms
5,760 KB
testcase_08 AC 230 ms
5,760 KB
testcase_09 AC 236 ms
6,016 KB
testcase_10 AC 232 ms
5,888 KB
testcase_11 AC 232 ms
5,888 KB
testcase_12 AC 228 ms
5,888 KB
testcase_13 AC 223 ms
5,760 KB
testcase_14 AC 222 ms
5,760 KB
testcase_15 AC 230 ms
5,888 KB
testcase_16 AC 231 ms
5,760 KB
testcase_17 AC 230 ms
5,760 KB
testcase_18 AC 228 ms
5,760 KB
testcase_19 AC 225 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
//const int mod = 1e9 + 7;
const int mod = 998244353;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;


template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) {
  if(b < 0)b *= -1;
  return a > b && (a = b, true);
}

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}


/**
 * @brief Formal-Power-Series(形式的冪級数)
 */
template< typename T >
struct FormalPowerSeries : vector< T > {
  using vector< T >::vector;
  using P = FormalPowerSeries;

  using MULT = function< vector< T >(P, P) >;
  using FFT = function< void(P &) >;
  using SQRT = function< T(T) >;

  static MULT &get_mult() {
    static MULT mult = nullptr;
    return mult;
  }

  static void set_mult(MULT f) {
    get_mult() = f;
  }

  static FFT &get_fft() {
    static FFT fft = nullptr;
    return fft;
  }

  static FFT &get_ifft() {
    static FFT ifft = nullptr;
    return ifft;
  }

  static void set_fft(FFT f, FFT g) {
    get_fft() = f;
    get_ifft() = g;
    if(get_mult() == nullptr) {
      auto mult = [&](P a, P b) {
        int need = a.size() + b.size() - 1;
        int nbase = 1;
        while((1 << nbase) < need) nbase++;
        int sz = 1 << nbase;
        a.resize(sz, T(0));
        b.resize(sz, T(0));
        get_fft()(a);
        get_fft()(b);
        for(int i = 0; i < sz; i++) a[i] *= b[i];
        get_ifft()(a);
        a.resize(need);
        return a;
      };
      set_mult(mult);
    }
  }

  static SQRT &get_sqrt() {
    static SQRT sqr = nullptr;
    return sqr;
  }

  static void set_sqrt(SQRT sqr) {
    get_sqrt() = sqr;
  }

  void shrink() {
    while(this->size() && this->back() == T(0)) this->pop_back();
  }

  P operator+(const P &r) const { return P(*this) += r; }

  P operator+(const T &v) const { return P(*this) += v; }

  P operator-(const P &r) const { return P(*this) -= r; }

  P operator-(const T &v) const { return P(*this) -= v; }

  P operator*(const P &r) const { return P(*this) *= r; }

  P operator*(const T &v) const { return P(*this) *= v; }

  P operator/(const P &r) const { return P(*this) /= r; }

  P operator%(const P &r) const { return P(*this) %= r; }

  P &operator+=(const P &r) {
    if(r.size() > this->size()) this->resize(r.size());
    for(int i = 0; i < r.size(); i++) (*this)[i] += r[i];
    return *this;
  }

  P &operator+=(const T &r) {
    if(this->empty()) this->resize(1);
    (*this)[0] += r;
    return *this;
  }

  P &operator-=(const P &r) {
    if(r.size() > this->size()) this->resize(r.size());
    for(int i = 0; i < r.size(); i++) (*this)[i] -= r[i];
    shrink();
    return *this;
  }

  P &operator-=(const T &r) {
    if(this->empty()) this->resize(1);
    (*this)[0] -= r;
    shrink();
    return *this;
  }

  P &operator*=(const T &v) {
    const int n = (int) this->size();
    for(int k = 0; k < n; k++) (*this)[k] *= v;
    return *this;
  }

  P &operator*=(const P &r) {
    if(this->empty() || r.empty()) {
      this->clear();
      return *this;
    }
    assert(get_mult() != nullptr);
    auto ret = get_mult()(*this, r);
    return *this = P(begin(ret), end(ret));
  }

  P &operator%=(const P &r) {
    return *this -= *this / r * r;
  }

  P operator-() const {
    P ret(this->size());
    for(int i = 0; i < this->size(); i++) ret[i] = -(*this)[i];
    return ret;
  }

  P &operator/=(const P &r) {
    if(this->size() < r.size()) {
      this->clear();
      return *this;
    }
    int n = this->size() - r.size() + 1;
    return *this = (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n);
  }

  P dot(P r) const {
    P ret(min(this->size(), r.size()));
    for(int i = 0; i < ret.size(); i++) ret[i] = (*this)[i] * r[i];
    return ret;
  }

  P pre(int sz) const {
    return P(begin(*this), begin(*this) + min((int) this->size(), sz));
  }

  P operator>>(int sz) const {
    if(this->size() <= sz) return {};
    P ret(*this);
    ret.erase(ret.begin(), ret.begin() + sz);
    return ret;
  }

  P operator<<(int sz) const {
    P ret(*this);
    ret.insert(ret.begin(), sz, T(0));
    return ret;
  }

  P rev(int deg = -1) const {
    P ret(*this);
    if(deg != -1) ret.resize(deg, T(0));
    reverse(begin(ret), end(ret));
    return ret;
  }

  T operator()(T x) const {
    T r = 0, w = 1;
    for(auto &v : *this) {
      r += w * v;
      w *= x;
    }
    return r;
  }

  P diff() const;

  P integral() const;

  // F(0) must not be 0
  P inv_fast() const;

  P inv(int deg = -1) const;

  // F(0) must be 1
  P log(int deg = -1) const;

  P sqrt(int deg = -1) const;

  // F(0) must be 0
  P exp_fast(int deg = -1) const;

  P exp(int deg = -1) const;

  P pow(int64_t k, int deg = -1) const;

  P mod_pow(int64_t k, P g) const;

  P taylor_shift(T c) const;
};


/**
 * @brief Diff ($f'(x)$)
 * @docs docs/diff.md
 */
template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::diff() const {
  const int n = (int) this->size();
  P ret(max(0, n - 1));
  for(int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * T(i);
  return ret;
}

/**
 * @brief Exp ($e^{f(x)}$)
 * @docs docs/exp.md
 */
template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::exp_fast(int deg) const {
  if(deg == -1) deg = this->size();
  assert((*this)[0] == T(0));

  P inv;
  inv.reserve(deg + 1);
  inv.push_back(T(0));
  inv.push_back(T(1));

  auto inplace_integral = [&](P &F) -> void {
    const int n = (int) F.size();
    auto mod = T::get_mod();
    while((int) inv.size() <= n) {
      int i = inv.size();
      inv.push_back((-inv[mod % i]) * (mod / i));
    }
    F.insert(begin(F), T(0));
    for(int i = 1; i <= n; i++) F[i] *= inv[i];
  };

  auto inplace_diff = [](P &F) -> void {
    if(F.empty()) return;
    F.erase(begin(F));
    T coeff = 1, one = 1;
    for(int i = 0; i < (int) F.size(); i++) {
      F[i] *= coeff;
      coeff += one;
    }
  };

  P b{1, 1 < (int) this->size() ? (*this)[1] : 0}, c{1}, z1, z2{1, 1};
  for(int m = 2; m < deg; m *= 2) {
    auto y = b;
    y.resize(2 * m);
    get_fft()(y);
    z1 = z2;
    P z(m);
    for(int i = 0; i < m; ++i) z[i] = y[i] * z1[i];
    get_ifft()(z);
    fill(begin(z), begin(z) + m / 2, T(0));
    get_fft()(z);
    for(int i = 0; i < m; ++i) z[i] *= -z1[i];
    get_ifft()(z);
    c.insert(end(c), begin(z) + m / 2, end(z));
    z2 = c;
    z2.resize(2 * m);
    get_fft()(z2);
    P x(begin(*this), begin(*this) + min< int >(this->size(), m));
    inplace_diff(x);
    x.push_back(T(0));
    get_fft()(x);
    for(int i = 0; i < m; ++i) x[i] *= y[i];
    get_ifft()(x);
    x -= b.diff();
    x.resize(2 * m);
    for(int i = 0; i < m - 1; ++i) x[m + i] = x[i], x[i] = T(0);
    get_fft()(x);
    for(int i = 0; i < 2 * m; ++i) x[i] *= z2[i];
    get_ifft()(x);
    x.pop_back();
    inplace_integral(x);
    for(int i = m; i < min< int >(this->size(), 2 * m); ++i) x[i] += (*this)[i];
    fill(begin(x), begin(x) + m, T(0));
    get_fft()(x);
    for(int i = 0; i < 2 * m; ++i) x[i] *= y[i];
    get_ifft()(x);
    b.insert(end(b), begin(x) + m, end(x));
  }
  return P{begin(b), begin(b) + deg};
}

template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::exp(int deg) const {
  assert((*this)[0] == T(0));
  const int n = (int) this->size();
  if(deg == -1) deg = n;
  if(get_fft() != nullptr) {
    P ret(*this);
    ret.resize(deg, T(0));
    return ret.exp_fast(deg);
  }
  P ret({T(1)});
  for(int i = 1; i < deg; i <<= 1) {
    ret = (ret * (pre(i << 1) + T(1) - ret.log(i << 1))).pre(i << 1);
  }
  return ret.pre(deg);
}


/**
 * @brief Integral ($\int f(x) dx$)
 * @docs docs/integral.md
 */
template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::integral() const {
  const int n = (int) this->size();
  P ret(n + 1);
  ret[0] = T(0);
  for(int i = 0; i < n; i++) ret[i + 1] = (*this)[i] / T(i + 1);
  return ret;
}

/**
 * @brief Inv ($\frac {1} {f(x)}$)
 */
template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::inv_fast() const {
  assert(((*this)[0]) != T(0));

  const int n = (int) this->size();
  P res{T(1) / (*this)[0]};

  for(int d = 1; d < n; d <<= 1) {
    P f(2 * d), g(2 * d);
    for(int j = 0; j < min(n, 2 * d); j++) f[j] = (*this)[j];
    for(int j = 0; j < d; j++) g[j] = res[j];
    get_fft()(f);
    get_fft()(g);
    for(int j = 0; j < 2 * d; j++) f[j] *= g[j];
    get_ifft()(f);
    for(int j = 0; j < d; j++) {
      f[j] = 0;
      f[j + d] = -f[j + d];
    }
    get_fft()(f);
    for(int j = 0; j < 2 * d; j++) f[j] *= g[j];
    get_ifft()(f);
    for(int j = 0; j < d; j++) f[j] = res[j];
    res = f;
  }
  return res.pre(n);
}

template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::inv(int deg) const {
  assert(((*this)[0]) != T(0));
  const int n = (int) this->size();
  if(deg == -1) deg = n;
  if(get_fft() != nullptr) {
    P ret(*this);
    ret.resize(deg, T(0));
    return ret.inv_fast();
  }
  P ret({T(1) / (*this)[0]});
  for(int i = 1; i < deg; i <<= 1) {
    ret = (ret + ret - ret * ret * pre(i << 1)).pre(i << 1);
  }
  return ret.pre(deg);
}

/**
 * @brief Log ($\log {f(x)}$)
 * @docs docs/log.md
 */
template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::log(int deg) const {
  assert((*this)[0] == 1);
  const int n = (int) this->size();
  if(deg == -1) deg = n;
  return (this->diff() * this->inv(deg)).pre(deg - 1).integral();
}

/**
 * @brief Pow ($f(x)^k$)
 */
template< typename T >
typename FormalPowerSeries< T >::P FormalPowerSeries< T >::pow(int64_t k, int deg) const {
  const int n = (int) this->size();
  if(deg == -1) deg = n;
  for(int i = 0; i < n; i++) {
    if((*this)[i] != T(0)) {
      T rev = T(1) / (*this)[i];
      P ret = (((*this * rev) >> i).log() * k).exp() * ((*this)[i].pow(k));
      if(i * k > deg) return P(deg, T(0));
      ret = (ret << (i * k)).pre(deg);
      if(ret.size() < deg) ret.resize(deg, T(0));
      return ret;
    }
  }
  return *this;
}


template< int mod >
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 ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  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; }
};

using modint = ModInt< mod >;

template< typename Mint >
struct NumberTheoreticTransformFriendlyModInt {

  vector< Mint > dw, idw;
  int max_base;
  Mint root;

  NumberTheoreticTransformFriendlyModInt() {
    const unsigned mod = Mint::get_mod();
    assert(mod >= 3 && mod % 2 == 1);
    auto tmp = mod - 1;
    max_base = 0;
    while(tmp % 2 == 0) tmp >>= 1, max_base++;
    root = 2;
    while(root.pow((mod - 1) >> 1) == 1) root += 1;
    assert(root.pow(mod - 1) == 1);
    dw.resize(max_base);
    idw.resize(max_base);
    for(int i = 0; i < max_base; i++) {
      dw[i] = -root.pow((mod - 1) >> (i + 2));
      idw[i] = Mint(1) / dw[i];
    }
  }

  void ntt(vector< Mint > &a) {
    const int n = (int) a.size();
    assert((n & (n - 1)) == 0);
    assert(__builtin_ctz(n) <= max_base);
    for(int m = n; m >>= 1;) {
      Mint w = 1;
      for(int s = 0, k = 0; s < n; s += 2 * m) {
        for(int i = s, j = s + m; i < s + m; ++i, ++j) {
          auto x = a[i], y = a[j] * w;
          a[i] = x + y, a[j] = x - y;
        }
        w *= dw[__builtin_ctz(++k)];
      }
    }
  }

  void intt(vector< Mint > &a, bool f = true) {
    const int n = (int) a.size();
    assert((n & (n - 1)) == 0);
    assert(__builtin_ctz(n) <= max_base);
    for(int m = 1; m < n; m *= 2) {
      Mint w = 1;
      for(int s = 0, k = 0; s < n; s += 2 * m) {
        for(int i = s, j = s + m; i < s + m; ++i, ++j) {
          auto x = a[i], y = a[j];
          a[i] = x + y, a[j] = (x - y) * w;
        }
        w *= idw[__builtin_ctz(++k)];
      }
    }
    if(f) {
      Mint inv_sz = Mint(1) / n;
      for(int i = 0; i < n; i++) a[i] *= inv_sz;
    }
  }

  vector< Mint > multiply(vector< Mint > a, vector< Mint > b) {
    int need = a.size() + b.size() - 1;
    int nbase = 1;
    while((1 << nbase) < need) nbase++;
    int sz = 1 << nbase;
    a.resize(sz, 0);
    b.resize(sz, 0);
    ntt(a);
    ntt(b);
    Mint inv_sz = Mint(1) / sz;
    for(int i = 0; i < sz; i++) a[i] *= b[i] * inv_sz;
    intt(a, false);
    a.resize(need);
    return a;
  }
};


int main() {
  int64 N;
  cin >> N;
  int Q;
  cin >> Q;
  vector< int64 > K(Q);
  vector< int > A(Q), B(Q), S(Q), T(Q);
  for(int i = 0; i < Q; i++) {
    cin >> K[i] >> A[i] >> B[i] >> S[i] >> T[i];
  }

  auto ks{K};
  sort(begin(ks), end(ks));
  ks.erase(unique(begin(ks), end(ks)), end(ks));
  for(int i = 0; i < Q; i++) {
    K[i] = lower_bound(begin(ks), end(ks), K[i]) - begin(ks);
  }

  NumberTheoreticTransformFriendlyModInt< modint > ntt;
  using FPS = FormalPowerSeries< modint >;
  FPS::set_fft([&](FPS &a) { ntt.ntt(a); }, [&](FPS &a) { ntt.intt(a); });
  FPS fps(3001, 1);
  vector< FPS > mark(ks.size(), fps);
  fps = fps.pow(N, 3001);
  auto can = make_v< int >(ks.size(), 3001);
  for(int i = 0; i < Q; i++) {
    fps *= mark[K[i]].inv(3001);
    fps.resize(3001);
    for(int j = A[i]; j <= B[i]; j++) {
      can[K[i]][j] = 1;
    }
    mark[K[i]].assign(3001, modint(0));
    for(int j = 0; j < 3001; j++) {
      if(can[K[i]][j] == 0) mark[K[i]][j] = 1;
    }
    fps *= mark[K[i]];
    fps.resize(3001);
    modint sum = 0;
    for(int j = S[i]; j <= T[i]; j++) {
      sum += fps[j];
    }
    cout << sum << "\n";
  }
}
0