結果

問題 No.2605 Pickup Parentheses
ユーザー coindarwcoindarw
提出日時 2024-01-15 01:30:55
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 819 ms / 2,000 ms
コード長 9,645 bytes
コンパイル時間 6,084 ms
コンパイル使用メモリ 325,928 KB
実行使用メモリ 18,320 KB
最終ジャッジ日時 2024-09-30 06:38:56
合計ジャッジ時間 20,382 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,824 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 4 ms
6,820 KB
testcase_04 AC 4 ms
6,816 KB
testcase_05 AC 6 ms
6,816 KB
testcase_06 AC 7 ms
6,816 KB
testcase_07 AC 6 ms
6,820 KB
testcase_08 AC 8 ms
6,816 KB
testcase_09 AC 3 ms
6,820 KB
testcase_10 AC 6 ms
6,816 KB
testcase_11 AC 4 ms
6,820 KB
testcase_12 AC 3 ms
6,820 KB
testcase_13 AC 3 ms
6,820 KB
testcase_14 AC 5 ms
6,816 KB
testcase_15 AC 6 ms
6,820 KB
testcase_16 AC 3 ms
6,820 KB
testcase_17 AC 5 ms
6,816 KB
testcase_18 AC 328 ms
9,008 KB
testcase_19 AC 167 ms
6,816 KB
testcase_20 AC 32 ms
6,816 KB
testcase_21 AC 21 ms
6,820 KB
testcase_22 AC 6 ms
6,816 KB
testcase_23 AC 330 ms
9,004 KB
testcase_24 AC 28 ms
6,820 KB
testcase_25 AC 433 ms
10,060 KB
testcase_26 AC 479 ms
10,464 KB
testcase_27 AC 303 ms
9,132 KB
testcase_28 AC 495 ms
11,196 KB
testcase_29 AC 89 ms
6,820 KB
testcase_30 AC 247 ms
7,244 KB
testcase_31 AC 202 ms
6,820 KB
testcase_32 AC 87 ms
6,816 KB
testcase_33 AC 496 ms
11,336 KB
testcase_34 AC 451 ms
11,108 KB
testcase_35 AC 278 ms
8,896 KB
testcase_36 AC 368 ms
10,152 KB
testcase_37 AC 380 ms
9,652 KB
testcase_38 AC 70 ms
6,820 KB
testcase_39 AC 25 ms
6,816 KB
testcase_40 AC 181 ms
8,692 KB
testcase_41 AC 161 ms
8,952 KB
testcase_42 AC 253 ms
9,196 KB
testcase_43 AC 51 ms
6,820 KB
testcase_44 AC 41 ms
6,816 KB
testcase_45 AC 3 ms
6,816 KB
testcase_46 AC 13 ms
6,816 KB
testcase_47 AC 2 ms
6,820 KB
testcase_48 AC 118 ms
8,084 KB
testcase_49 AC 282 ms
9,576 KB
testcase_50 AC 114 ms
6,820 KB
testcase_51 AC 220 ms
8,780 KB
testcase_52 AC 149 ms
6,816 KB
testcase_53 AC 211 ms
8,696 KB
testcase_54 AC 106 ms
6,820 KB
testcase_55 AC 202 ms
8,808 KB
testcase_56 AC 38 ms
6,816 KB
testcase_57 AC 243 ms
9,108 KB
testcase_58 AC 819 ms
18,320 KB
testcase_59 AC 344 ms
9,260 KB
testcase_60 AC 422 ms
9,844 KB
testcase_61 AC 407 ms
9,912 KB
testcase_62 AC 476 ms
10,140 KB
testcase_63 AC 441 ms
10,424 KB
testcase_64 AC 546 ms
11,492 KB
testcase_65 AC 386 ms
9,320 KB
testcase_66 AC 781 ms
15,872 KB
testcase_67 AC 443 ms
10,056 KB
testcase_68 AC 8 ms
6,816 KB
testcase_69 AC 2 ms
6,816 KB
testcase_70 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef ONLINE_JUDGE
#include <bits/stdc++.h>

#include <atcoder/all>
#else
#include <mylibs/all.h>
#endif

