結果

問題 No.1696 Nonnil
ユーザー shotoyooshotoyoo
提出日時 2021-10-02 00:09:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,798 ms / 3,500 ms
コード長 11,864 bytes
コンパイル時間 2,818 ms
コンパイル使用メモリ 220,216 KB
実行使用メモリ 54,944 KB
最終ジャッジ日時 2023-09-26 23:59:35
合計ジャッジ時間 41,504 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,628 KB
testcase_01 AC 5 ms
4,540 KB
testcase_02 AC 4 ms
4,548 KB
testcase_03 AC 1,166 ms
52,552 KB
testcase_04 AC 5 ms
4,716 KB
testcase_05 AC 5 ms
4,576 KB
testcase_06 AC 5 ms
4,552 KB
testcase_07 AC 5 ms
4,540 KB
testcase_08 AC 1,135 ms
52,480 KB
testcase_09 AC 64 ms
7,564 KB
testcase_10 AC 96 ms
8,284 KB
testcase_11 AC 53 ms
7,228 KB
testcase_12 AC 89 ms
8,272 KB
testcase_13 AC 53 ms
7,272 KB
testcase_14 AC 1,462 ms
51,208 KB
testcase_15 AC 154 ms
13,876 KB
testcase_16 AC 1,628 ms
54,580 KB
testcase_17 AC 1,262 ms
48,448 KB
testcase_18 AC 557 ms
20,744 KB
testcase_19 AC 5 ms
4,524 KB
testcase_20 AC 5 ms
4,544 KB
testcase_21 AC 1,094 ms
42,924 KB
testcase_22 AC 1,038 ms
42,640 KB
testcase_23 AC 924 ms
40,660 KB
testcase_24 AC 866 ms
39,352 KB
testcase_25 AC 1,396 ms
48,464 KB
testcase_26 AC 1,411 ms
48,220 KB
testcase_27 AC 1,722 ms
53,636 KB
testcase_28 AC 959 ms
41,872 KB
testcase_29 AC 1,218 ms
45,740 KB
testcase_30 AC 725 ms
21,636 KB
testcase_31 AC 1,489 ms
49,752 KB
testcase_32 AC 1,633 ms
51,896 KB
testcase_33 AC 1,135 ms
44,704 KB
testcase_34 AC 1,151 ms
44,068 KB
testcase_35 AC 1,325 ms
46,904 KB
testcase_36 AC 1,770 ms
54,808 KB
testcase_37 AC 1,798 ms
54,796 KB
testcase_38 AC 1,790 ms
54,944 KB
testcase_39 AC 1,550 ms
54,512 KB
testcase_40 AC 1,739 ms
54,856 KB
testcase_41 AC 293 ms
16,816 KB
testcase_42 AC 1,047 ms
49,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

 bool TEST = false;

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

#define rep(i,n) for(int (i)=0;(i)<(ll)(n);i++)
#define rrep(i,n) for(int (i)=(ll)(n)-1;(i)>=0;i--)
#define range(i,start,end,step) for(int (i)=start;(i)<(ll)(end);(i)+=(step))
#define rrange(i,start,end,step) for(int (i)=start;(i)>(ll)(end);(i)+=(step))

#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define all(a)  (a).begin(),(a).end()
#define allr(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
 
