結果

問題 No.758 VVVVV
ユーザー pekempeypekempey
提出日時 2018-12-06 00:46:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 1,136 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 72,284 KB
実行使用メモリ 11,320 KB
最終ジャッジ日時 2023-08-04 04:39:59
合計ジャッジ時間 2,779 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,000 KB
testcase_01 AC 3 ms
6,124 KB
testcase_02 AC 2 ms
6,004 KB
testcase_03 AC 74 ms
11,320 KB
testcase_04 AC 34 ms
8,500 KB
testcase_05 AC 24 ms
7,876 KB
testcase_06 AC 22 ms
7,652 KB
testcase_07 AC 74 ms
11,252 KB
testcase_08 AC 38 ms
8,668 KB
testcase_09 AC 15 ms
7,044 KB
testcase_10 AC 26 ms
7,864 KB
testcase_11 AC 81 ms
11,148 KB
testcase_12 AC 19 ms
7,404 KB
testcase_13 AC 24 ms
7,828 KB
testcase_14 AC 14 ms
6,932 KB
testcase_15 AC 9 ms
6,620 KB
testcase_16 AC 11 ms
6,908 KB
testcase_17 AC 64 ms
10,464 KB
testcase_18 AC 34 ms
8,552 KB
testcase_19 AC 77 ms
11,156 KB
testcase_20 AC 21 ms
7,480 KB
testcase_21 AC 22 ms
7,748 KB
testcase_22 AC 82 ms
11,312 KB
testcase_23 AC 2 ms
6,000 KB
testcase_24 AC 3 ms
6,132 KB
testcase_25 AC 3 ms
6,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

// https://www.geeksforgeeks.org/combinatorics-ordered-trees/

using namespace std;

const long long mod = 1000000007;

vector<int> g[100000];
long long F[100001];
long long IF[100001];
long long inv[100001];
int L;

void dfs(int u, int p) {
    bool l = true;
    for (int v : g[u]) if (v != p) {
        dfs(v, u);
        l = false;
    }
    L += l;
}

long long C(int n, int r) {
    if (n < 0 || r < 0 || n < r) return 0;
    return F[n] * IF[r] % mod * IF[n - r] % mod;
}

int main() {
    int n;
    cin >> n;
    if (n == 1) {
        cout << 1 << endl;
        return 0;
    }
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0, -1);
    inv[1] = 1;
    for (int i = 2; i <= n; i++) {
        inv[i] = inv[mod % i] * (mod - mod / i) % mod;
    }
    F[0] = 1;
    IF[0] = 1;
    for (int i = 1; i <= n; i++) {
        F[i] = F[i - 1] * i % mod;
        IF[i] = IF[i - 1] * inv[i] % mod;
    }
    cout << C(n - 1, L) * C(n - 1, L - 1) % mod * inv[n - 1] % mod << endl;
}
0