結果

問題 No.1667 Forest
ユーザー nawawannawawan
提出日時 2021-09-04 18:44:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 3,000 ms
コード長 3,507 bytes
コンパイル時間 2,012 ms
コンパイル使用メモリ 205,308 KB
実行使用メモリ 13,712 KB
最終ジャッジ日時 2023-08-23 08:18:00
合計ジャッジ時間 5,410 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
13,712 KB
testcase_01 AC 284 ms
13,676 KB
testcase_02 AC 278 ms
13,668 KB
testcase_03 AC 39 ms
13,004 KB
testcase_04 AC 270 ms
13,664 KB
testcase_05 AC 179 ms
13,464 KB
testcase_06 AC 125 ms
13,332 KB
testcase_07 AC 91 ms
13,216 KB
testcase_08 AC 63 ms
13,168 KB
testcase_09 AC 52 ms
13,076 KB
testcase_10 AC 45 ms
13,092 KB
testcase_11 AC 38 ms
12,988 KB
testcase_12 AC 37 ms
13,000 KB
testcase_13 AC 36 ms
12,968 KB
testcase_14 AC 36 ms
12,960 KB
testcase_15 AC 37 ms
12,956 KB
testcase_16 AC 36 ms
12,960 KB
testcase_17 AC 36 ms
12,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct modint{
    long long val;
    modint(): val(0) {}
    modint(long long x){
        if(x < 0) val = x % mod() + mod();
        else val = x % mod();
    }
    modint(const modint &t){
        val = t.val;
    }
    static int &mod(){
        static int m = 0;
        return m;
    }
    static void set_mod(int md){
        mod() = md;
    }
    modint& operator =(const modint m){
        val = m.val;
        return *this;
    }
    modint operator -(){
        return modint(-val);
    }
    modint& operator-=(const modint &m){
        val -= m.val;
        if(val < 0) val += mod();
        return *this;
    }
    modint& operator+=(const modint &m){
        val += m.val;
        if(val >= mod()) val -= mod();
        return *this;
    }
    modint& operator*=(const modint &m){
        val *= m.val;
        val %= mod();
        return *this;
    }
    modint& operator/=(modint m){
        *this *= m.inv();
        return *this;
    }
    modint inv(){
        long long x = 1, y = 0;
        long long a = val, b = mod();
        while(b != 0){
            long long t = a / b;
            a -= t * b;
            x -= t * y;
            swap(a, b);
            swap(x, y);
        }
        x %= mod();
        if(x < 0) x += mod();
        return modint(x);
    }
    modint pow(long long k){
        long long res = 1;
        long long v = val;
        while(k > 0){
            if(k & 1) res = res * v % mod();
            v = v * v % mod();
            k >>= 1;
        }
        return modint(res);
    }
    bool operator==(const modint &m){
        return val == m.val;
    }
    modint operator+(const modint &m){
        return modint(*this) += m;
    }
    modint operator-(const modint &m){
        return modint(*this) -= m;
    }
    modint operator*(const modint &m){
        return modint(*this) *= m;
    }
    modint operator/(const modint &m){
        return modint(*this) /= m;
    }
    bool operator!=(const modint &m){
        return modint(*this).val != m.val;
    }
    bool operator!=(const int &m){
        return modint(*this).val != m;
    }
};
using mint = modint;
const int MAX = 410000;
mint fac[MAX], finv[MAX], inv[MAX];
//MAX < MOD
void COMinit(){
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i;
        inv[i] = mint(mint::mod()) - inv[mint::mod() % i] * (mint::mod() / i);
        finv[i] = finv[i - 1] * inv[i];
    }
}
mint COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * finv[k] * finv[n - k];
}
mint repow(long long x, long long y){
    if(y == 0) return 1;
    mint res = 1;
    mint x2 = x;
    while(y > 0){
        if(y & 1) res = res * x2;
        x2 = x2 * x2;
        y >>= 1;
    }
    return res;
}
int main(){
    int N, MOD;
    cin >> N >> MOD;
    mint::set_mod(MOD);
    COMinit();
    vector<vector<mint>> dp(N + 1, vector<mint>(N + 1));
    dp[0][0] = 1;
    vector<mint> tr(N + 1);
    for(int i = 2; i <= N; i++) {
        tr[i] = repow(i, i - 2);
    }
    for(int i = 0; i < N; i++){
        for(int j = 0; j <= i; j++){
            if(i != 0 && i == j) continue;
            for(int k = 1; k <= N - i; k++){
                if(k == 1) dp[i + 1][j] += dp[i][j];
                else dp[i + k][j + k - 1] += dp[i][j] * COM(N - i - 1, k - 1) * tr[k];
            }
        }
    }
    for(int i = 0; i < N; i++) cout << dp[N][i].val << endl;
}
0