結果

問題 No.196 典型DP (1)
ユーザー minatominato
提出日時 2020-04-08 07:51:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 3,305 bytes
コンパイル時間 2,423 ms
コンパイル使用メモリ 177,468 KB
実行使用メモリ 94,588 KB
最終ジャッジ日時 2023-09-25 01:18:01
合計ジャッジ時間 5,122 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 6 ms
8,484 KB
testcase_16 AC 9 ms
14,324 KB
testcase_17 AC 13 ms
20,624 KB
testcase_18 AC 19 ms
27,036 KB
testcase_19 AC 25 ms
29,192 KB
testcase_20 AC 24 ms
33,260 KB
testcase_21 AC 23 ms
33,212 KB
testcase_22 AC 26 ms
33,320 KB
testcase_23 AC 63 ms
59,888 KB
testcase_24 AC 52 ms
49,952 KB
testcase_25 AC 47 ms
48,748 KB
testcase_26 AC 79 ms
93,548 KB
testcase_27 AC 81 ms
93,704 KB
testcase_28 AC 79 ms
94,164 KB
testcase_29 AC 80 ms
93,676 KB
testcase_30 AC 80 ms
94,588 KB
testcase_31 AC 20 ms
32,956 KB
testcase_32 AC 20 ms
33,088 KB
testcase_33 AC 20 ms
33,016 KB
testcase_34 AC 20 ms
32,948 KB
testcase_35 AC 20 ms
33,020 KB
testcase_36 AC 19 ms
33,036 KB
testcase_37 AC 21 ms
32,992 KB
testcase_38 AC 20 ms
32,984 KB
testcase_39 AC 19 ms
33,200 KB
testcase_40 AC 20 ms
33,052 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define ln '\n'
constexpr long long MOD = 1000000007LL;
//constexpr long long MOD = 998244353LL;
typedef long long ll;
typedef unsigned long long ull; 
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; }
template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


template <std::uint_fast64_t Modulus> 
struct ModInt {
    using u64 = std::uint_fast64_t;

    u64 a;

    constexpr ModInt(const long long x = 0) noexcept : a(x >= 0 ? x % Modulus : (Modulus - (-x) % Modulus) % Modulus) {}
    constexpr u64 &value() noexcept { return a; }
    constexpr const u64 &value() const noexcept { return a; }
    constexpr ModInt operator+(const ModInt rhs) const noexcept {return ModInt(*this) += rhs;}
    constexpr ModInt operator-(const ModInt rhs) const noexcept {return ModInt(*this) -= rhs;}
    constexpr ModInt operator*(const ModInt rhs) const noexcept {return ModInt(*this) *= rhs;}
    constexpr ModInt operator/(const ModInt rhs) const noexcept {return ModInt(*this) /= rhs;}
    constexpr ModInt &operator+=(const ModInt rhs) noexcept {
        a += rhs.a;
        if (a >= Modulus) {
            a -= Modulus;
        }
        return *this;
    }
    constexpr ModInt &operator-=(const ModInt rhs) noexcept {
        if (a < rhs.a) {
            a += Modulus;
        }
        a -= rhs.a;
        return *this;
    }
    constexpr ModInt &operator*=(const ModInt rhs) noexcept {
        a = a * rhs.a % Modulus;
        return *this;
    }
    constexpr ModInt &operator/=(ModInt rhs) noexcept {
        u64 exp = Modulus - 2;
        while (exp) {
            if (exp % 2) {
                *this *= rhs;
            }
            rhs *= rhs;
            exp /= 2;
        }
        return *this;
    }

    bool operator==(const ModInt &p) const {return a == p.a;}
    bool operator!=(const ModInt &p) const {return a != p.a;}

    ModInt pow(long long N) const {
        ModInt ret(1), mul(a);
        while(N > 0) {
            if(N & 1) ret *= mul;
            mul *= mul;
            N >>= 1;
        }
        return ret;
    }
};

using mint = ModInt<MOD>;

mint ans[2020][2020];
vector<int> G[2020];
int sub[2020];

void dfs(int v, int pv) {
    vector<mint> arr(2020);
    int sz = 1;
    arr[0] = 1;
    for (auto nv : G[v]) {
        if (nv==pv) continue;
        vector<mint> dp2(2020);
        dfs(nv,v);
        rep(i,sz) {
            rep(j,sub[nv]+1) {
                dp2[i+j] += arr[i]*ans[nv][j];
            }
        }
        swap(arr,dp2);
        sz += sub[nv];
    }
    sub[v] = sz;
    rep(i,sub[v]) ans[v][i] = arr[i];
    ans[v][sub[v]] = 1;
    return;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,K; cin >> N >> K;
    rep(i,N-1) {
        int u,v; cin >> u >> v;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    dfs(0,-1);
    cout << ans[0][K].a << ln;
}
0