結果

問題 No.1696 Nonnil
ユーザー KoDKoD
提出日時 2021-10-01 23:32:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 88 ms / 3,500 ms
コード長 11,248 bytes
コンパイル時間 3,404 ms
コンパイル使用メモリ 261,648 KB
実行使用メモリ 21,292 KB
最終ジャッジ日時 2023-09-26 22:43:33
合計ジャッジ時間 7,297 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 61 ms
21,132 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 54 ms
21,180 KB
testcase_09 AC 9 ms
4,476 KB
testcase_10 AC 12 ms
5,084 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 11 ms
4,992 KB
testcase_13 AC 9 ms
4,508 KB
testcase_14 AC 63 ms
19,032 KB
testcase_15 AC 21 ms
5,632 KB
testcase_16 AC 74 ms
21,292 KB
testcase_17 AC 51 ms
17,816 KB
testcase_18 AC 37 ms
10,300 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 40 ms
14,540 KB
testcase_22 AC 45 ms
13,904 KB
testcase_23 AC 42 ms
12,760 KB
testcase_24 AC 41 ms
12,168 KB
testcase_25 AC 60 ms
17,036 KB
testcase_26 AC 53 ms
17,528 KB
testcase_27 AC 65 ms
20,768 KB
testcase_28 AC 52 ms
13,160 KB
testcase_29 AC 46 ms
15,872 KB
testcase_30 AC 34 ms
11,716 KB
testcase_31 AC 53 ms
18,552 KB
testcase_32 AC 58 ms
20,012 KB
testcase_33 AC 55 ms
14,564 KB
testcase_34 AC 42 ms
15,156 KB
testcase_35 AC 50 ms
16,580 KB
testcase_36 AC 73 ms
21,060 KB
testcase_37 AC 74 ms
21,016 KB
testcase_38 AC 74 ms
21,068 KB
testcase_39 AC 86 ms
21,112 KB
testcase_40 AC 88 ms
20,944 KB
testcase_41 AC 35 ms
6,604 KB
testcase_42 AC 51 ms
19,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T> constexpr T totient(T x) {
    T ret = x;
    for (T i = 2; i * i <= x; ++i) {
        if (x % i == 0) {
            ret /= i;
            ret *= i - 1;
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) {
        ret /= x;
        ret *= x - 1;
    }
    return ret;
}

template <class T> constexpr T rem_euclid(T value, const T& mod) { return (value %= mod) >= 0 ? value : value + mod; }

template <u32 MOD, std::enable_if_t<((u32)1 <= MOD and MOD <= ((u32)1 << 31))>* = nullptr> class StaticModint {
    using Mint = StaticModint;

    static inline constexpr u32 PHI = totient(MOD);
    u32 v;

  public:
    static constexpr u32 mod() noexcept { return MOD; }

    template <class T, std::enable_if_t<std::is_signed_v<T> and std::is_integral_v<T>>* = nullptr>
    static constexpr T normalize(const T x) noexcept {
        return rem_euclid<std::common_type_t<T, i64>>(x, MOD);
    }
    template <class T, std::enable_if_t<std::is_unsigned_v<T> and std::is_integral_v<T>>* = nullptr>
    static constexpr T normalize(const T x) noexcept {
        return x % MOD;
    }

    constexpr StaticModint() noexcept : v(0) {}
    template <class T> constexpr StaticModint(const T x) noexcept : v(normalize(x)) {}
    template <class T> static constexpr Mint raw(const T x) noexcept {
        Mint ret;
        ret.v = x;
        return ret;
    }

    constexpr u32 get() const noexcept { return v; }
    constexpr Mint neg() const noexcept { return raw(v == 0 ? 0 : MOD - v); }
    constexpr Mint inv() const noexcept { return pow(PHI - 1); }
    constexpr Mint pow(u64 exp) const noexcept {
        Mint ret(1), mult(*this);
        for (; exp > 0; exp >>= 1) {
            if (exp & 1) ret *= mult;
            mult *= mult;
        }
        return ret;
    }

    constexpr Mint operator-() const noexcept { return neg(); }
    constexpr Mint operator~() const noexcept { return inv(); }

    constexpr Mint operator+(const Mint& rhs) const noexcept { return Mint(*this) += rhs; }
    constexpr Mint& operator+=(const Mint& rhs) noexcept {
        if ((v += rhs.v) >= MOD) v -= MOD;
        return *this;
    }

    constexpr Mint operator-(const Mint& rhs) const noexcept { return Mint(*this) -= rhs; }
    constexpr Mint& operator-=(const Mint& rhs) noexcept {
        if (v < rhs.v) v += MOD;
        v -= rhs.v;
        return *this;
    }

    constexpr Mint operator*(const Mint& rhs) const noexcept { return Mint(*this) *= rhs; }
    constexpr Mint& operator*=(const Mint& rhs) noexcept {
        v = (u64)v * rhs.v % MOD;
        return *this;
    }

    constexpr Mint operator/(const Mint& rhs) const noexcept { return Mint(*this) /= rhs; }
    constexpr Mint& operator/=(const Mint& rhs) noexcept { return *this *= rhs.inv(); }

    constexpr bool operator==(const Mint& rhs) const noexcept { return v == rhs.v; }
    constexpr bool operator!=(const Mint& rhs) const noexcept { return v != rhs.v; }
    friend std::ostream& operator<<(std::ostream& stream, const Mint& rhs) { return stream << rhs.v; }
};

using Modint1000000007 = StaticModint<1000000007>;
using Modint998244353 = StaticModint<998244353>;

__attribute__((target("avx2"))) constexpr u64 ceil_log2(const u64 x) {
    u64 e = 0;
    while (((u64)1 << e) < x) ++e;
    return e;
}

template <class F> class AutoReallocation {
    using R = typename decltype(std::declval<F>()((usize)0))::value_type;

    F func;
    mutable std::vector<R> data;

  public:
    explicit AutoReallocation(F&& f) : func(std::forward<F>(f)), data() {}

    void reserve(const usize size) const {
        if (data.size() < size) data = func(((usize)1 << ceil_log2(size)));
    }
    R operator[](const usize i) const {
        reserve(i + 1);
        return data[i];
    }
};

template <class F> decltype(auto) auto_realloc(F&& f) {
    using G = std::decay_t<F>;
    return AutoReallocation<G>(std::forward<G>(f));
}

template <class M> struct ModintUtil {
    static inline const auto fact = auto_realloc([](const usize n) {
        std::vector<M> ret(n);
        ret[0] = M(1);
        for (const usize i : rep(1, n)) {
            ret[i] = ret[i - 1] * M(i);
        }
        return ret;
    });
    static inline const auto inv = auto_realloc([](const usize n) {
        std::vector<M> ret(n);
        if (n == 1) return ret;
        ret[1] = M(1);
        for (const usize i : rep(2, n)) {
            ret[i] = -M(M::mod() / i) * ret[M::mod() % i];
        }
        return ret;
    });
    static inline const auto inv_fact = auto_realloc([](const usize n) {
        std::vector<M> ret(n);
        ret[0] = M(1);
        for (const usize i : rep(1, n)) {
            ret[i] = ret[i - 1] * inv[i];
        }
        return ret;
    });
    static M binom(const usize n, const usize k) {
        assert(k <= n);
        return fact[n] * inv_fact[n - k] * inv_fact[k];
    }
    static M factpow(const usize n, const usize k) {
        assert(k <= n);
        return fact[n] * inv_fact[n - k];
    }
    static M homo(const usize n, const usize k) {
        if (n == 0 and k == 0) return M(1);
        return binom(n + k - 1, k);
    }
};

class revrep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { --itr; }
        constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr revrep(const usize first, const usize last) noexcept
        : first(last - 1), last(std::min(first, last) - 1) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class M> class SegmentTree {
    using T = typename M::Type;
    usize internal_size, seg_size;
    std::vector<T> data;

    void fetch(const usize k) { data[k] = M::operation(data[2 * k], data[2 * k + 1]); }

  public:
    explicit SegmentTree(const usize size = 0, const T& value = M::identity())
        : SegmentTree(std::vector<T>(size, value)) {}
    explicit SegmentTree(const std::vector<T>& vec) : internal_size(vec.size()) {
        seg_size = 1 << ceil_log2(internal_size);
        data = std::vector<T>(2 * seg_size, M::identity());
        for (const usize i : rep(0, internal_size)) data[seg_size + i] = vec[i];
        for (const usize i : revrep(1, seg_size)) fetch(i);
    }

    usize size() const { return internal_size; }

    void assign(usize i, const T& value) {
        assert(i < internal_size);
        i += seg_size;
        data[i] = value;
        while (i > 1) {
            i >>= 1;
            fetch(i);
        }
    }

    T fold() const { return data[1]; }
    T fold(usize l, usize r) const {
        assert(l <= r and r <= internal_size);
        l += seg_size;
        r += seg_size;
        T ret_l = M::identity(), ret_r = M::identity();
        while (l < r) {
            if (l & 1) ret_l = M::operation(ret_l, data[l++]);
            if (r & 1) ret_r = M::operation(data[--r], ret_r);
            l >>= 1;
            r >>= 1;
        }
        return M::operation(ret_l, ret_r);
    }

    template <class F> usize max_right(usize l, const F& f) const {
        assert(l <= internal_size);
        assert(f(M::identity()));
        if (l == internal_size) return internal_size;
        l += seg_size;
        T sum = M::identity();
        do {
            while (!(l & 1)) l >>= 1;
            if (!f(M::operation(sum, data[l]))) {
                while (l < seg_size) {
                    l = 2 * l;
                    if (f(M::operation(sum, data[l]))) sum = M::operation(sum, data[l++]);
                }
                return l - seg_size;
            }
            sum = M::operation(sum, data[l++]);
        } while ((l & -l) != l);
        return internal_size;
    }

    template <class F> usize min_left(usize r, const F& f) const {
        assert(r <= internal_size);
        assert(f(M::identity()));
        if (r == 0) return 0;
        r += seg_size;
        T sum = M::identity();
        do {
            r -= 1;
            while (r > 1 and (r & 1)) r >>= 1;
            if (!f(M::operation(data[r], sum))) {
                while (r < seg_size) {
                    r = 2 * r + 1;
                    if (f(M::operation(data[r], sum))) sum = M::operation(data[r--], sum);
                }
                return r + 1 - seg_size;
            }
            sum = M::operation(data[r], sum);
        } while ((r & -r) != r);
        return 0;
    }
};
#include <optional>

template <class S> struct OptionalMonoid {
    using Type = std::optional<typename S::Type>;
    static constexpr Type identity() { return std::nullopt; }
    static constexpr Type operation(const Type& l, const Type& r) {
        if (!l) return r;
        if (!r) return l;
        return Type(std::in_place, S::operation(*l, *r));
    }
};

template <class T> bool setmax(T& lhs, const T& rhs) {
    if (lhs < rhs) {
        lhs = rhs;
        return true;
    }
    return false;
}

template <class T> using Vec = std::vector<T>;

using Fp = Modint998244353;
using Util = ModintUtil<Fp>;

struct Add {
    using Type = std::valarray<Fp>;
    static Type operation(const Type& l, const Type& r) { return l + r; }
};

using Monoid = OptionalMonoid<Add>;

void main_() {
    usize N, K, M;
    std::cin >> N >> K >> M;
    Vec<usize> max(K + 1);
    while (M--) {
        usize l, r;
        std::cin >> l >> r;
        setmax(max[r], l);
    }
    SegmentTree<Monoid> seg(K + 1);
    {
        std::valarray<Fp> tmp(K + 1);
        tmp[0] = 1;
        seg.assign(0, tmp);
    }
    usize left = 0;
    for (const auto r : rep(1, K + 1)) {
        seg.assign(r, seg.fold(left, r)->cshift(-1));
        setmax(left, max[r]);
    }
    Vec<Fp> pow(K + 1);
    for (const auto k : rep(0, K + 1)) {
        pow[k] = Fp(k).pow(N);
    }
    std::valarray<Fp> coeff(K + 1);
    for (const auto k : rep(0, K + 1)) {
        for (const auto i : rep(0, k + 1)) {
            if ((k - i) & 1)
                coeff[k] -= Util::binom(k, i) * pow[i];
            else
                coeff[k] += Util::binom(k, i) * pow[i];
        }
    }
    std::cout << (*seg.fold(left, K + 1) * coeff).sum() << '\n';
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    main_();
    return 0;
}
0