結果

問題 No.2459 Stampaholic (Hard)
ユーザー tokusakuraitokusakurai
提出日時 2023-09-01 23:49:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 24,847 bytes
コンパイル時間 2,476 ms
コンパイル使用メモリ 220,932 KB
実行使用メモリ 47,504 KB
最終ジャッジ日時 2024-06-11 06:06:43
合計ジャッジ時間 24,866 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
47,504 KB
testcase_01 WA -
testcase_02 AC 1,577 ms
16,500 KB
testcase_03 AC 17 ms
11,008 KB
testcase_04 AC 16 ms
11,008 KB
testcase_05 AC 16 ms
11,008 KB
testcase_06 AC 15 ms
11,008 KB
testcase_07 AC 15 ms
11,136 KB
testcase_08 WA -
testcase_09 AC 1,621 ms
17,040 KB
testcase_10 WA -
testcase_11 AC 3,482 ms
23,560 KB
testcase_12 TLE -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 TLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define per(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define per2(i, l, r) for (int i = (r)-1; i >= (l); i--)
#define each(e, v) for (auto &e : v)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
using minheap = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
using maxheap = priority_queue<T>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

template <typename T>
int flg(T x, int i) {
    return (x >> i) & 1;
}

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int botbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int botbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

template <typename T>
void printn(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << '\n';
}

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <typename T>
vector<int> id_sort(const vector<T> &v, bool greater = false) {
    int n = v.size();
    vector<int> ret(n);
    iota(begin(ret), end(ret), 0);
    sort(begin(ret), end(ret), [&](int i, int j) { return greater ? v[i] > v[j] : v[i] < v[j]; });
    return ret;
}

template <typename T>
void reorder(vector<T> &a, const vector<int> &ord) {
    int n = a.size();
    vector<T> b(n);
    for (int i = 0; i < n; i++) b[i] = a[ord[i]];
    swap(a, b);
}

template <typename T>
T floor(T x, T y) {
    assert(y != 0);
    if (y < 0) x = -x, y = -y;
    return (x >= 0 ? x / y : (x - y + 1) / y);
}

template <typename T>
T ceil(T x, T y) {
    assert(y != 0);
    if (y < 0) x = -x, y = -y;
    return (x >= 0 ? (x + y - 1) / y : x / y);
}

template <typename S, typename T>
pair<S, T> operator+(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first + q.first, p.second + q.second);
}

template <typename S, typename T>
pair<S, T> operator-(const pair<S, T> &p, const pair<S, T> &q) {
    return make_pair(p.first - q.first, p.second - q.second);
}

template <typename S, typename T>
istream &operator>>(istream &is, pair<S, T> &p) {
    S a;
    T b;
    is >> a >> b;
    p = make_pair(a, b);
    return is;
}

template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
    return os << p.first << ' ' << p.second;
}

struct io_setup {
    io_setup() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(15);
    }
} io_setup;

constexpr int inf = (1 << 30) - 1;
constexpr ll INF = (1LL << 60) - 1;
// constexpr int MOD = 1000000007;
constexpr int MOD = 998244353;

template <int mod>
struct Mod_Int {
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    static int get_mod() { return mod; }

