結果

問題 No.1667 Forest
ユーザー ぷらぷら
提出日時 2022-09-30 23:25:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,998 ms / 3,000 ms
コード長 1,233 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 198,996 KB
実行使用メモリ 8,588 KB
最終ジャッジ日時 2023-08-24 16:58:36
合計ジャッジ時間 16,211 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,998 ms
8,584 KB
testcase_01 AC 1,977 ms
8,508 KB
testcase_02 AC 1,936 ms
8,588 KB
testcase_03 AC 27 ms
8,244 KB
testcase_04 AC 1,913 ms
8,448 KB
testcase_05 AC 1,106 ms
8,348 KB
testcase_06 AC 657 ms
8,332 KB
testcase_07 AC 394 ms
8,244 KB
testcase_08 AC 188 ms
8,188 KB
testcase_09 AC 111 ms
8,448 KB
testcase_10 AC 60 ms
8,192 KB
testcase_11 AC 20 ms
8,084 KB
testcase_12 AC 15 ms
8,016 KB
testcase_13 AC 15 ms
8,028 KB
testcase_14 AC 16 ms
8,040 KB
testcase_15 AC 15 ms
8,084 KB
testcase_16 AC 15 ms
8,040 KB
testcase_17 AC 15 ms
8,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long modpow(long long a,long long b,long long m) {
    long long ans = 1;
    while(b) {
        if(b & 1) {
            (ans *= a) %= m;
        }
        (a *= a) %= m;
        b /= 2;
    }
    return ans;
}

int mod;

long long fac[200005], finv[200005], inv[200005];

void COMinit() {
    fac[0] = fac[1] = finv[0] = finv[1] = inv[1] = 1;
    for (int i = 2; i < 200005; i++) {
        fac[i] = fac[i - 1] * i % mod;
        inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
}

long long 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] % mod) % mod;
}

void mpl(int &x,int y) {
    x += y;
    if(x >= mod) x -= mod;
}

int dp[305][305];

int main() {
    int N,MOD;
    cin >> N >> MOD;
    dp[0][0] = 1;
    mod = MOD;
    COMinit();
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            for(int k = 1; i+k-1 < N; k++) {
                mpl(dp[i+k-1][j+1],dp[i][j]*COM(N-i-j-1,k-1)%mod*modpow(k,max(0,k-2),MOD)%mod);
            }
        }
    }
    for(int i = 0; i < N; i++) {
        cout << dp[i][N-i] << endl;
    }
}
0