結果

問題 No.1302 Random Tree Score
ユーザー yuto1115yuto1115
提出日時 2020-11-27 22:51:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,838 ms / 3,000 ms
コード長 7,093 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 212,192 KB
実行使用メモリ 30,468 KB
最終ジャッジ日時 2023-10-09 20:53:50
合計ジャッジ時間 17,175 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 322 ms
9,992 KB
testcase_03 AC 787 ms
17,060 KB
testcase_04 AC 304 ms
9,916 KB
testcase_05 AC 1,838 ms
29,984 KB
testcase_06 AC 1,500 ms
30,136 KB
testcase_07 AC 292 ms
10,004 KB
testcase_08 AC 901 ms
17,448 KB
testcase_09 AC 1,460 ms
30,248 KB
testcase_10 AC 1,561 ms
28,880 KB
testcase_11 AC 282 ms
9,576 KB
testcase_12 AC 1,781 ms
29,688 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1,487 ms
30,292 KB
testcase_15 AC 1,713 ms
30,468 KB
testcase_16 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> void fin(T a) { cout << a << endl; exit(0); }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

//constexpr int mod = 1000000007;
constexpr int mod = 998244353;

struct mint {
    ll x;
    
    constexpr mint(ll x = 0) : x((x % mod + mod) % mod) {}
    
    constexpr mint operator-() const { return mint(-x); }
    
    constexpr mint &operator+=(const mint &a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    
    constexpr mint &operator++() { return *this += mint(1); }
    
    constexpr mint &operator-=(const mint &a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    
    constexpr mint &operator--() { return *this -= mint(1); }
    
    constexpr mint &operator*=(const mint &a) {
        (x *= a.x) %= mod;
        return *this;
    }
    
    constexpr mint operator+(const mint &a) const {
        mint res(*this);
        return res += a;
    }
    
    constexpr mint operator-(const mint &a) const {
        mint res(*this);
        return res -= a;
    }
    
    constexpr mint operator*(const mint &a) const {
        mint res(*this);
        return res *= a;
    }
    
    constexpr mint pow(ll t) const {
        mint res = mint(1), a(*this);
        while (t > 0) {
            if (t & 1) res *= a;
            t >>= 1;
            a *= a;
        }
        return res;
    }
    
    // for prime mod
    constexpr mint inv() const { return pow(mod - 2); }
    
    constexpr mint &operator/=(const mint &a) { return *this *= a.inv(); }
    
    constexpr mint operator/(const mint &a) const {
        mint res(*this);
        return res /= a;
    }
};

ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

bool operator==(const mint &a, const mint &b) { return a.x == b.x; }

bool operator!=(const mint &a, const mint &b) { return a.x != b.x; }

bool operator==(const mint &a, const int &b) { return a.x == b; }

bool operator!=(const mint &a, const int &b) { return a.x != b; }

template< typename Mint >
struct NumberTheoreticTransformFriendlyModInt {
    
    vector< int > rev;
    vector< Mint > rts;
    int base, max_base;
    Mint root;
    
    NumberTheoreticTransformFriendlyModInt() : base(1), rev{0, 1}, rts{0, 1} {
        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.pow((mod - 1) >> 1) == 1) root += 1;
        assert(root.pow(mod - 1) == 1);
        root = root.pow((mod - 1) >> max_base);
    }
    
    void ensure_base(int nbase) {
        if(nbase <= base) return;
        rev.resize(1 << nbase);
        rts.resize(1 << nbase);
        for(int i = 0; i < (1 << nbase); i++) {
            rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
        }
        assert(nbase <= max_base);
        while(base < nbase) {
            Mint z = root.pow(1 << (max_base - 1 - base));
            for(int i = 1 << (base - 1); i < (1 << base); i++) {
                rts[i << 1] = rts[i];
                rts[(i << 1) + 1] = rts[i] * z;
            }
            ++base;
        }
    }
    
    
    void ntt(vector< Mint > &a) {
        const int n = (int) a.size();
        assert((n & (n - 1)) == 0);
        int zeros = __builtin_ctz(n);
        ensure_base(zeros);
        int shift = base - zeros;
        for(int i = 0; i < n; i++) {
            if(i < (rev[i] >> shift)) {
                swap(a[i], a[rev[i] >> shift]);
            }
        }
        for(int k = 1; k < n; k <<= 1) {
            for(int i = 0; i < n; i += 2 * k) {
                for(int j = 0; j < k; j++) {
                    Mint z = a[i + j + k] * rts[j + k];
                    a[i + j + k] = a[i + j] - z;
                    a[i + j] = a[i + j] + z;
                }
            }
        }
    }
    
    
    void intt(vector< Mint > &a) {
        const int n = (int) a.size();
        ntt(a);
        reverse(a.begin() + 1, a.end());
        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++;
        ensure_base(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;
        }
        reverse(a.begin() + 1, a.end());
        ntt(a);
        a.resize(need);
        return a;
    }
};

NumberTheoreticTransformFriendlyModInt<mint> ntt;

vector<mint> pow(const vector<mint> &f, int k) {
    int n = f.size();
    vector<mint> res(n, 0);
    res[0] = 1;
    vector<mint> now = f;
    while (k > 0) {
        if (k & 1) {
            res = ntt.multiply(res, now);
            if (res.size() > n) res.erase(res.begin() + n, res.end());
        }
        now = ntt.multiply(now, now);
        if (now.size() > n) now.erase(now.begin() + n, now.end());
        k >>= 1;
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    vector<mint> v(2 * n - 1, 0);
    mint now = 1;
    rep2(i, 1, n) {
        if (i > 1) now /= i - 1;
        v[i] = now * i;
    }
    auto ans = pow(v, n);
    now = 1;
    rep2(i, 1, n - 1) now *= i;
    cout << ans[2 * n - 2] * now / mint(n).pow(n - 2) << endl;
}
0