結果

問題 No.1667 Forest
ユーザー ぷらぷら
提出日時 2021-09-07 02:35:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,161 ms / 3,000 ms
コード長 1,209 bytes
コンパイル時間 2,274 ms
コンパイル使用メモリ 200,536 KB
実行使用メモリ 8,948 KB
最終ジャッジ日時 2023-08-24 13:20:21
合計ジャッジ時間 16,790 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,161 ms
8,716 KB
testcase_01 AC 2,135 ms
8,948 KB
testcase_02 AC 2,089 ms
8,780 KB
testcase_03 AC 28 ms
8,168 KB
testcase_04 AC 2,067 ms
8,732 KB
testcase_05 AC 1,199 ms
8,596 KB
testcase_06 AC 714 ms
8,528 KB
testcase_07 AC 428 ms
8,412 KB
testcase_08 AC 203 ms
8,336 KB
testcase_09 AC 121 ms
8,252 KB
testcase_10 AC 64 ms
8,300 KB
testcase_11 AC 19 ms
8,108 KB
testcase_12 AC 14 ms
8,004 KB
testcase_13 AC 14 ms
8,008 KB
testcase_14 AC 14 ms
8,252 KB
testcase_15 AC 14 ms
8,112 KB
testcase_16 AC 14 ms
7,992 KB
testcase_17 AC 14 ms
8,032 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;
}

long long 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++) {
                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;
                dp[i+k-1][j+1] %= MOD;
            }
        }
    }
    for(int i = 0; i < N; i++) {
        cout << dp[i][N-i] << endl;
    }
}
0