結果

問題 No.2605 Pickup Parentheses
ユーザー SSRSSSRS
提出日時 2024-01-12 21:42:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 15,911 bytes
コンパイル時間 2,855 ms
コンパイル使用メモリ 228,560 KB
実行使用メモリ 12,188 KB
最終ジャッジ日時 2024-09-30 06:19:16
合計ジャッジ時間 6,575 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 3 ms
6,816 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 4 ms
6,820 KB
testcase_07 AC 3 ms
6,816 KB
testcase_08 AC 4 ms
6,820 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 3 ms
6,820 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 4 ms
6,816 KB
testcase_14 AC 4 ms
6,816 KB
testcase_15 AC 4 ms
6,820 KB
testcase_16 AC 2 ms
6,816 KB
testcase_17 AC 3 ms
6,820 KB
testcase_18 AC 74 ms
10,248 KB
testcase_19 AC 37 ms
7,168 KB
testcase_20 AC 9 ms
6,816 KB
testcase_21 AC 6 ms
6,820 KB
testcase_22 AC 10 ms
6,816 KB
testcase_23 AC 74 ms
10,260 KB
testcase_24 AC 10 ms
6,816 KB
testcase_25 AC 78 ms
11,552 KB
testcase_26 AC 79 ms
11,760 KB
testcase_27 AC 73 ms
10,188 KB
testcase_28 AC 11 ms
6,816 KB
testcase_29 AC 19 ms
6,824 KB
testcase_30 AC 41 ms
7,648 KB
testcase_31 AC 4 ms
6,820 KB
testcase_32 AC 1 ms
6,820 KB
testcase_33 AC 87 ms
11,492 KB
testcase_34 AC 10 ms
6,816 KB
testcase_35 AC 2 ms
6,816 KB
testcase_36 AC 9 ms
6,816 KB
testcase_37 AC 78 ms
10,936 KB
testcase_38 AC 37 ms
7,424 KB
testcase_39 AC 17 ms
6,816 KB
testcase_40 AC 72 ms
10,344 KB
testcase_41 AC 75 ms
11,312 KB
testcase_42 AC 75 ms
11,016 KB
testcase_43 AC 19 ms
6,816 KB
testcase_44 AC 35 ms
7,040 KB
testcase_45 AC 18 ms
6,820 KB
testcase_46 AC 17 ms
6,820 KB
testcase_47 AC 3 ms
6,816 KB
testcase_48 AC 72 ms
10,312 KB
testcase_49 AC 74 ms
11,096 KB
testcase_50 AC 36 ms
7,296 KB
testcase_51 AC 2 ms
6,816 KB
testcase_52 AC 38 ms
8,192 KB
testcase_53 AC 73 ms
10,588 KB
testcase_54 AC 38 ms
7,552 KB
testcase_55 AC 75 ms
10,876 KB
testcase_56 AC 18 ms
6,820 KB
testcase_57 AC 2 ms
6,816 KB
testcase_58 AC 128 ms
12,188 KB
testcase_59 AC 76 ms
11,412 KB
testcase_60 AC 77 ms
11,432 KB
testcase_61 AC 79 ms
11,556 KB
testcase_62 AC 81 ms
11,676 KB
testcase_63 AC 79 ms
11,664 KB
testcase_64 AC 88 ms
11,580 KB
testcase_65 AC 77 ms
11,412 KB
testcase_66 AC 112 ms
11,956 KB
testcase_67 AC 78 ms
11,428 KB
testcase_68 AC 78 ms
11,408 KB
testcase_69 AC 2 ms
6,816 KB
testcase_70 AC 3 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
//https://judge.yosupo.jp/submission/101681
template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    static int get_mod() { return mod; }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() { return *this += Mod_Int(1); }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() { return *this -= Mod_Int(1); }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const { return Mod_Int(-x); }

    Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }

    Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }

    Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }

    Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }

    bool operator==(const Mod_Int &p) const { return x == p.x; }

    bool operator!=(const Mod_Int &p) const { return x != p.x; }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) { return os << p.x; }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

using mint = Mod_Int<MOD>;

template <typename T>
struct Number_Theoretic_Transform {
    static int max_base;
    static T root;
    static vector<T> r, ir;

    Number_Theoretic_Transform() {}

    static void init() {
        if (!r.empty()) return;
        int mod = T::get_mod();
        int tmp = mod - 1;
        root = 2;
        while (root.pow(tmp >> 1) == 1) root++;
        max_base = 0;
        while (tmp % 2 == 0) tmp >>= 1, max_base++;
        r.resize(max_base), ir.resize(max_base);
        for (int i = 0; i < max_base; i++) {
            r[i] = -root.pow((mod - 1) >> (i + 2)); // r[i]  := 1 の 2^(i+2) 乗根
            ir[i] = r[i].inverse();                 // ir[i] := 1/r[i]
        }
    }

