結果

問題 No.827 総神童数
ユーザー PachicobuePachicobue
提出日時 2019-04-23 16:31:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 6,193 bytes
コンパイル時間 1,016 ms
コンパイル使用メモリ 80,268 KB
実行使用メモリ 23,296 KB
最終ジャッジ日時 2024-10-14 17:01:18
合計ジャッジ時間 5,229 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 1 ms
5,248 KB
testcase_09 AC 92 ms
23,296 KB
testcase_10 AC 28 ms
9,728 KB
testcase_11 AC 3 ms
5,248 KB
testcase_12 AC 13 ms
6,272 KB
testcase_13 AC 73 ms
19,840 KB
testcase_14 AC 4 ms
5,248 KB
testcase_15 AC 27 ms
9,728 KB
testcase_16 AC 68 ms
16,896 KB
testcase_17 AC 85 ms
19,968 KB
testcase_18 AC 35 ms
11,264 KB
testcase_19 AC 94 ms
15,488 KB
testcase_20 AC 67 ms
12,160 KB
testcase_21 AC 45 ms
9,856 KB
testcase_22 AC 70 ms
12,928 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 77 ms
14,040 KB
testcase_25 AC 55 ms
11,520 KB
testcase_26 AC 80 ms
14,080 KB
testcase_27 AC 48 ms
10,496 KB
testcase_28 AC 45 ms
10,240 KB
testcase_29 AC 35 ms
8,832 KB
testcase_30 AC 11 ms
5,248 KB
testcase_31 AC 29 ms
7,808 KB
testcase_32 AC 28 ms
7,552 KB
testcase_33 AC 86 ms
14,976 KB
testcase_34 AC 82 ms
14,044 KB
testcase_35 AC 29 ms
7,808 KB
testcase_36 AC 42 ms
9,600 KB
testcase_37 AC 55 ms
11,392 KB
testcase_38 AC 86 ms
14,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <utility>
//!===============================================================!//
//!   88888888b            dP       .88888.   a88888b. 888888ba   !//
//!   88                   88      d8'   '88 d8'   '88 88    '8b  !//
//!  a88aaaa    dP.  .dP d8888P    88        88        88     88  !//
//!   88         '8bd8'    88      88   YP88 88        88     88  !//
//!   88         .d88b.    88      Y8.   .88 Y8.   .88 88    .8P  !//
//!   88888888P dP'  'dP   dP       '88888'   Y88888P' 8888888P   !//
//!===============================================================!//
template <typename T>
constexpr std::pair<T, T> extgcd(const T a, const T b)
{
    if (b == 0) { return std::pair<T, T>{1, 0}; }
    const auto p = extgcd(b, a % b);
    return {p.second, p.first - p.second * (a / b)};
}
template <typename T>
constexpr T inverse(const T a, const T mod) { return (mod + extgcd((mod + a % mod) % mod, mod).first % mod) % mod; }
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
//!========================================================!//
//!  8888ba.88ba                 dP    dP            dP    !//
//!  88  '8b  '8b                88    88            88    !//
//!  88   88   88 .d8888b. .d888b88    88 88d888b. d8888P  !//
//!  88   88   88 88'  '88 88'  '88    88 88'  '88   88    !//
//!  88   88   88 88.  .88 88.  .88    88 88    88   88    !//
//!  dP   dP   dP '88888P' '88888P8    dP dP    dP   dP    !//
//!========================================================!//
template <uint mod>
class ModInt
{
private:
    uint v;
    static uint norm(const uint& x) { return x < mod ? x : x - mod; }
    static ModInt make(const uint& x)
    {
        ModInt m;
        return m.v = x, m;
    }
    static ModInt power(ModInt x, ll n)
    {
        ModInt ans = 1;
        for (; n; n >>= 1, x *= x) {
            if (n & 1) { ans *= x; }
        }
        return ans;
    }
    static ModInt inv(const ModInt& x) { return ModInt{inverse((ll)x.v, (ll)mod)}; }

public:
    ModInt() : v{0} {}
    ModInt(const ll val) : v{norm(uint(val % (ll)mod + (ll)mod))} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
    explicit operator bool() const { return v != 0; }
    ModInt<mod>& operator=(const ModInt<mod>& n) { return v = n(), (*this); }
    ModInt<mod>& operator=(const ll val) { return v = norm(uint(val % (ll)mod + (ll)mod)), (*this); }
    ModInt<mod> operator+() const { return *this; }
    ModInt<mod> operator-() const { return make(norm(mod - v)); }
    ModInt<mod> operator+(const ModInt<mod>& val) const { return make(norm(v + val())); }
    ModInt<mod> operator-(const ModInt<mod>& val) const { return make(norm(v + mod - val())); }
    ModInt<mod> operator*(const ModInt<mod>& val) const { return make((uint)((ll)v * val() % (ll)mod)); }
    ModInt<mod> operator/(const ModInt<mod>& val) const { return *this * inv(val()); }
    ModInt<mod>& operator+=(const ModInt<mod>& val) { return *this = *this + val; }
    ModInt<mod>& operator-=(const ModInt<mod>& val) { return *this = *this - val; }
    ModInt<mod>& operator*=(const ModInt<mod>& val) { return *this = *this * val; }
    ModInt<mod>& operator/=(const ModInt<mod>& val) { return *this = *this / val; }
    ModInt<mod> operator+(const ll val) const { return ModInt{v + val}; }
    ModInt<mod> operator-(const ll val) const { return ModInt{v - val}; }
    ModInt<mod> operator*(const ll val) const { return ModInt{(ll)v * (val % mod)}; }
    ModInt<mod> operator/(const ll val) const { return ModInt{(ll)v * inv(val)}; }
    template <typename I>
    ModInt<mod> operator^(const I n) const { return power(v, n); }
    ModInt<mod>& operator+=(const ll val) { return *this = *this + val; }
    ModInt<mod>& operator-=(const ll val) { return *this = *this - val; }
    ModInt<mod>& operator*=(const ll val) { return *this = *this * val; }
    ModInt<mod>& operator/=(const ll val) { return *this = *this / val; }
    template <typename I>
    ModInt<mod>& operator^=(const I n) { return (*this) = ((*this) ^ n); }
    bool operator==(const ModInt<mod>& val) const { return v == val.v; }
    bool operator!=(const ModInt<mod>& val) const { return not(*this == val); }
    bool operator==(const ll val) const { return v == norm(uint((ll)mod + val % (ll)mod)); }
    bool operator!=(const ll val) const { return not(*this == val); }
    uint operator()() const { return v; }
};
template <uint mod>
inline ModInt<mod> operator+(const ll val, const ModInt<mod>& n) { return n + val; }
template <uint mod>
inline ModInt<mod> operator-(const ll val, const ModInt<mod>& n) { return ModInt<mod>{val - (ll)n()}; }
template <uint mod>
inline ModInt<mod> operator*(const ll val, const ModInt<mod>& n) { return n * val; }
template <uint mod>
inline ModInt<mod> operator/(const ll val, const ModInt<mod>& n) { return ModInt<mod>(val) / n; }
template <uint mod>
inline bool operator==(const ll val, const ModInt<mod>& n) { return n == val; }
template <uint mod>
inline bool operator!=(const ll val, const ModInt<mod>& n) { return not(val == n); }
template <uint mod>
inline std::istream& operator>>(std::istream& is, ModInt<mod>& n)
{
    uint v;
    return is >> v, n = v, is;
}
template <uint mod>
std::ostream& operator<<(std::ostream& os, const ModInt<mod>& n) { return (os << n()); }
constexpr uint MOD = 1000000007;
using mint = ModInt<MOD>;
int main()
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int N;
    std::cin >> N;
    std::vector<std::vector<int>> G(N);
    for (int i = 0; i < N - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        G[u].push_back(v), G[v].push_back(u);
    }
    std::vector<mint> L(N + 2, 1), R(N + 2, 1);
    for (int i = 1; i <= N; i++) { L[i] = L[i - 1] * i; }
    for (int i = N; i >= 1; i--) { R[i] = R[i + 1] * i; }
    mint ans = 0;
    auto dfs = [&](auto&& self, const int s, const int p, const int d) -> void {
        ans += L[d - 1] * R[d + 1];
        for (const int to : G[s]) {
            if (to == p) { continue; }
            self(self, to, s, d + 1);
        }
    };
    dfs(dfs, 0, -1, 1);
    std::cout << ans << std::endl;
    return 0;
}
0