結果

問題 No.196 典型DP (1)
ユーザー ゆにぽけゆにぽけ
提出日時 2023-10-02 22:43:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 3,778 bytes
コンパイル時間 1,099 ms
コンパイル使用メモリ 117,648 KB
実行使用メモリ 50,776 KB
最終ジャッジ日時 2023-10-02 22:43:50
合計ジャッジ時間 3,770 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
19,088 KB
testcase_01 AC 8 ms
19,132 KB
testcase_02 AC 7 ms
19,224 KB
testcase_03 AC 7 ms
19,080 KB
testcase_04 AC 7 ms
19,108 KB
testcase_05 AC 7 ms
19,104 KB
testcase_06 AC 7 ms
19,128 KB
testcase_07 AC 7 ms
19,136 KB
testcase_08 AC 7 ms
19,076 KB
testcase_09 AC 7 ms
19,336 KB
testcase_10 AC 7 ms
19,296 KB
testcase_11 AC 7 ms
19,324 KB
testcase_12 AC 7 ms
19,232 KB
testcase_13 AC 7 ms
19,560 KB
testcase_14 AC 7 ms
19,360 KB
testcase_15 AC 8 ms
19,452 KB
testcase_16 AC 8 ms
19,336 KB
testcase_17 AC 10 ms
19,568 KB
testcase_18 AC 12 ms
19,548 KB
testcase_19 AC 13 ms
19,588 KB
testcase_20 AC 15 ms
19,696 KB
testcase_21 AC 14 ms
19,492 KB
testcase_22 AC 14 ms
19,476 KB
testcase_23 AC 27 ms
32,820 KB
testcase_24 AC 23 ms
27,796 KB
testcase_25 AC 22 ms
27,256 KB
testcase_26 AC 39 ms
50,564 KB
testcase_27 AC 40 ms
50,776 KB
testcase_28 AC 39 ms
50,588 KB
testcase_29 AC 39 ms
50,576 KB
testcase_30 AC 39 ms
50,652 KB
testcase_31 AC 19 ms
19,168 KB
testcase_32 AC 19 ms
19,204 KB
testcase_33 AC 20 ms
19,152 KB
testcase_34 AC 20 ms
19,136 KB
testcase_35 AC 20 ms
19,160 KB
testcase_36 AC 19 ms
19,400 KB
testcase_37 AC 15 ms
19,260 KB
testcase_38 AC 19 ms
19,176 KB
testcase_39 AC 20 ms
19,180 KB
testcase_40 AC 19 ms
19,144 KB
testcase_41 AC 7 ms
19,084 KB
testcase_42 AC 7 ms
19,068 KB
testcase_43 AC 7 ms
19,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cassert>
#include<cmath>
#include<ctime>
#include<iomanip>
#include<numeric>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<random>
using namespace std;
template<long long MOD>struct Modint
{
    private:
    unsigned int value;

    public:
    constexpr Modint(const long long x = 0) noexcept
    {
	long long y = x;
	if(y < 0 || y >= MOD)
	{
		y %= MOD;
		if(y < 0) y += MOD;
	}
        value = (unsigned int)y;
    }

    constexpr unsigned int &val() noexcept {return value;}

    constexpr Modint operator+(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp += other;
    }

    constexpr Modint operator-(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp -= other;
    }

    constexpr Modint operator*(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp *= other;
    }

    constexpr Modint operator/(const Modint &other) noexcept
    {
        Modint tmp = *this;
        return tmp /= other;
    }

    constexpr Modint &operator+=(const Modint &other) noexcept
    {
        value += other.value;
        if(value >= MOD) value -= MOD;

        return *this;
    }

    constexpr Modint &operator-=(const Modint &other) noexcept
    {
        unsigned long long  x = value;
        if(x < other.value) x += MOD;
        x -= other.value;

        value = (unsigned int) x;

        return *this;
    }

    constexpr Modint &operator*=(const Modint &other) noexcept
    {
        unsigned long long x = value;
        x *= other.value;
        value = (unsigned int)(x%MOD);

        return *this;
    }

    constexpr Modint &operator/=(const Modint &other) noexcept
    {
        return *this = *this * other.inverse();
    }

    constexpr Modint power(long long ex) const noexcept
    {
        assert(ex >= 0);
        Modint p = *this,res = 1;

        while(ex)
        {
            if(ex & 1) res *= p;
            p *= p;
            ex >>= 1;
        }

        return res;
    }

    constexpr Modint inverse() const noexcept
    {
        assert(value);
        long long a = value,b = MOD,x = 1,y = 0;

        while(b)
        {
            long long q = a/b;

            a -= q*b; swap(a,b);
            x -= q*y; swap(x,y);
        }

        return Modint(x);
    }

    constexpr Modint &operator++(int) noexcept {return *this += 1;}

    constexpr Modint &operator--(int) noexcept {return *this -= 1;}

    constexpr bool operator==(const Modint &other){return value == other.value;}

    constexpr bool operator!=(const Modint &other){return value != other.value;}

    template<long long mod> friend ostream &operator<<(ostream &os,const Modint<mod> &x);
};

template<long long mod> ostream &operator<<(ostream &os,const Modint<mod> &x){os << x.value; return os;}

//using mint = Modint<998244353>;
using mint = Modint<1000000007>;
int N,K,ch[2000];
vector<int> G[2000];
mint dp[2000][2001];
void dfs(int u,int p)
{
	ch[u] = 1;
	mint ep[2][2001];
	int now = 0;
	for(int i = 0;i <= N;i++) ep[now][i] = 0;
	ep[now][0] = 1;
	for(int v:G[u]) if(v != p)
	{
		dfs(v,u);
		int nxt = 1-now;
		for(int i = 0;i <= ch[u]+ch[v];i++) ep[nxt][i] = 0;
		for(int i = 0;i < ch[u];i++)for(int j = 0;j <= ch[v];j++) ep[nxt][i+j] += ep[now][i]*dp[v][j];
		now = nxt;
		ch[u] += ch[v];
	}
	for(int i = 0;i <= N;i++) dp[u][i] = ep[now][i];
	dp[u][ch[u]] = 1;
}
void solve()
{
	cin >> N >> K;
	for(int i = 1;i < N;i++)
	{
		int u,v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	dfs(0,-1);

	cout << dp[0][K] << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int T = 1;
	//cin >> T;
	while(T--) solve();
}
0