結果

問題 No.758 VVVVV
ユーザー 👑 nu50218nu50218
提出日時 2019-09-03 00:23:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,436 bytes
コンパイル時間 1,891 ms
コンパイル使用メモリ 174,984 KB
実行使用メモリ 9,980 KB
最終ジャッジ日時 2024-05-10 02:34:07
合計ジャッジ時間 9,730 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 618 ms
9,812 KB
testcase_04 AC 272 ms
6,400 KB
testcase_05 AC 194 ms
5,376 KB
testcase_06 AC 179 ms
5,376 KB
testcase_07 AC 587 ms
9,592 KB
testcase_08 AC 298 ms
6,528 KB
testcase_09 AC 107 ms
5,376 KB
testcase_10 AC 214 ms
5,888 KB
testcase_11 AC 620 ms
9,848 KB
testcase_12 AC 153 ms
5,376 KB
testcase_13 AC 192 ms
5,376 KB
testcase_14 AC 102 ms
5,376 KB
testcase_15 AC 67 ms
5,376 KB
testcase_16 AC 81 ms
5,376 KB
testcase_17 AC 509 ms
9,012 KB
testcase_18 AC 273 ms
6,272 KB
testcase_19 AC 642 ms
9,804 KB
testcase_20 AC 168 ms
5,376 KB
testcase_21 AC 179 ms
5,376 KB
testcase_22 AC 642 ms
9,980 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 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) {
    cerr << x << ' ' << prev << endl;
    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;
    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