template<typename T> using V = vector<T>;
template<typename T> using VV = V<V<T>>;
template<typename T, typename T2> using P = pair<T, T2>;
template<typename T, typename T2> using M = map<T, T2>;
template<typename T> using S = set<T>;
template<typename T, typename T2> using UM = unordered_map<T, T2>;
template<typename T> using PQ = priority_queue<T, V<T>, greater<T>>;
template<typename T> using rPQ = priority_queue<T, V<T>, less<T>>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class SS, class T> ostream& operator << (ostream& os, const pair<SS, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
 
template <class T> void UNIQUE(vector<T> &x) {sort(all(x));x.erase(unique(all(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v){for(ll i=0;i<v.size();i++)
{cerr<<v[i][0];for(ll j=1;j<v[i].size();j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v){if(v.size()!=0)cerr<<v[0];
for(ll i=1;i<v.size();i++)cerr spa v[i];
cerr<<"\n";};
template<typename T> P<T,T> divmod(T a, T b) {return make_pair(a/b, a%b);}

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);

int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct LSG {
  public:
    LSG() : LSG(0) {}
    explicit LSG(int n) : LSG(std::vector<S>(n, e())) {}
    explicit LSG(const std::vector<S>& v) : _n(int(v.size())) {
        log = ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            _update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) _update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S query(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S query_all() { return d[1]; }

    void check() {
      rep(i,_n) cout << get(i) << " ";
      cout << endl;
    }

    void update(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) _update(p >> i);
    }
    void update(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) _update(l >> i);
            if (((r >> i) << i) != r) _update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void _update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

// class S,
// S (*op)(S, S),
// S (*e)(),
// class F,
// S (*mapping)(F, S),
// F (*composition)(F, F),
// F (*id)()

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 m17 = ModInt<1'000'000'007>;
using m98 = ModInt<998'244'353>;
using MOD = m98;
// ll mod = 1'000'000'007;
ll mod = 998'244'353;


MOD g1[100001];
MOD g2[100001];
MOD inverse[100001];
void prepare() {
  g1[0] = g1[1] = g2[0] = g2[1] = 1;
  inverse[0] = 0;
  inverse[1] = 1;
  range(i,2,100001,1) {
    g1[i] = g1[i-1]*i;
    inverse[i] = -inverse[mod%i]*(mod/i);
    g2[i] = g2[i-1]*inverse[i];
  }
}
template<typename T>
MOD cmb(T n, T r) {
  assert(g1[0]==1);
  if (r<0 || r>n) return 0;
  r = min(r, n-r);
  return g1[n]*g2[r]*g2[n-r];
}
template<typename T>
MOD perm(T n, T r) {
  if (r<0 || r>n) return 0;
  return g1[n]*g2[n-r];
}


using T = MOD;
using F = P<MOD,bool>;
T op (T l, T r) {
    return l+r;
}
T e () {
    return 0;
}
T mapping(F f, T v) {
    if (f.second) return f.first;
    return v;
}
// f1 : new
F composition(F f1, F f2) {
    if (!f1.second) return f2;
    return f1;
}
F id() {
    return {0,0};
}

    // LSG<T,op,e,F,mapping,composition,id> sg(V<T>(n,0));


void Main(){
    prepare();
    int n,k,m;
    cin >> n >> k >> m;
    V<int> lr;
    int l,r;
    rep(i,m) {
        cin >> l >> r;
        lr.push_back(5000-l + r*10000);
    }
    sort(all(lr));
    V<P<int,int>> nlr;
    rep(i,m) {
        auto [rr,ll] = divmod(lr[i], 10000);
        nlr.emplace_back(5000-ll,rr);
    }
    int ind = 0;
    V<LSG<T,op,e,F,mapping,composition,id>> cum(k+1);
    rep(i,k+1) {
        V<MOD> a(k+1,0);
        if (i==0) a[0] = 1;
        cum[i] = LSG<T,op,e,F,mapping,composition,id>(a);
    }
    range(r, 1, k+1, 1) {
        while (ind<m and nlr[ind].second<r) ind++;
        range(i, 1, k+1, 1) {
            auto val = cum[i-1].query(0, r);
            cum[i].update(r,r+1,{val,1});
        }
        if (ind<m and nlr[ind].second==r) {
            auto l = nlr[ind].first;
            rep(i,k+1) {
                cum[i].update(0,l,{0,1});
            }
        }
    }
    MOD ans = 0;
    range(i, 1, k+1, 1) {
        MOD val = 0;
        int s = 1;
        rep(j,i+1) {
            val += cmb(i,j) * MOD(i-j).pow(n) * s;
            s *= -1;
        }
        ans += cum[i].query(0,k+1) * val;
    }
    cout << ans << endl;
}

int main(void){
    std::ifstream in("tmp_in");
    if (TEST) {
        std::cin.rdbuf(in.rdbuf());
        std::cout << std::fixed << std::setprecision(15);
    } else {
        std::cin.tie(nullptr);
        std::ios_base::sync_with_stdio(false);
        std::cout << std::fixed << std::setprecision(15);
    }
    Main();
}
0