using ll = long long;
using lll = __int128_t;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; --i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repc2(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define length(v) ((int)(v).size())
constexpr int inf = 2'000'000'000;
constexpr ll linf = 4'000'000'000'000'000'000, M7 = 1'000'000'007, M9 = 998'244'353;
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using namespace atcoder;

// clang-format off
#define Vec(type, ...) __make_vec<type>(__VA_ARGS__)
template <class T> vector<T> __make_vec(size_t a) {return vector<T>(a);}template <class T, class... Ts>
auto __make_vec(size_t a, Ts... ts) {return vector<decltype(__make_vec<T>(ts...))>(a, __make_vec<T>(ts...));}
#define VecI(init, type, ...) __make_vecI<type, init>(__VA_ARGS__)
template <class T, T init>vector<T> __make_vecI(size_t a) {return vector<T>(a, init);}
template <class T, T init, class... Ts>
auto __make_vecI(size_t a, Ts... ts) {return vector<decltype(__make_vecI<T, init>(ts...))>(a, __make_vecI<T, init>(ts...));}

template <typename T, typename U>inline ostream& operator<<(ostream& os, const pair<T, U>& p) noexcept {return os << p.first << " " << p.second;}
inline ostream& operator<<(ostream& os, const modint& m) noexcept { return os << m.val(); }
template <int M>inline ostream& operator<<(ostream& os, const static_modint<M>& m) noexcept { return os << m.val(); }

template <typename T> struct is_static_modint : std::false_type {}; template <int MOD> struct is_static_modint<static_modint<MOD>> : std::true_type {};
template <template <typename...> typename C, typename Number>concept MyContainer = std::is_same_v<C<Number>, std::vector<Number>> || std::is_same_v<C<Number>, std::deque<Number>> || std::is_same_v<C<Number>, std::set<Number>> || std::is_same_v<C<Number>, std::unordered_set<Number>> || std::is_same_v<C<Number>, std::unordered_multiset<Number>> || std::is_same_v<C<Number>, std::multiset<Number>>;
template <typename Number>concept MyNumber = std::is_same_v<Number, int> || std::is_same_v<Number, ll> || std::is_same_v<Number, char> || std::is_same_v<Number, modint> || is_static_modint<Number>::value;
template <template <typename...> typename C, typename Number>concept MyContainerNumber = MyContainer<C, Number> && MyNumber<Number>;
template <template <typename...> typename OutCon, template <typename...> typename InCon, typename Number>concept MyNestedContainerNumber = MyContainer<OutCon, InCon<Number>> && MyContainerNumber<InCon, Number>;
template <template <typename...> typename C, typename Number>requires MyContainerNumber<C, Number>std::ostream& operator<<(std::ostream& os, const C<Number>& t) {auto itr = t.begin();auto end = t.end();if (itr != end) {os << *itr++;for (; itr != end; ++itr) os << ' ' << *itr;}return os;}
template <template <typename...> typename OutCon, template <typename...> typename InCon, typename Number>requires MyNestedContainerNumber<OutCon, InCon, Number>std::ostream& operator<<(std::ostream& os, const OutCon<InCon<Number>>& t) {auto itr = t.begin();auto end = t.end();if (itr != end) {os << *itr++;for (; itr != end; ++itr) os << '\n' << *itr;}return os;}
template <typename T, typename U>istream& operator>>(istream& is, pair<T, U>& p) {return is >> p.first >> p.second;}
template <typename T>istream& operator>>(istream& is, vector<T>& v) {for (auto& e : v) is >> e;return is;}
void inp() {}
template <typename T, typename... Args>void inp(T& a, Args&... args) {cin >> a, inp(args...);}
template <typename T>void inp1(vector<T>& v, int offset = 1, int len = -1) {if (len == -1) len = int(v.size()) - offset;assert(offset >= 0 && len >= 0);for (int i = offset; i < offset + len; ++i) cin >> v[i];}
template <typename T>void oup(const T& a) {cout << a << "\n";}
template <typename T, typename... Args>void oup(const T& a, const Args&... args) {cout << a << " ", oup(args...);}

inline string YESNO(bool cond) { return cond ? "YES" : "NO"; }inline string yesno(bool cond) { return cond ? "yes" : "no"; }inline string YesNo(bool cond) { return cond ? "Yes" : "No"; }
inline auto add1(auto vec, ll offset = 1) {for (auto& e : vec) e += offset;return vec;}
#ifdef ONLINE_JUDGE
#define debug(...)
#else
#define debug(...) cerr << "<" << #__VA_ARGS__ << ">: ", debug_out(__VA_ARGS__)
template <typename T>void debug_out(T t) {cerr << t << "\n";}
template <typename T, typename... Args>void debug_out(T t, Args... args) {cerr << t << ", ";debug_out(args...);}
#endif

#ifdef ONLINE_JUDGE
#define todo(...) static_assert(false)
#else
#define todo(...)
#endif
// clang-format on

class Convolution9 {
   private:
    constexpr static ll M9 = 998244353;
    vector<ll> ws;
    vector<ll> w_inv;
    ll mod_pow(ll x, ll n, ll M) {
        ll res = 1;
        while (n > 0) {
            if (n & 1) res *= x, res %= M;
            x *= x, x %= M;
            n >>= 1;
        }
        return res;
    }
    void _ntt(vector<int>& v, int l, int r, int inv, int d) {
        const int len = r - l;
        const int half = len / 2;
        ll w = inv == 1 ? ws[d] : w_inv[d];
        ll alpha = 1;
        if (len > 1) {
            for (int i = 0; i < half; ++i) {
                const ll a = v[l + i], b = v[l + i + half];
                v[l + i] = (a + b) % M9;
                v[l + i + half] = ((a - b + M9) * alpha) % M9;
                alpha = (alpha * w) % M9;
            }
            _ntt(v, l, l + half, inv, d - 1);
            _ntt(v, l + half, r, inv, d - 1);
        }
    }
    void bit_reverse(vector<int>& v) {
        const int n = v.size();
        int i = 0;
        for (int j = 1; j < n - 1; ++j) {
            for (int k = n >> 1; k > (i ^= k); k >>= 1)
                ;
            if (j < i) swap(v[i], v[j]);
        }
    }
    void ntt(vector<int>& v) {
        _ntt(v, 0, v.size(), 1, __builtin_ffs(v.size()) - 1);
        bit_reverse(v);
    }
    void intt(vector<int>& v) {
        const int n = v.size();
        _ntt(v, 0, n, -1, __builtin_ffs(n) - 1);
        bit_reverse(v);
        ll ninv = mod_pow(n, M9 - 2, M9);
        for (int i = 0; i < n; ++i) {
            v[i] = (v[i] * ninv) % M9;
        }
    }

   public:
    Convolution9() {
        ws.resize(23);
        w_inv.resize(23);
        rep(i, 23) {
            ws[i] = mod_pow(3, (M9 - 1) / (1 << i), M9);
            w_inv[i] = mod_pow(ws[i], M9 - 2, M9);
        }
    }
    vector<int> convolution(vector<int> a, vector<int> b) {
        const int n = a.size(), m = b.size();
        int N = 1;
        while (N < n + m - 1) N <<= 1;
        a.resize(N);
        b.resize(N);
        ntt(a);
        ntt(b);
        vector<int> c(N);
        for (int i = 0; i < N; ++i) {
            c[i] = ll(a[i]) * b[i] % M9;
        }
        intt(c);
        c.resize(n + m - 1);
        return c;
    }
};

constexpr ll MOD = M9;
using mint = static_modint<MOD>;
struct modInv {
    int n;
    vector<mint> d;
    modInv() : n(2), d({0, 1}) {}
    mint operator()(int i) {
        while (n <= i) d.emplace_back(-d[MOD % n] * (MOD / n)), ++n;
        return d[i];
    }
    mint operator[](int i) const { return d[i]; }
} invs;
struct Factorial {
    int n;
    vector<mint> d;
    Factorial() : n(2), d({1, 1}) {}
    mint operator()(int i) {
        while (n <= i) d.emplace_back(d.back() * n), ++n;
        return d[i];
    }
    mint operator[](int i) const { return d[i]; }
} factorial;
struct FactorialInv {
    int n;
    vector<mint> d;
    FactorialInv() : n(2), d({1, 1}) {}
    mint operator()(int i) {
        while (n <= i) d.emplace_back(d.back() * invs(n)), ++n;
        return d[i];
    }
    mint operator[](int i) const { return d[i]; }
} factorialInv;
mint P(int n, int r) {
    if (n < r || n < 0 || r < 0) return 0;
    return factorial(n) * factorialInv(n - r);
}
mint C(int n, int r) {
    if (n < r || n < 0 || r < 0) return 0;
    return factorial(n) * factorialInv(r) * factorialInv(n - r);
}
mint H(int n, int r) {
    const int _n = n + r - 1;
    if (_n < r || _n < 0 || r < 0) return 0;
    return factorial(_n) * factorialInv(r) * factorialInv(_n - r);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto catalan = [&](int x) -> mint {
        if (x % 2 == 1) return 0;
        x /= 2;
        return C(2 * x, x) * invs(x + 1);
    };

    int n, m;
    inp(n, m);
    if (m == 0) {
        oup(catalan(n));
        return 0;
    }

    vector<vector<int>> vf;

    rep(i, m) {
        int l, r;
        inp(l, r);
        int len = r - l + 1;
        vf.push_back(vector<int>(len + 1));
        vf.back()[0] = 1;
        vf.back().back() = (-catalan(len)).val();
    }

    auto cmp = [&](const auto& f, const auto& g) -> bool { return f.size() > g.size(); };
    priority_queue<vector<int>, vector<vector<int>>, decltype(cmp)> pq(cmp);

    for (auto& f : vf) {
        pq.push(f);
    }

    auto cnv = Convolution9();
    while (pq.size() > 1) {
        auto f = pq.top();
        pq.pop();
        auto g = pq.top();
        pq.pop();
        auto h = cnv.convolution(f, g);
        pq.push(h);
    }

    auto f = pq.top();
    mint ans = 0;
    rep(i, f.size()) {
        if (i % 2 == 0) {
            ans += f[i] * catalan(n - i);
        } else {
            ans -= f[i] * catalan(n - i);
        }
    }
    oup(ans);
    return 0;
}
0