結果

問題 No.1321 塗るめた
ユーザー tokusakuraitokusakurai
提出日時 2020-12-18 11:02:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 12,679 bytes
コンパイル時間 2,987 ms
コンパイル使用メモリ 222,060 KB
実行使用メモリ 12,940 KB
最終ジャッジ日時 2023-10-21 07:57:27
合計ジャッジ時間 12,263 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 10 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 157 ms
8,620 KB
testcase_13 AC 314 ms
12,164 KB
testcase_14 AC 151 ms
7,416 KB
testcase_15 AC 154 ms
8,044 KB
testcase_16 AC 158 ms
8,748 KB
testcase_17 AC 10 ms
4,348 KB
testcase_18 AC 317 ms
12,940 KB
testcase_19 AC 76 ms
5,988 KB
testcase_20 AC 153 ms
7,616 KB
testcase_21 AC 319 ms
12,444 KB
testcase_22 AC 318 ms
12,824 KB
testcase_23 AC 318 ms
12,820 KB
testcase_24 AC 318 ms
12,820 KB
testcase_25 AC 318 ms
12,820 KB
testcase_26 AC 319 ms
12,824 KB
testcase_27 AC 319 ms
12,824 KB
testcase_28 AC 320 ms
12,820 KB
testcase_29 AC 320 ms
12,820 KB
testcase_30 AC 319 ms
12,824 KB
testcase_31 AC 318 ms
12,824 KB
testcase_32 AC 157 ms
8,620 KB
testcase_33 AC 152 ms
7,468 KB
testcase_34 AC 159 ms
8,620 KB
testcase_35 AC 152 ms
7,468 KB
testcase_36 AC 320 ms
12,824 KB
testcase_37 AC 321 ms
12,824 KB
testcase_38 AC 321 ms
12,824 KB
testcase_39 AC 321 ms
12,820 KB
testcase_40 AC 319 ms
12,820 KB
testcase_41 AC 319 ms
12,820 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 160 ms
8,620 KB
testcase_44 AC 153 ms
7,468 KB
testcase_45 AC 158 ms
8,620 KB
testcase_46 AC 38 ms
4,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#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>;
//const int MOD = 1000000007;
const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
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;};

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

template<int mod>
struct Mod_Int{
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % 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(ll k) const{
        Mod_Int now = *this, ret = 1;
        for(; k; 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){
        ll a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

template<int mod>
struct Combination{
    using T = Mod_Int<mod>;
    vector<T> _fac, _ifac;

    Combination(int n){
        _fac.resize(n+1), _ifac.resize(n+1);
        _fac[0] = 1;
        rep2(i, 1, n) _fac[i] = _fac[i-1]*i;
        _ifac[n] = _fac[n].inverse();
        rep3(i, n, 1) _ifac[i-1] = _ifac[i]*i;
    }

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

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

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

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

    T second_stirling_number(int n, int k) const{
        T ret = 0;
        rep(i, k+1){
            T tmp = comb(k, i)*T(i).pow(n);
            ret += ((k-i)&1)? -tmp : tmp;
        }
        return ret*ifac(k);
    }

    T bell_number(int n, int k) const{
        if(n == 0) return 1;
        chmin(k, n);
        vector<T> pref(k+1);
        pref[0] = 1;
        rep2(i, 1, k){
            if(i&1) pref[i] = pref[i-1]-ifac(i);
            else pref[i] = pref[i-1]+ifac(i);   
        }
        T ret = 0;
        rep2(i, 1, k){
            ret += T(i).pow(n)*ifac(i)*pref[k-i];
        }
        return ret;
    }
};

using comb = Combination<MOD>;

using mint = Mod_Int<998244353>;

template<int mod, int primitive_root>
struct Number_Theorem_Transform{
    using T = Mod_Int<mod>;
    vector<T> r, ir;

    Number_Theorem_Transform(){
        r.resize(30), ir.resize(30);
        rep(i, 30){
            r[i] = -T(primitive_root).pow((mod-1)>>(i+2));
            ir[i] = r[i].inverse();
        }
    }

    void ntt(vector<T> &a, int n) const{
        assert((n&(n-1)) == 0);
        a.resize(n);
        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)];
            }
        }
    }

    void intt(vector<T> &a, int n) const{
        assert((n&(n-1)) == 0);
        a.resize(n);
        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;
    }

    vector<T> convolve(vector<T> a, vector<T> b) const{
        int k = sz(a)+sz(b)-1, n = 1;
        while(n < k) n <<= 1;
        ntt(a, n), ntt(b, n);
        rep(i, n) a[i] *= b[i];
        intt(a, n), a.resize(k);
        return a;
    }
};

