結果

問題 No.1354 Sambo's Treasure
ユーザー ningenMeningenMe
提出日時 2021-01-17 15:50:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 9,185 bytes
コンパイル時間 3,743 ms
コンパイル使用メモリ 224,796 KB
実行使用メモリ 98,544 KB
最終ジャッジ日時 2024-05-09 14:07:17
合計ジャッジ時間 10,609 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
15,088 KB
testcase_01 AC 21 ms
14,848 KB
testcase_02 AC 20 ms
14,848 KB
testcase_03 AC 20 ms
14,904 KB
testcase_04 AC 22 ms
14,848 KB
testcase_05 AC 23 ms
14,848 KB
testcase_06 AC 22 ms
14,976 KB
testcase_07 AC 21 ms
14,916 KB
testcase_08 AC 20 ms
14,780 KB
testcase_09 AC 21 ms
15,104 KB
testcase_10 AC 21 ms
14,976 KB
testcase_11 AC 22 ms
14,976 KB
testcase_12 AC 21 ms
14,976 KB
testcase_13 AC 22 ms
14,848 KB
testcase_14 AC 20 ms
14,944 KB
testcase_15 AC 21 ms
14,976 KB
testcase_16 AC 22 ms
14,916 KB
testcase_17 AC 21 ms
14,976 KB
testcase_18 AC 23 ms
14,976 KB
testcase_19 AC 21 ms
14,976 KB
testcase_20 AC 20 ms
14,976 KB
testcase_21 AC 20 ms
14,936 KB
testcase_22 AC 21 ms
14,948 KB
testcase_23 AC 215 ms
92,288 KB
testcase_24 AC 225 ms
98,544 KB
testcase_25 AC 214 ms
93,696 KB
testcase_26 AC 218 ms
93,696 KB
testcase_27 AC 215 ms
92,288 KB
testcase_28 AC 215 ms
92,288 KB
testcase_29 AC 221 ms
98,432 KB
testcase_30 AC 215 ms
90,740 KB
testcase_31 AC 224 ms
93,824 KB
testcase_32 AC 214 ms
92,416 KB
testcase_33 AC 211 ms
92,260 KB
testcase_34 AC 216 ms
96,948 KB
testcase_35 AC 220 ms
95,372 KB
testcase_36 AC 218 ms
97,024 KB
testcase_37 AC 216 ms
97,024 KB
testcase_38 AC 209 ms
90,752 KB
testcase_39 AC 221 ms
95,488 KB
testcase_40 AC 227 ms
96,992 KB
testcase_41 AC 216 ms
93,900 KB
testcase_42 AC 219 ms
92,160 KB
testcase_43 AC 57 ms
15,104 KB
testcase_44 AC 55 ms
15,104 KB
testcase_45 AC 62 ms
15,104 KB
testcase_46 AC 65 ms
15,104 KB
testcase_47 AC 67 ms
15,104 KB
testcase_48 AC 75 ms
15,116 KB
testcase_49 AC 58 ms
15,104 KB
testcase_50 AC 58 ms
15,104 KB
testcase_51 AC 72 ms
15,224 KB
testcase_52 AC 65 ms
15,104 KB
testcase_53 AC 67 ms
15,100 KB
testcase_54 AC 63 ms
15,232 KB
testcase_55 AC 58 ms
15,032 KB
testcase_56 AC 67 ms
15,104 KB
testcase_57 AC 65 ms
15,104 KB
testcase_58 AC 66 ms
15,104 KB
testcase_59 AC 63 ms
15,232 KB
testcase_60 AC 69 ms
15,224 KB
testcase_61 AC 57 ms
15,096 KB
testcase_62 AC 70 ms
15,132 KB
testcase_63 AC 206 ms
90,752 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