結果

問題 No.1354 Sambo's Treasure
ユーザー ningenMeningenMe
提出日時 2021-01-17 15:50:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 234 ms / 2,000 ms
コード長 9,185 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 222,720 KB
実行使用メモリ 98,368 KB
最終ジャッジ日時 2023-08-22 08:18:02
合計ジャッジ時間 11,627 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
14,560 KB
testcase_01 AC 22 ms
14,740 KB
testcase_02 AC 24 ms
14,556 KB
testcase_03 AC 22 ms
14,736 KB
testcase_04 AC 23 ms
14,820 KB
testcase_05 AC 23 ms
14,652 KB
testcase_06 AC 23 ms
14,728 KB
testcase_07 AC 22 ms
14,748 KB
testcase_08 AC 23 ms
14,728 KB
testcase_09 AC 23 ms
14,628 KB
testcase_10 AC 22 ms
14,732 KB
testcase_11 AC 22 ms
14,540 KB
testcase_12 AC 23 ms
14,700 KB
testcase_13 AC 24 ms
14,744 KB
testcase_14 AC 23 ms
14,520 KB
testcase_15 AC 23 ms
14,476 KB
testcase_16 AC 24 ms
14,668 KB
testcase_17 AC 24 ms
14,580 KB
testcase_18 AC 24 ms
14,744 KB
testcase_19 AC 22 ms
14,632 KB
testcase_20 AC 23 ms
14,496 KB
testcase_21 AC 23 ms
14,612 KB
testcase_22 AC 22 ms
14,808 KB
testcase_23 AC 221 ms
92,108 KB
testcase_24 AC 233 ms
98,332 KB
testcase_25 AC 225 ms
93,592 KB
testcase_26 AC 225 ms
93,748 KB
testcase_27 AC 222 ms
92,148 KB
testcase_28 AC 225 ms
92,108 KB
testcase_29 AC 234 ms
98,368 KB
testcase_30 AC 220 ms
90,444 KB
testcase_31 AC 226 ms
93,620 KB
testcase_32 AC 223 ms
92,136 KB
testcase_33 AC 222 ms
92,012 KB
testcase_34 AC 230 ms
96,796 KB
testcase_35 AC 228 ms
95,256 KB
testcase_36 AC 232 ms
96,764 KB
testcase_37 AC 232 ms
96,872 KB
testcase_38 AC 221 ms
90,624 KB
testcase_39 AC 228 ms
95,216 KB
testcase_40 AC 233 ms
96,784 KB
testcase_41 AC 227 ms
93,680 KB
testcase_42 AC 223 ms
92,136 KB
testcase_43 AC 61 ms
14,992 KB
testcase_44 AC 59 ms
14,932 KB
testcase_45 AC 63 ms
14,952 KB
testcase_46 AC 68 ms
15,020 KB
testcase_47 AC 67 ms
15,124 KB
testcase_48 AC 77 ms
14,944 KB
testcase_49 AC 62 ms
14,996 KB
testcase_50 AC 62 ms
15,120 KB
testcase_51 AC 77 ms
15,084 KB
testcase_52 AC 70 ms
15,012 KB
testcase_53 AC 74 ms
15,112 KB
testcase_54 AC 65 ms
14,928 KB
testcase_55 AC 61 ms
14,956 KB
testcase_56 AC 74 ms
14,940 KB
testcase_57 AC 71 ms
15,016 KB
testcase_58 AC 71 ms
15,092 KB
testcase_59 AC 67 ms
14,944 KB
testcase_60 AC 73 ms
14,920 KB
testcase_61 AC 63 ms
14,912 KB
testcase_62 AC 76 ms
15,064 KB
testcase_63 AC 219 ms
90,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using int128  = __int128_t;
using int64   = long long;
using int32   = int;
using uint128 = __uint128_t;
using uint64  = unsigned long long;
using uint32  = unsigned int;

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 MOD = 1'000'000'000LL + 7; //'
constexpr int64 MOD2 = 998244353;
constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

/*
 * @title ModInt
 * @docs md/util/ModInt.md
 */
template<long long mod> class ModInt {
public:
    long long x;
    constexpr ModInt():x(0) {}
    constexpr ModInt(long long 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 long long y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;}
    ModInt &operator+=(const int y) {ModInt p(y);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 long long y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;}
    ModInt &operator-=(const int y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;}
    ModInt &operator*=(const ModInt &p) {x = (x * p.x % mod);return *this;}
    ModInt &operator*=(const long long y) {ModInt p(y);x = (x * p.x % mod);return *this;}
    ModInt &operator*=(const int y) {ModInt p(y);x = (x * p.x % mod);return *this;}
    ModInt &operator^=(const ModInt &p) {x = (x ^ p.x) % mod;return *this;}
    ModInt &operator^=(const long long y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;}
    ModInt &operator^=(const int y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;}
    ModInt &operator/=(const ModInt &p) {*this *= p.inv();return *this;}
    ModInt &operator/=(const long long y) {ModInt p(y);*this *= p.inv();return *this;}
    ModInt &operator/=(const int y) {ModInt p(y);*this *= p.inv();return *this;}
    ModInt operator=(const int y) {ModInt p(y);*this = p;return *this;}
    ModInt operator=(const long long y) {ModInt p(y);*this = p;return *this;}
    ModInt operator-() const {return ModInt(-x); }
    ModInt operator++() {x++;if(x>=mod) x-=mod;return *this;}
    ModInt operator--() {x--;if(x<0) x+=mod;return *this;}
    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; }
    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 inv() 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(long long n) const {ModInt ret(1), mul(x);for(;n > 0;mul *= mul,n >>= 1) if(n & 1) ret *= mul;return ret;}
    friend ostream &operator<<(ostream &os, const ModInt &p) {return os << p.x;}
    friend istream &operator>>(istream &is, ModInt &a) {long long t;is >> t;a = ModInt<mod>(t);return (is);}
};
using modint = ModInt<MOD2>;

