結果

問題 No.1354 Sambo's Treasure
ユーザー ChanyuhChanyuh
提出日時 2021-01-20 17:15:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 398 ms / 2,000 ms
コード長 8,645 bytes
コンパイル時間 2,285 ms
コンパイル使用メモリ 146,888 KB
実行使用メモリ 110,628 KB
最終ジャッジ日時 2023-08-24 21:05:36
合計ジャッジ時間 14,567 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
96,832 KB
testcase_01 AC 35 ms
96,652 KB
testcase_02 AC 35 ms
96,640 KB
testcase_03 AC 34 ms
96,728 KB
testcase_04 AC 35 ms
96,696 KB
testcase_05 AC 34 ms
96,656 KB
testcase_06 AC 35 ms
96,768 KB
testcase_07 AC 34 ms
96,640 KB
testcase_08 AC 35 ms
96,768 KB
testcase_09 AC 34 ms
96,728 KB
testcase_10 AC 34 ms
96,712 KB
testcase_11 AC 34 ms
96,904 KB
testcase_12 AC 36 ms
96,768 KB
testcase_13 AC 34 ms
96,712 KB
testcase_14 AC 34 ms
96,696 KB
testcase_15 AC 34 ms
96,644 KB
testcase_16 AC 35 ms
96,828 KB
testcase_17 AC 34 ms
96,812 KB
testcase_18 AC 35 ms
96,824 KB
testcase_19 AC 35 ms
96,696 KB
testcase_20 AC 35 ms
96,652 KB
testcase_21 AC 35 ms
96,592 KB
testcase_22 AC 34 ms
96,696 KB
testcase_23 AC 393 ms
110,044 KB
testcase_24 AC 396 ms
109,964 KB
testcase_25 AC 397 ms
109,992 KB
testcase_26 AC 396 ms
109,992 KB
testcase_27 AC 396 ms
110,044 KB
testcase_28 AC 396 ms
110,104 KB
testcase_29 AC 398 ms
109,380 KB
testcase_30 AC 395 ms
110,036 KB
testcase_31 AC 398 ms
110,628 KB
testcase_32 AC 395 ms
110,260 KB
testcase_33 AC 398 ms
109,968 KB
testcase_34 AC 398 ms
110,032 KB
testcase_35 AC 397 ms
110,080 KB
testcase_36 AC 396 ms
110,068 KB
testcase_37 AC 396 ms
110,096 KB
testcase_38 AC 395 ms
109,992 KB
testcase_39 AC 398 ms
110,096 KB
testcase_40 AC 396 ms
110,040 KB
testcase_41 AC 395 ms
110,040 KB
testcase_42 AC 394 ms
110,320 KB
testcase_43 AC 37 ms
97,052 KB
testcase_44 AC 37 ms
97,024 KB
testcase_45 AC 37 ms
97,060 KB
testcase_46 AC 36 ms
97,056 KB
testcase_47 AC 37 ms
97,048 KB
testcase_48 AC 38 ms
97,108 KB
testcase_49 AC 38 ms
97,160 KB
testcase_50 AC 37 ms
97,156 KB
testcase_51 AC 37 ms
97,140 KB
testcase_52 AC 37 ms
97,056 KB
testcase_53 AC 38 ms
96,788 KB
testcase_54 AC 36 ms
97,076 KB
testcase_55 AC 37 ms
97,152 KB
testcase_56 AC 38 ms
97,064 KB
testcase_57 AC 36 ms
97,100 KB
testcase_58 AC 37 ms
97,168 KB
testcase_59 AC 37 ms
97,100 KB
testcase_60 AC 37 ms
97,124 KB
testcase_61 AC 36 ms
97,112 KB
testcase_62 AC 37 ms
97,124 KB
testcase_63 AC 397 ms
109,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
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(b<a){a=b;return 1;}return 0;}

template<int mod>
struct ModInt {
    long long x;
    static constexpr int MOD = mod;
 
    ModInt() : x(0) {}
    ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    explicit operator int() const {return x;}
 
    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; }
    ModInt operator%(const ModInt &p) const { return ModInt(0); }         
 
    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;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return ModInt(u);
    }

    ModInt power(long long n) const {
        ModInt ret(1), mul(x);
        while(n > 0) {
            if(n & 1)
            ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    ModInt power(const ModInt p) const{
        return ((ModInt)x).power(p.x);
    }

    friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
        return os << p.x;
    }
    friend istream &operator>>(istream &is, ModInt<mod> &a) {
        long long x;
        is >> x;
        a = ModInt<mod>(x);
        return (is);
    }
};

using modint = ModInt<mod>;

struct ModFac{
  public:
    vector<modint> f,i_f;
    int n;

    ModFac(int n_){
      n=n_;
      f.resize(n+1,1);
      i_f.resize(n+1,1);
      for(int i=0;i<n;i++){
        f[i+1]=f[i]*(modint)(i+1);
      }
      i_f[n]=f[n].power(mod-2);
      for(int i=n-1;i>=0;i--){
        i_f[i]=i_f[i+1]*(modint)(i+1);
      }
    }
    ModFac(modint n_){
      n=(int)n_;
      f.resize(n+1,1);
      i_f.resize(n+1,1);
      for(int i=0;i<n;i++){
        f[i+1]=f[i]*(modint)(i+1);
      }
      i_f[n]=f[n].power(mod-2);
      for(int i=n-1;i>=0;i--){
        i_f[i]=i_f[i+1]*(modint)(i+1);
      }
    }
    
    modint factorial(int x){
      //cout << f.size() << endl;
      return f[x];
    }
        
    modint inv_factorial(int x){
      return i_f[x];
    }
    