    static void ntt(vector<T> &a) {
        init();
        int n = a.size();
        assert((n & (n - 1)) == 0);
        assert(n <= (1 << max_base));
        for (int k = n; k >>= 1;) {
            T w = 1;
            for (int s = 0, t = 0; s < n; s += 2 * k) {
                for (int i = s, j = s + k; i < s + k; i++, j++) {
                    T x = a[i], y = w * a[j];
                    a[i] = x + y, a[j] = x - y;
                }
                w *= r[__builtin_ctz(++t)];
            }
        }
    }

    static void intt(vector<T> &a) {
        init();
        int n = a.size();
        assert((n & (n - 1)) == 0);
        assert(n <= (1 << max_base));
        for (int k = 1; k < n; k <<= 1) {
            T w = 1;
            for (int s = 0, t = 0; s < n; s += 2 * k) {
                for (int i = s, j = s + k; i < s + k; i++, j++) {
                    T x = a[i], y = a[j];
                    a[i] = x + y, a[j] = w * (x - y);
                }
                w *= ir[__builtin_ctz(++t)];
            }
        }
        T inv = T(n).inverse();
        for (auto &e : a) e *= inv;
    }

    static vector<T> convolve(vector<T> a, vector<T> b) {
        if (a.empty() || b.empty()) return {};
        int k = (int)a.size() + (int)b.size() - 1, n = 1;
        while (n < k) n <<= 1;
        a.resize(n), b.resize(n);
        ntt(a), ntt(b);
        for (int i = 0; i < n; i++) a[i] *= b[i];
        intt(a), a.resize(k);
        return a;
    }
};

template <typename T>
int Number_Theoretic_Transform<T>::max_base = 0;

template <typename T>
T Number_Theoretic_Transform<T>::root = T();

template <typename T>
vector<T> Number_Theoretic_Transform<T>::r = vector<T>();

template <typename T>
vector<T> Number_Theoretic_Transform<T>::ir = vector<T>();

using NTT = Number_Theoretic_Transform<mint>;

template <typename T>
struct Formal_Power_Series : vector<T> {
    using NTT_ = Number_Theoretic_Transform<T>;
    using vector<T>::vector;

    Formal_Power_Series(const vector<T> &f) : vector<T>(f) {}

    // f(x) mod x^n
    Formal_Power_Series pre(int n) const {
        Formal_Power_Series ret(begin(*this), begin(*this) + min((int)this->size(), n));
        ret.resize(n, 0);
        return ret;
    }

    // f(1/x)x^{n-1}
    Formal_Power_Series rev(int n = -1) const {
        Formal_Power_Series ret = *this;
        if (n != -1) ret.resize(n, 0);
        reverse(begin(ret), end(ret));
        return ret;
    }

    void normalize() {
        while (!this->empty() && this->back() == 0) this->pop_back();
    }

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

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

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

    Formal_Power_Series &operator-=(const T &t) {
        if (this->empty()) this->resize(1, 0);
        *this[0] -= t;
        return *this;
    }

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

    Formal_Power_Series &operator*=(const T &t) {
        for (int i = 0; i < (int)this->size(); i++) (*this)[i] *= t;
        return *this;
    }

    Formal_Power_Series &operator*=(const Formal_Power_Series &g) {
        if (empty(*this) || empty(g)) {
            this->clear();
            return *this;
        }
        return *this = NTT_::convolve(*this, g);
    }

    Formal_Power_Series &operator/=(const T &t) {
        assert(t != 0);
        T inv = t.inverse();
        return *this *= inv;
    }

    // f(x) を g(x) で割った商
    Formal_Power_Series &operator/=(const Formal_Power_Series &g) {
        if (g.size() > this->size()) {
            this->clear();
            return *this;
        }
        int n = this->size(), m = g.size();
        return *this = (rev() * g.rev().inv(n - m + 1)).pre(n - m + 1).rev();
    }

    // f(x) を g(x) で割った余り
    Formal_Power_Series &operator%=(const Formal_Power_Series &g) { return *this -= (*this / g) * g; }

    // f(x)/x^k
    Formal_Power_Series &operator<<=(int k) {
        Formal_Power_Series ret(k, 0);
        ret.insert(end(ret), begin(*this), end(*this));
        return *this = ret;
    }