/*
 * @title CombinationMod - mod上の二項係数・階乗
 * @docs md/math/CombinationMod.md
 */
template<long long mod> class CombinationMod {
	vector<long long> fac,finv,inv;
public:
	CombinationMod(int N) : fac(N + 1), finv(N + 1), inv(N + 1) {
		fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1;
		for (int i = 2; i <= N; ++i) {
			fac[i] = fac[i - 1] * i % mod;
			inv[i] = mod - inv[mod%i] * (mod / i) % mod;
			finv[i] = finv[i - 1] * inv[i] % mod;
		}
	}
	inline long long binom(int n, int k) {
		return ((n < 0 || k < 0 || n < k) ? 0 : fac[n] * (finv[k] * finv[n - k] % mod) % mod);
	}
	inline long long factorial(int n) {
		return fac[n];
	}
};

//verify https://atcoder.jp/contests/abc021/tasks/abc021_d

using Pa = pair<int64,int64>;
CombinationMod<MOD2> CM(500000);

//s to g  s: vp.front, g: vp.back
modint f(vector<Pa> vp) {
    int N = vp.size();
    for(int i=N-1; 0<=i; --i) vp[i].first  -= vp[0].first;
    for(int i=N-1; 0<=i; --i) vp[i].second -= vp[0].second;
    vector<modint> dp(N,0);
    for(int i=1;i<N;++i) {
        dp[i] = CM.binom(vp[i].first+vp[i].second,vp[i].first);
        for(int j=1;j<i;++j) {
            int64 y = vp[i].first - vp[j].first;
            int64 x = vp[i].second - vp[j].second;
            if(y<0 || x<0) continue;
            dp[i] -= dp[j]*CM.binom(y+x,x);
        }
    }
    return dp.back();
}

vector<modint> calc(vector<Pa> vp) {
    int N = vp.size();
    auto coe = multivector(N,N,modint(0));
    for(int i=0;i+1<N;++i) {
        vector<Pa> tmp;
        tmp.push_back(vp[i]);
        for(int j=i+1;j<N;++j) {
            tmp.push_back(vp[j]);
            coe[i][j] = f(tmp);
        }
    }
    auto dp = multivector(N,N,modint(0));
    dp[0][0] = 1;
    for(int i=0;i<N;++i) {
        for(int j=i+1; j<N;++j) {
            for(int k=0;k<N;++k) {
                if(j!=N-1 && k+1<=N) dp[j][k+1] += dp[i][k]*coe[i][j];
                if(j==N-1)           dp[j][k]   += dp[i][k]*coe[i][j];
            }
        }
    }
    return dp[N-1];
}


/**
 * @url 
 * @est
 */ 
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
	int N,M,L,K; cin >> N >> M >> L >> K;
    vector<int64> cy(M+2),cx(M+2),dy(L),dx(L);
    cy[0]=cx[0]=0, cy[M+1]=cx[M+1]=N;
    for(int i=1;i<=M;++i) cin >> cy[i] >> cx[i];
    for(int i=0;i< L;++i) cin >> dy[i] >> dx[i];

    auto dp = multivector(M+2,L+1,modint(0));
    dp[0][0] = 1;
    for(int i=0; i+1 < M+2; ++i) {
        Pa s = {cy[i]  ,cx[i]};
        Pa g = {cy[i+1],cx[i+1]};
        vector<Pa> vp;
        vp.push_back(s);
        vp.push_back(g);
        int flg = 0;
        for(int j=0;j<L;++j) {
            if(s.first == dy[j] && s.second == dx[j]) continue;
            if(g.first == dy[j] && g.second == dx[j]) {
                flg = 1;
                continue;
            }
            if(s.first <= dy[j] && s.second <= dx[j] && dy[j] <= g.first && dx[j] <= g.second) {
                vp.emplace_back(dy[j],dx[j]);                
            }
        }
        sort(ALL(vp),[&](Pa l,Pa r){return l.first+l.second < r.first+r.second;});
        auto tp = calc(vp);
        int X = tp.size();
        for(int j=0; j<=L; ++j) {
            for(int k=0; k<X; ++k) {
                if(j + k + flg <= L) {
                    dp[i+1][j+k+flg] += dp[i][j]*tp[k];
                }
            }
        }
    }
    modint ans = 0;
    for(int i=0;i<=L;++i) if(i <= K) ans += dp[M+1][i];
    cout << ans << endl;
    return 0;
}
0