    modint comb(int m,int k){
      if (m<0 or k<0) return 0;
      if (m<k) return 0;
      return f[m]*i_f[k]*i_f[m-k];
    }
};

template <typename Mint>
struct NumberTheoreticTransformFriendlyModInt {

    vector<Mint> dw, idw;
    int max_base;
    Mint root;

    NumberTheoreticTransformFriendlyModInt() {
        const unsigned mod = Mint::MOD;
        assert(mod >= 3 && mod % 2 == 1);
        auto tmp = mod - 1;
        max_base = 0;
        while(tmp % 2 == 0)
            tmp >>= 1, max_base++;
        root = 2;
        while(root.power((mod - 1) >> 1) == 1)
            root += 1;
        assert(root.power(mod - 1) == 1);
        dw.resize(max_base);
        idw.resize(max_base);
        for(int i = 0; i < max_base; i++) {
            dw[i] = -root.power((mod - 1) >> (i + 2));
            idw[i] = Mint(1) / dw[i];
        }
    }

    void ntt(vector<Mint> &a) {
        const int n = (int)a.size();
        assert((n & (n - 1)) == 0);
        assert(__builtin_ctz(n) <= max_base);
        for(int m = n; m >>= 1;) {
            Mint w = 1;
            for(int s = 0, k = 0; s < n; s += 2 * m) {
                for(int i = s, j = s + m; i < s + m; ++i, ++j) {
                    auto x = a[i], y = a[j] * w;
                    a[i] = x + y, a[j] = x - y;
                }
                w *= dw[__builtin_ctz(++k)];
            }
        }
    }

    void intt(vector<Mint> &a, bool f = true) {
        const int n = (int)a.size();
        assert((n & (n - 1)) == 0);
        assert(__builtin_ctz(n) <= max_base);
        for(int m = 1; m < n; m *= 2) {
            Mint w = 1;
            for(int s = 0, k = 0; s < n; s += 2 * m) {
                for(int i = s, j = s + m; i < s + m; ++i, ++j) {
                    auto x = a[i], y = a[j];
                    a[i] = x + y, a[j] = (x - y) * w;
                }
                w *= idw[__builtin_ctz(++k)];
            }
        }
        if(f) {
            Mint inv_sz = Mint(1) / n;
            for(int i = 0; i < n; i++)
                a[i] *= inv_sz;
        }
    }

    vector<Mint> multiply(vector<Mint> a, vector<Mint> b) {
        int need = a.size() + b.size() - 1;
        int nbase = 1;
        while((1 << nbase) < need)
            nbase++;
        int sz = 1 << nbase;
        a.resize(sz, 0);
        b.resize(sz, 0);
        ntt(a);
        ntt(b);
        Mint inv_sz = Mint(1) / sz;
        for(int i = 0; i < sz; i++)
            a[i] *= b[i] * inv_sz;
        intt(a, false);
        a.resize(need);
        return a;
    }
};



int n,m,l,k;
vector<P> cs,ts;
modint dp1[100010][110];
ModFac MF(500010);

vector<modint> calc(int s){
  vector<P> vec={cs[s]};
  bool f=false;
  rep(i,l){
    if(cs[s].first<=ts[i].first && ts[i].first<=cs[s+1].first){
      if(cs[s].second<=ts[i].second && ts[i].second<=cs[s+1].second){
        if(cs[s+1]==ts[i]) f=true;
        if(cs[s]==ts[i] || cs[s+1]==ts[i]) continue;
        vec.push_back(ts[i]);
      }
    }
  }
  vec.push_back(cs[s+1]);
  int z=vec.size();
  vector<vector<modint>> r(z,vector<modint>(z));
  per(i,z){
    Rep(j,i+1,z){
      r[i][j]=MF.comb(vec[j].first-vec[i].first+vec[j].second-vec[i].second,vec[j].first-vec[i].first);
      Rep(k,i+1,j){
        r[i][j]-=MF.comb(vec[k].first-vec[i].first+vec[k].second-vec[i].second,vec[k].first-vec[i].first)*r[k][j];
      }
    }
  }
  vector<vector<modint>> dp2(z,vector<modint>(z+1));
  //cout << z << " " << f << endl;
  dp2[0][0]=1;
  Rep(i,1,z){
    rep(j,i){
      rep(k,i+1){
        dp2[i][k+1]+=r[j][i]*dp2[j][k];
      }
    }
    // rep(k,i+2){
    //   cout << i << " " << k << " " << dp2[i][k] << endl;
    // }
  }
  vector<modint> res(z+1,0);
  Rep(k,1,z+1){
    //cout << k << " " << dp2[z-1][k] << endl;
    if(f)res[k]=dp2[z-1][k];
    else res[k-1]=dp2[z-1][k];
  }
  return res;
}

NumberTheoreticTransformFriendlyModInt<modint> NTT;

vector<modint> mul(vector<vector<modint>> &fs,int l,int r){
  if(r-l==0)return vector<modint>(1,1);
  if(r-l==1)return fs[l];
  return NTT.multiply(mul(fs,l,(l+r)/2),mul(fs,(l+r)/2,r));
}

void solve(){
  cin >> n >> m >> l >> k;chmin(k,l);
  cs.resize(m+2);ts.resize(l);
  cs[0]=P(0,0);cs[m+1]=P(n,n);
  rep(i,m){
    cin >> cs[i+1].first >> cs[i+1].second;
  }
  rep(i,l){
    cin >> ts[i].first >> ts[i].second;
  }
  sort(ts.begin(),ts.end());
  vector<vector<modint>> fs;
  rep(i,m+1){
    fs.push_back(calc(i));
  }
  vector<modint> F=mul(fs,0,m+1);
  modint ans=0;
  rep(i,min((int)F.size(),k)+1){
    ans+=F[i];
  }
  cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0