Number_Theorem_Transform<998244353, 3> NTT;

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

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

    Formal_Power_Series pre(int n) const{
        return Formal_Power_Series(begin(*this), begin(*this)+min((int)this->size(), n));
    }

    Formal_Power_Series rev() const{
        Formal_Power_Series ret = *this;
        reverse(all(ret));
        return ret;
    }

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

    Formal_Power_Series operator - () const noexcept{
        Formal_Power_Series ret = *this;
        rep(i, sz(ret)) ret[i] = -ret[i];
        return ret; 
    }

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

    Formal_Power_Series &operator += (const Formal_Power_Series &v){
        if(v.size() > this->size()) this->resize(sz(v));
        rep(i, sz(v)) (*this)[i] += v[i];
        return this->normalize();
    }

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

    Formal_Power_Series &operator -= (const Formal_Power_Series &v){
        if(v.size() > this->size()) this->resize(sz(v));
        rep(i, sz(v)) (*this)[i] -= v[i];
        return this->normalize();
    }

    Formal_Power_Series &operator *= (const T &x){
        rep(i, this->size()) (*this)[i] *= x;
        return *this;
    }

    Formal_Power_Series &operator *= (const Formal_Power_Series &v){
        return *this = NTT.convolve(*this, v);
    }

    Formal_Power_Series &operator /= (const T &x){
        assert(x != 0);
        T inv = x.inverse();
        rep(i, this->size()) (*this)[i] *= inv;
        return *this;
    }

    Formal_Power_Series &operator /= (const Formal_Power_Series &v){
        if(v.size() > this->size()){
            this->clear();
            return *this;
        }
        int n = this->size()-sz(v)+1;
        return *this = (rev().pre(n)*v.rev().inv(n)).pre(n).rev(n);
    }

    Formal_Power_Series &operator %= (const Formal_Power_Series &v){
        return *this -= (*this/v)*v;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    T val(const T &x) const{
        T ret = 0;
        rep3(i, this->size()-1, 0) ret *= x, ret += (*this)[i];
        return ret;
    }

    Formal_Power_Series diff() const{ // df/dx
        int n = this->size();
        Formal_Power_Series ret(n-1);
        rep2(i, 1, n-1) ret[i-1] = (*this)[i]*i;
        return ret;
    }

    Formal_Power_Series integral() const{ // ∫fdx
        int n = this->size();
        Formal_Power_Series ret(n+1);
        rep(i, n) ret[i+1] = (*this)[i]/(i+1);
        return ret;
    }

    Formal_Power_Series inv(int deg) const{ // 1/f (f[0] != 0)
        assert((*this)[0] != T(0));
        Formal_Power_Series ret(1, (*this)[0].inverse());
        for(int i = 1; i < deg; i <<= 1){
            Formal_Power_Series f = pre(i<<1), g = ret;
            NTT.ntt(f, 2*i), NTT.ntt(g, 2*i);
            Formal_Power_Series h(2*i);
            rep(j, 2*i) h[j] = f[j]*g[j];
            NTT.intt(h, 2*i);
            rep(j, i) h[j] = 0;
            NTT.ntt(h, 2*i);
            rep(j, 2*i) h[j] *= g[j];
            NTT.intt(h, 2*i);
            rep(j, i) h[j] = 0;
            ret -= h;
            //ret = (ret+ret-ret*ret*pre(i<<1)).pre(i<<1);
        }
        ret.resize(deg);
        return ret;
    }

    Formal_Power_Series inv() const {return inv(this->size());}

    Formal_Power_Series log(int deg) const{ // log(f) (f[0] = 1)
        assert((*this)[0] == 1);
        Formal_Power_Series ret = (diff()*inv(deg)).pre(deg-1).integral();
        ret.resize(deg);
        return ret;
    }

    Formal_Power_Series log() const {return log(this->size());}

    Formal_Power_Series exp(int deg) const{ // exp(f) (f[0] = 0)
        assert((*this)[0] == 0);
        Formal_Power_Series ret(1, 1);
        for(int i = 1; i < deg; i <<= 1){
            ret = (ret*(pre(i<<1)+1-ret.log(i<<1))).pre(i<<1);
        }
        ret.resize(deg);
        return ret;
    }

    Formal_Power_Series exp() const {return exp(this->size());}

    Formal_Power_Series pow(ll k, int deg) const{ // f^k
        int n = this->size();
        rep(i, n){
            if((*this)[i] == 0) continue;
            T rev = (*this)[i].inverse();
            Formal_Power_Series C(*this*rev), D(n-i, 0);
            rep2(j, i, n-1) D[j-i] = C[j];
            D = (D.log()*k).exp()*((*this)[i].pow(k));
            Formal_Power_Series E(deg, 0);
            if(i > 0 && k > deg/i) return E;
            ll S = i*k;
            for(int j = 0; j+S < deg && j < D.size(); j++) E[j+S] = D[j];
            E.resize(deg);
            return E;
        }
        return Formal_Power_Series(deg, 0);
    }

    Formal_Power_Series pow(ll k) const {return pow(k, this->size());}
};

using fps = Formal_Power_Series<mint>;

int main(){
    int N, M, K;
    cin >> N >> M >> K;

    comb C(N);
    vector<mint> pw(N+1, 1), pw2(N+1, 1);
    rep(i, N) pw[i+1] = pw[i]*2, pw2[i+1] = pw2[i]*(M-K);

    fps f(N+1);
    rep(i, N+1) f[i] = (pw[i]-1)*C.ifac(i);

    f = f.pow(K);
    mint ans = 0;
    rep(i, N+1){
        ans += f[i]*pw2[N-i]*C.ifac(N-i);
    }
    ans *= C.fac(N)*C.comb(M, K);

    cout << ans << '\n';
}
0