    // f(x)x^k
    Formal_Power_Series &operator>>=(int k) {
        Formal_Power_Series ret;
        ret.insert(end(ret), begin(*this) + k, end(*this));
        return *this = ret;
    }

    Formal_Power_Series operator+(const T &x) const { return Formal_Power_Series(*this) += x; }

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

    Formal_Power_Series operator-(const T &x) const { return Formal_Power_Series(*this) -= x; }

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

    Formal_Power_Series operator*(const T &x) const { return Formal_Power_Series(*this) *= x; }

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

    Formal_Power_Series operator/(const T &x) const { return Formal_Power_Series(*this) /= x; }

    Formal_Power_Series operator/(const Formal_Power_Series &v) const { return Formal_Power_Series(*this) /= v; }

    Formal_Power_Series operator%(const Formal_Power_Series &v) const { return Formal_Power_Series(*this) %= v; }

    Formal_Power_Series operator<<(int x) const { return Formal_Power_Series(*this) <<= x; }

    Formal_Power_Series operator>>(int x) const { return Formal_Power_Series(*this) >>= x; }

    // f(c)
    T val(const T &c) const {
        T ret = 0;
        for (int i = (int)this->size() - 1; i >= 0; i--) ret *= c, ret += (*this)[i];
        return ret;
    }

