結果

問題 No.758 VVVVV
ユーザー 👑 nu50218nu50218
提出日時 2019-09-03 00:24:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,467 bytes
コンパイル時間 1,814 ms
コンパイル使用メモリ 171,920 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2024-04-22 03:09:11
合計ジャッジ時間 3,171 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 69 ms
9,680 KB
testcase_04 AC 31 ms
6,940 KB
testcase_05 AC 24 ms
6,940 KB
testcase_06 AC 21 ms
6,940 KB
testcase_07 AC 70 ms
9,592 KB
testcase_08 AC 34 ms
6,940 KB
testcase_09 AC 13 ms
6,940 KB
testcase_10 AC 24 ms
6,940 KB
testcase_11 AC 72 ms
9,840 KB
testcase_12 AC 17 ms
6,940 KB
testcase_13 AC 22 ms
6,940 KB
testcase_14 AC 13 ms
6,940 KB
testcase_15 AC 8 ms
6,940 KB
testcase_16 AC 10 ms
6,940 KB
testcase_17 AC 63 ms
9,008 KB
testcase_18 AC 32 ms
6,940 KB
testcase_19 AC 73 ms
9,804 KB
testcase_20 AC 20 ms
6,940 KB
testcase_21 AC 21 ms
6,944 KB
testcase_22 AC 74 ms
9,984 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MOD = 1000000007;

long long pow(long long x, long long n, long long mod) {
    long long ans = 1;
    while (n) {
        if (n % 2) ans *= x;
        x *= x;
        x %= mod;
        ans %= mod;
        n >>= 1;
    }
    return ans;
}
vector<long long> factorial(2, 1);
long long comb(long long n, long long k, long long p) {
    if (n < k || n <= 0 || k < 0) return 0;
    while (factorial.size() <= n) {
        long long i = factorial.size();
        factorial.push_back(i * factorial[i - 1] % p);
    }
    return ((factorial[n] * pow(factorial[k], p - 2, p)) % p) *
           pow(factorial[n - k], p - 2, p) % p;
}

long long Narayana(const long long& n, const long long& k, const long long& p) {
    return comb(n, k, p) * comb(n, k - 1, p) % p * pow(n, MOD - 2, p) % p;
}

vector<vector<int>> adj;
int leaf_count = 0;

void dfs(int x, int prev) {
    bool is_leaf = true;
    for (auto next : adj[x]) {
        if (next != prev) {
            dfs(next, x);
            is_leaf = false;
        }
    }
    leaf_count += is_leaf;
}

int main() {
    int N;
    cin >> N;
    if (N == 1) {
        cout << 1 << endl;
        return 0;
    }
    adj.resize(N);
    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(0, -1);
    cout << Narayana(N - 1, leaf_count, MOD) << endl;
}
0