    Mod_Int &operator+=(const Mod_Int &p) {
        if ((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator-=(const Mod_Int &p) {
        if ((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator*=(const Mod_Int &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator/=(const Mod_Int &p) {
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator++() { return *this += Mod_Int(1); }

    Mod_Int operator++(int) {
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator--() { return *this -= Mod_Int(1); }

    Mod_Int operator--(int) {
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator-() const { return Mod_Int(-x); }

    Mod_Int operator+(const Mod_Int &p) const { return Mod_Int(*this) += p; }

    Mod_Int operator-(const Mod_Int &p) const { return Mod_Int(*this) -= p; }

    Mod_Int operator*(const Mod_Int &p) const { return Mod_Int(*this) *= p; }

    Mod_Int operator/(const Mod_Int &p) const { return Mod_Int(*this) /= p; }

    bool operator==(const Mod_Int &p) const { return x == p.x; }

    bool operator!=(const Mod_Int &p) const { return x != p.x; }

    Mod_Int inverse() const {
        assert(*this != Mod_Int(0));
        return pow(mod - 2);
    }

    Mod_Int pow(long long k) const {
        Mod_Int now = *this, ret = 1;
        for (; k > 0; k >>= 1, now *= now) {
            if (k & 1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Mod_Int &p) { return os << p.x; }

    friend istream &operator>>(istream &is, Mod_Int &p) {
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

using mint = Mod_Int<MOD>;

template <typename T>
struct Combination {
    static vector<T> _fac, _ifac;

    Combination() {}

    static void init(int n) {
        _fac.resize(n + 1), _ifac.resize(n + 1);
        _fac[0] = 1;
        for (int i = 1; i <= n; i++) _fac[i] = _fac[i - 1] * i;
        _ifac[n] = _fac[n].inverse();
        for (int i = n; i >= 1; i--) _ifac[i - 1] = _ifac[i] * i;
    }

    static T fac(int k) { return _fac[k]; }

    static T ifac(int k) { return _ifac[k]; }

    static T inv(int k) { return fac(k - 1) * ifac(k); }

    static T P(int n, int k) {
        if (k < 0 || n < k) return 0;
        return fac(n) * ifac(n - k);
    }

    static T C(int n, int k) {
        if (k < 0 || n < k) return 0;
        return fac(n) * ifac(n - k) * ifac(k);
    }

    // n 個の区別できる箱に、k 個の区別できない玉を入れる場合の数
    static T H(int n, int k) {
        if (n < 0 || k < 0) return 0;
        return k == 0 ? 1 : C(n + k - 1, k);
    }

    // n 個の区別できる玉を、k 個の区別しない箱に、各箱に 1 個以上玉が入るように入れる場合の数
    static T second_stirling_number(int n, int k) {
        T ret = 0;
        for (int i = 0; i <= k; i++) {
            T tmp = C(k, i) * T(i).pow(n);
            ret += ((k - i) & 1) ? -tmp : tmp;
        }
        return ret * ifac(k);
    }

    // n 個の区別できる玉を、k 個の区別しない箱に入れる場合の数
    static T bell_number(int n, int k) {
        if (n == 0) return 1;
        k = min(k, n);
        vector<T> pref(k + 1);
        pref[0] = 1;
        for (int i = 1; i <= k; i++) {
            if (i & 1) {
                pref[i] = pref[i - 1] - ifac(i);
            } else {
                pref[i] = pref[i - 1] + ifac(i);
            }
        }
        T ret = 0;
        for (int i = 1; i <= k; i++) ret += T(i).pow(n) * ifac(i) * pref[k - i];
        return ret;
    }
};

template <typename T>
vector<T> Combination<T>::_fac = vector<T>();

template <typename T>
vector<T> Combination<T>::_ifac = vector<T>();

using comb = Combination<mint>;

// n 次多項式 f の f(0),...,f(n) を与えて f(c) を計算
// comb を n まで初期化する
template <typename T>
T single_point_interpolation(vector<T> ys, T c) {
    using comb_ = Combination<T>;
    int n = ys.size();
    T coef = 1;
    for (int i = 0; i < n; i++) {
        ys[i] *= coef * comb_::ifac(i);
        coef *= c - i;
    }
    coef = 1;
    T ret = 0;
    for (int i = n - 1; i >= 0; i--) {
        ret += ys[i] * coef * comb_::ifac(n - 1 - i) * ((n - 1 - i) & 1 ? -1 : 1);
        coef *= c - i;
    }
    return ret;
}

template <typename T>
struct Number_Theoretic_Transform {
    static int max_base;
    static T root;
    static vector<T> r, ir;

    Number_Theoretic_Transform() {}

    static void init() {
        if (!r.empty()) return;
        int mod = T::get_mod();
        int tmp = mod - 1;
        root = 2;
        while (root.pow(tmp >> 1) == 1) root++;
        max_base = 0;
        while (tmp % 2 == 0) tmp >>= 1, max_base++;
        r.resize(max_base), ir.resize(max_base);
        for (int i = 0; i < max_base; i++) {
            r[i] = -root.pow((mod - 1) >> (i + 2)); // r[i]  := 1 の 2^(i+2) 乗根
            ir[i] = r[i].inverse();                 // ir[i] := 1/r[i]
        }
    }

    static void ntt(vector<T> &a) {
        init();
        int n = a.size();
        assert((n & (n - 1)) == 0);
        assert(n <= (1 << max_base));
        for (int k = n; k >>= 1;) {
            T w = 1;
            for (int s = 0, t = 0; s < n; s += 2 * k) {
                for (int i = s, j = s + k; i < s + k; i++, j++) {
                    T x = a[i], y = w * a[j];
                    a[i] = x + y, a[j] = x - y;
                }
                w *= r[__builtin_ctz(++t)];
            }
        }
    }

    static void intt(vector<T> &a) {
        init();
        int n = a.size();
        assert((n & (n - 1)) == 0);
        assert(n <= (1 << max_base));
        for (int k = 1; k < n; k <<= 1) {
            T w = 1;
            for (int s = 0, t = 0; s < n; s += 2 * k) {
                for (int i = s, j = s + k; i < s + k; i++, j++) {
                    T x = a[i], y = a[j];
                    a[i] = x + y, a[j] = w * (x - y);
                }
                w *= ir[__builtin_ctz(++t)];
            }
        }
        T inv = T(n).inverse();
        for (auto &e : a) e *= inv;
    }

    static vector<T> convolve(vector<T> a, vector<T> b) {
        if (a.empty() || b.empty()) return {};
        if (min(a.size(), b.size()) < 40) {
            int n = a.size(), m = b.size();
            vector<T> c(n + m - 1, 0);
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) c[i + j] += a[i] * b[j];
            }
            return c;
        }
        int k = (int)a.size() + (int)b.size() - 1, n = 1;
        while (n < k) n <<= 1;
        a.resize(n, 0), b.resize(n, 0);
        ntt(a), ntt(b);
        for (int i = 0; i < n; i++) a[i] *= b[i];
        intt(a), a.resize(k);
        return a;
    }
};

template <typename T>
int Number_Theoretic_Transform<T>::max_base = 0;

template <typename T>
T Number_Theoretic_Transform<T>::root = T();

template <typename T>
vector<T> Number_Theoretic_Transform<T>::r = vector<T>();

template <typename T>
vector<T> Number_Theoretic_Transform<T>::ir = vector<T>();

using NTT = Number_Theoretic_Transform<mint>;

template <typename T>
struct Formal_Power_Series : vector<T> {
    using NTT_ = Number_Theoretic_Transform<T>;
    using vector<T>::vector;

    Formal_Power_Series(const vector<T> &f) : vector<T>(f) {}

    // f(x) mod x^n
    Formal_Power_Series pre(int n) const {
        Formal_Power_Series ret(begin(*this), begin(*this) + min((int)this->size(), n));
        ret.resize(n, 0);
        return ret;
    }

    // f(1/x)x^{n-1}
    Formal_Power_Series rev(int n = -1) const {
        Formal_Power_Series ret = *this;
        if (n != -1) ret.resize(n, 0);
        reverse(begin(ret), end(ret));
        return ret;
    }

    void normalize() {
        while (!this->empty() && this->back() == 0) this->pop_back();
    }

    Formal_Power_Series operator-() const {
        Formal_Power_Series ret = *this;
        for (int i = 0; i < (int)ret.size(); i++) ret[i] = -ret[i];
        return ret;
    }

    Formal_Power_Series &operator+=(const T &t) {
        if (this->empty()) this->resize(1, 0);
        (*this)[0] += t;
        return *this;
    }

    Formal_Power_Series &operator+=(const Formal_Power_Series &g) {
        if (g.size() > this->size()) this->resize(g.size());
        for (int i = 0; i < (int)g.size(); i++) (*this)[i] += g[i];
        this->normalize();
        return *this;
    }

    Formal_Power_Series &operator-=(const T &t) {
        if (this->empty()) this->resize(1, 0);
        (*this)[0] -= t;
        return *this;
    }

    Formal_Power_Series &operator-=(const Formal_Power_Series &g) {
        if (g.size() > this->size()) this->resize(g.size());
        for (int i = 0; i < (int)g.size(); i++) (*this)[i] -= g[i];
        this->normalize();
        return *this;
    }

    Formal_Power_Series &operator*=(const T &t) {
        for (int i = 0; i < (int)this->size(); i++) (*this)[i] *= t;
        return *this;
    }

    Formal_Power_Series &operator*=(const Formal_Power_Series &g) {
        if (empty(*this) || empty(g)) {
            this->clear();
            return *this;
        }
        return *this = NTT_::convolve(*this, g);
    }

    Formal_Power_Series &operator/=(const T &t) {
        assert(t != 0);
        T inv = t.inverse();
        return *this *= inv;
    }

    // f(x) を g(x) で割った商
    Formal_Power_Series &operator/=(const Formal_Power_Series &g) {
        if (g.size() > this->size()) {
            this->clear();
            return *this;
        }
        int n = this->size(), m = g.size();
        return *this = (rev() * g.rev().inv(n - m + 1)).pre(n - m + 1).rev();
    }

    // f(x) を g(x) で割った余り
    Formal_Power_Series &operator%=(const Formal_Power_Series &g) { return *this -= (*this / g) * g; }

    // f(x)/x^k
    Formal_Power_Series &operator<<=(int k) {
        Formal_Power_Series ret(k, 0);
        ret.insert(end(ret), begin(*this), end(*this));
        return *this = ret;
    }

    // f(x)x^k
    Formal_Power_Series &operator>>=(int k) {
        Formal_Power_Series ret;
        ret.insert(end(ret), begin(*this) + k, end(*this));
        return *this = ret;
    }

    Formal_Power_Series operator+(const T &t) const { return Formal_Power_Series(*this) += t; }

    Formal_Power_Series operator+(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) += g; }

    Formal_Power_Series operator-(const T &t) const { return Formal_Power_Series(*this) -= t; }

    Formal_Power_Series operator-(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) -= g; }

    Formal_Power_Series operator*(const T &t) const { return Formal_Power_Series(*this) *= t; }

    Formal_Power_Series operator*(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) *= g; }

    Formal_Power_Series operator/(const T &t) const { return Formal_Power_Series(*this) /= t; }

    Formal_Power_Series operator/(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) /= g; }

    Formal_Power_Series operator%(const Formal_Power_Series &g) const { return Formal_Power_Series(*this) %= g; }

    Formal_Power_Series operator<<(int k) const { return Formal_Power_Series(*this) <<= k; }

    Formal_Power_Series operator>>(int k) const { return Formal_Power_Series(*this) >>= k; }

    // f(c)
    T val(const T &c) const {
        T ret = 0;
        for (int i = (int)this->size() - 1; i >= 0; i--) ret *= c, ret += (*this)[i];
        return ret;
    }

    // df/dx
    Formal_Power_Series derivative() const {
        if (empty(*this)) return *this;
        int n = this->size();
        Formal_Power_Series ret(n - 1);
        for (int i = 1; i < n; i++) ret[i - 1] = (*this)[i] * i;
        return ret;
    }

    // ∫f(x)dx
    Formal_Power_Series integral() const {
        if (empty(*this)) return *this;
        int n = this->size();
        vector<T> inv(n + 1, 0);
        inv[1] = 1;
        int mod = T::get_mod();
        for (int i = 2; i <= n; i++) inv[i] = -inv[mod % i] * T(mod / i);
        Formal_Power_Series ret(n + 1, 0);
        for (int i = 0; i < n; i++) ret[i + 1] = (*this)[i] * inv[i + 1];
        return ret;
    }

    // 1/f(x) mod x^n (f[0] != 0)
    Formal_Power_Series inv(int n = -1) const {
        assert((*this)[0] != 0);
        if (n == -1) n = this->size();
        Formal_Power_Series ret(1, (*this)[0].inverse());
        for (int m = 1; m < n; m <<= 1) {
            Formal_Power_Series f = pre(2 * m), g = ret;
            f.resize(2 * m), g.resize(2 * m);
            NTT_::ntt(f), NTT_::ntt(g);
            Formal_Power_Series h(2 * m);
            for (int i = 0; i < 2 * m; i++) h[i] = f[i] * g[i];
            NTT_::intt(h);
            for (int i = 0; i < m; i++) h[i] = 0;
            NTT_::ntt(h);
            for (int i = 0; i < 2 * m; i++) h[i] *= g[i];
            NTT_::intt(h);
            for (int i = 0; i < m; i++) h[i] = 0;
            ret -= h;
        }
        ret.resize(n);
        return ret;
    }

    // log(f(x)) mod x^n (f[0] = 1)
    Formal_Power_Series log(int n = -1) const {
        assert((*this)[0] == 1);
        if (n == -1) n = this->size();
        Formal_Power_Series ret = (derivative() * inv(n)).pre(n - 1).integral();
        ret.resize(n);
        return ret;
    }

    // exp(f(x)) mod x^n (f[0] = 0)
    Formal_Power_Series exp(int n = -1) const {
        assert((*this)[0] == 0);
        if (n == -1) n = this->size();
        vector<T> inv(2 * n + 1, 0);
        inv[1] = 1;
        int mod = T::get_mod();
        for (int i = 2; i <= 2 * n; i++) inv[i] = -inv[mod % i] * T(mod / i);

        auto inplace_integral = [inv](Formal_Power_Series &f) {
            if (empty(f)) return;
            int m = f.size();
            f.insert(begin(f), 0);
            for (int i = 1; i <= m; i++) f[i] *= inv[i];
        };

        auto inplace_derivative = [](Formal_Power_Series &f) {
            if (empty(f)) return;
            int m = f.size();
            f.erase(begin(f));
            for (int i = 0; i < m - 1; i++) f[i] *= T(i + 1);
        };

        Formal_Power_Series ret{1, this->size() > 1 ? (*this)[1] : 0}, c{1}, z1, z2{1, 1};
        for (int m = 2; m < n; m *= 2) {
            auto y = ret;
            y.resize(2 * m);
            NTT_::ntt(y);
            z1 = z2;
            Formal_Power_Series z(m);
            for (int i = 0; i < m; i++) z[i] = y[i] * z1[i];
            NTT_::intt(z);
            fill(begin(z), begin(z) + m / 2, 0);
            NTT_::ntt(z);
            for (int i = 0; i < m; i++) z[i] *= -z1[i];
            NTT_::intt(z);
            c.insert(end(c), begin(z) + m / 2, end(z));
            z2 = c, z2.resize(2 * m);
            NTT_::ntt(z2);
            Formal_Power_Series x(begin(*this), begin(*this) + min((int)this->size(), m));
            inplace_derivative(x);
            x.resize(m, 0);
            NTT_::ntt(x);
            for (int i = 0; i < m; i++) x[i] *= y[i];
            NTT_::intt(x);
            x -= ret.derivative(), x.resize(2 * m);
            for (int i = 0; i < m - 1; i++) x[m + i] = x[i], x[i] = 0;
            NTT_::ntt(x);
            for (int i = 0; i < 2 * m; i++) x[i] *= z2[i];
            NTT_::intt(x);
            x.pop_back();
            inplace_integral(x);
            for (int i = m; i < min((int)this->size(), 2 * m); i++) x[i] += (*this)[i];
            fill(begin(x), begin(x) + m, 0);
            NTT_::ntt(x);
            for (int i = 0; i < 2 * m; i++) x[i] *= y[i];
            NTT_::intt(x);
            ret.insert(end(ret), begin(x) + m, end(x));
        }
        ret.resize(n);
        return ret;
    }

    // f(x)^k mod x^n
    Formal_Power_Series pow(long long k, int n = -1) const {
        if (n == -1) n = this->size();
        int m = this->size();
        for (int i = 0; i < m; i++) {
            if ((*this)[i] == 0) continue;
            T inv = (*this)[i].inverse();
            Formal_Power_Series g(m - i, 0);
            for (int j = i; j < m; j++) g[j - i] = (*this)[j] * inv;
            g = (g.log(n) * k).exp(n) * ((*this)[i].pow(k));
            Formal_Power_Series ret(n, 0);
            if (i > 0 && k > n / i) return ret;
            long long d = i * k;
            for (int j = 0; j + d < n && j < (int)g.size(); j++) ret[j + d] = g[j];
            return ret;
        }
        Formal_Power_Series ret(n, 0);
        if (k == 0) ret[0] = 1;
        return ret;
    }

    // √f(x) mod x^n (存在しなければ空)
    Formal_Power_Series sqrt(int n = -1) const {
        if (n == -1) n = this->size();
        int mod = T::get_mod();

        auto sqrt_mod = [mod](const T &a) {
            if (mod == 2) return a;
            int s = mod - 1, t = 0;
            while (s % 2 == 0) s /= 2, t++;
            T root = 2;
            while (root.pow((mod - 1) / 2) == 1) root++;
            T x = a.pow((s + 1) / 2);
            T u = root.pow(s);
            T y = x * x * a.inverse();
            while (y != 1) {
                int k = 0;
                T z = y;
                while (z != 1) k++, z *= z;
                for (int i = 0; i < t - k - 1; i++) u *= u;
                x *= u, u *= u, y *= u;
                t = k;
            }
            return x;
        };

        if ((*this)[0] == 0) {
            for (int i = 1; i < (int)this->size(); i++) {
                if ((*this)[i] != 0) {
                    if (i & 1) return {};
                    if ((*this)[i].pow((mod - 1) / 2) != 1) return {};
                    if (n <= i / 2) break;
                    return ((*this) >> i).sqrt(n - i / 2) << (i / 2);
                }
            }
            return Formal_Power_Series(n, 0);
        }
        if ((*this)[0].pow((mod - 1) / 2) != 1) return {};
        T tw = T(2).inverse();
        Formal_Power_Series ret{sqrt_mod((*this)[0])};
        for (int m = 1; m < n; m *= 2) {
            Formal_Power_Series g = (*this).pre(m * 2) * ret.inv(m * 2);
            g.resize(2 * m);
            ret = (ret + g) * tw;
        }
        ret.resize(n);
        return ret;
    }

    // f(x+c)
    Formal_Power_Series Taylor_shift(T c) const {
        int n = this->size();
        vector<T> ifac(n, 1);
        Formal_Power_Series f(n), g(n);
        for (int i = 0; i < n; i++) {
            f[n - 1 - i] = (*this)[i] * ifac[n - 1];
            if (i < n - 1) ifac[n - 1] *= i + 1;
        }
        ifac[n - 1] = ifac[n - 1].inverse();
        for (int i = n - 1; i > 0; i--) ifac[i - 1] = ifac[i] * i;
        T pw = 1;
        for (int i = 0; i < n; i++) {
            g[i] = pw * ifac[i];
            pw *= c;
        }
        f *= g;
        Formal_Power_Series b(n);
        for (int i = 0; i < n; i++) b[i] = f[n - 1 - i] * ifac[i];
        return b;
    }
};

using fps = Formal_Power_Series<mint>;

void solve() {
    ll H, W, N, K;
    cin >> H >> W >> N >> K;

    ll s = (H - K + 1) * (W - K + 1);
    // cout << s << '\n';

    comb::init(1000000);

    // ∑[1<=i<=n] i^k を k=0,...,N について計算する
    auto get = [&](ll n) {
        fps f(N + 1, 0), g(N + 1, 0);
        rep(i, N + 1) f[i] = mint(n + 1).pow(i + 1) * comb::ifac(i + 1);
        rep(i, N + 1) g[i] = comb::ifac(i + 1);
        auto h = f * g.inv();
        h.resize(N + 1);
        h[0]--;
        rep(i, N + 1) h[i] *= comb::fac(i);
        return h;
    };

    auto calc = [&](ll a, ll x, ll y) {
        mint ret = 0;
        auto f = get(x), g = get(y);
        vector<mint> pa(N + 1, 1);
        rep(i, N) pa[i + 1] = pa[i] * a;
        rep(k, N + 1) {
            ret += f[k] * g[k] * (k & 1 ? -1 : 1) * pa[N - k] * comb::C(N, k); //
        }
        return ret;
    };

    ll lx = K - 1, rx = min(K - 1, H - (K - 1));
    ll ly = K - 1, ry = min(K - 1, W - (K - 1));

    // print(get(1)), print(get(2)), print(get(3));

    mint ans = 0;

    for (auto x : {lx, rx}) {
        for (auto y : {ly, ry}) {
            ans += calc(s, x, y); //
        }
    }

    if (ly + ry < W) {
        for (auto x : {lx, rx}) {
            mint tmp = calc(s, x, K) - calc(s, x, K - 1);
            ans += tmp * mint(W - ly - ry);
        }
    }
    if (lx + rx < H) {
        for (auto y : {ly, ry}) {
            mint tmp = calc(s, K, y) - calc(s, K - 1, y);
            ans += tmp * mint(H - lx - rx);
        }
    }
    if (ly + ry < W && lx + rx < H) {
        mint tmp = mint(s - K * K).pow(N);
        ans += tmp * mint(H - lx - rx) * mint(W - ly - ry); //
    }

    ans = mint(s).pow(N) * mint(H * W) - ans;
    // cout << ans << ' ';
    cout << ans / mint(s).pow(N) << '\n';
}

int main() {
    int T = 1;
    // cin >> T;
    while (T--) solve();
}
0