    // df/dx
    Formal_Power_Series derivative() const {
        if (empty(*this)) return *this;
        int n = this->size();
        Formal_Power_Series ret(n - 1);
        for (int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * i;
        return ret;
    }

    // ∫f(x)dx
    Formal_Power_Series integral() const {
        if (empty(*this)) return *this;
        int n = this->size();
        vector<T> inv(n + 1, 0);
        inv[1] = 1;
        int mod = T::get_mod();
        for (int i = 2; i <= n; i++) inv[i] = -inv[mod % i] * T(mod / i);
        Formal_Power_Series ret(n + 1, 0);
        for (int i = 0; i < n; i++) ret[i + 1] = (*this)[i] * inv[i + 1];
        return ret;
    }

    // 1/f(x) mod x^n (f[0] != 0)
    Formal_Power_Series inv(int n = -1) const {
        assert((*this)[0] != 0);
        if (n == -1) n = this->size();
        Formal_Power_Series ret(1, (*this)[0].inverse());
        for (int m = 1; m < n; m <<= 1) {
            Formal_Power_Series f = pre(2 * m), g = ret;
            f.resize(2 * m), g.resize(2 * m);
            NTT_::ntt(f), NTT_::ntt(g);
            Formal_Power_Series h(2 * m);
            for (int i = 0; i < 2 * m; i++) h[i] = f[i] * g[i];
            NTT_::intt(h);
            for (int i = 0; i < m; i++) h[i] = 0;
            NTT_::ntt(h);
            for (int i = 0; i < 2 * m; i++) h[i] *= g[i];
            NTT_::intt(h);
            for (int i = 0; i < m; i++) h[i] = 0;
            ret -= h;
        }
        ret.resize(n);
        return ret;
    }

    // log(f(x)) mod x^n (f[0] = 1)
    Formal_Power_Series log(int n = -1) const {
        assert((*this)[0] == 1);
        if (n == -1) n = this->size();
        Formal_Power_Series ret = (derivative() * inv(n)).pre(n - 1).integral();
        ret.resize(n);
        return ret;
    }

    // exp(f(x)) mod x^n (f[0] = 0)
    Formal_Power_Series exp(int n = -1) const {
        assert((*this)[0] == 0);
        if (n == -1) n = this->size();
        vector<T> inv(2 * n + 1, 0);
        inv[1] = 1;
        int mod = T::get_mod();
        for (int i = 2; i <= 2 * n; i++) inv[i] = -inv[mod % i] * T(mod / i);

        auto inplace_integral = [inv](Formal_Power_Series &f) {
            if (empty(f)) return;
            int n = f.size();
            f.insert(begin(f), 0);
            for (int i = 1; i <= n; i++) f[i] *= inv[i];
        };

        auto inplace_derivative = [](Formal_Power_Series &f) {
            if (empty(f)) return;
            int n = f.size();
            f.erase(begin(f));
            for (int i = 0; i < n - 1; i++) f[i] *= T(i + 1);
        };

        Formal_Power_Series ret{1, this->size() > 1 ? (*this)[1] : 0}, c{1}, z1, z2{1, 1};
        for (int m = 2; m < n; m *= 2) {
            auto y = ret;
            y.resize(2 * m);
            NTT_::ntt(y);
            z1 = z2;
            Formal_Power_Series z(m);
            for (int i = 0; i < m; i++) z[i] = y[i] * z1[i];
            NTT_::intt(z);
            fill(begin(z), begin(z) + m / 2, 0);
            NTT_::ntt(z);
            for (int i = 0; i < m; i++) z[i] *= -z1[i];
            NTT_::intt(z);
            c.insert(end(c), begin(z) + m / 2, end(z));
            z2 = c, z2.resize(2 * m);
            NTT_::ntt(z2);
            Formal_Power_Series x(begin(*this), begin(*this) + min((int)this->size(), m));
            inplace_derivative(x);
            x.resize(m, 0);
            NTT_::ntt(x);
            for (int i = 0; i < m; i++) x[i] *= y[i];
            NTT_::intt(x);
            x -= ret.derivative(), x.resize(2 * m);
            for (int i = 0; i < m - 1; i++) x[m + i] = x[i], x[i] = 0;
            NTT_::ntt(x);
            for (int i = 0; i < 2 * m; i++) x[i] *= z2[i];
            NTT_::intt(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, 0);
            NTT_::ntt(x);
            for (int i = 0; i < 2 * m; i++) x[i] *= y[i];
            NTT_::intt(x);
            ret.insert(end(ret), begin(x) + m, end(x));
        }
        ret.resize(n);
        return ret;
    }

    // f(x)^k mod x^n
    Formal_Power_Series pow(long long k, int n = -1) const {
        if (n == -1) n = this->size();
        int m = this->size();
        for (int i = 0; i < m; i++) {
            if ((*this)[i] == 0) continue;
            T rev = (*this)[i].inverse();
            Formal_Power_Series C(*this * rev), D(m - i, 0);
            for (int j = i; j < m; j++) D[j - i] = C[j];
            D = (D.log() * k).exp() * ((*this)[i].pow(k));
            Formal_Power_Series E(n, 0);
            if (i > 0 && k > n / i) return E;
            long long S = i * k;
            for (int j = 0; j + S < n && j < D.size(); j++) E[j + S] = D[j];
            E.resize(n);
            return E;
        }
        return Formal_Power_Series(n, 0);
    }

    // f(x+c)
    Formal_Power_Series Taylor_shift(T c) const {
        int n = this->size();
        vector<T> ifac(n, 1);
        Formal_Power_Series f(n), g(n);
        for (int i = 0; i < n; i++) {
            f[n - 1 - i] = (*this)[i] * ifac[n - 1];
            if (i < n - 1) ifac[n - 1] *= i + 1;
        }
        ifac[n - 1] = ifac[n - 1].inverse();
        for (int i = n - 1; i > 0; i--) ifac[i - 1] = ifac[i] * i;
        T pw = 1;
        for (int i = 0; i < n; i++) {
            g[i] = pw * ifac[i];
            pw *= c;
        }
        f *= g;
        Formal_Power_Series b(n);
        for (int i = 0; i < n; i++) b[i] = f[n - 1 - i] * ifac[i];
        return b;
    }
};
using fps = Formal_Power_Series<mint>;
int main(){
  int N, M;
  cin >> N >> M;
  vector<int> L(M), R(M);
  for (int i = 0; i < M; i++){
    cin >> L[i] >> R[i];
    L[i]--;
  }
  if (N % 2 == 1){
    cout << 0 << endl;
  } else {
    N /= 2;
    vector<long long> inv(N + 2);
    inv[1] = 1;
    for (int i = 2; i <= N + 1; i++){
      inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    }
    vector<long long> cat(N + 1);
    cat[0] = 1;
    cat[1] = 1;
    for (int i = 1; i < N; i++){
      cat[i + 1] = cat[i] * 2 * (2 * i + 1) % MOD * inv[i + 2] % MOD;
    }
    vector<int> cnt(N + 1, 0);
    for (int i = 0; i < M; i++){
      if ((R[i] - L[i]) % 2 == 0){
        cnt[(R[i] - L[i]) / 2]++;
      }
    }
    vector<long long> f(N + 1, 0);
    for (int i = 1; i <= N; i++){
      long long tmp = 1;
      for (int j = 1; i * j <= N; j++){
        tmp *= cat[i];
        tmp %= MOD;
        f[i * j] += MOD - tmp * inv[j] % MOD * cnt[i] % MOD;
        f[i * j] %= MOD;
      }
    }
    fps f2(N + 1);
    for (int i = 0; i <= N; i++){
      f2[i] = f[i];
    }
    fps g = f2.exp();
    long long ans = 0;
    for (int i = 0; i <= N; i++){
      ans += g[i].x * cat[N - i] % MOD;
      ans %= MOD;
    }
    cout << ans << endl;
  }
}
0