結果

問題 No.196 典型DP (1)
ユーザー kappybarkappybar
提出日時 2021-03-23 15:06:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 3,545 bytes
コンパイル時間 1,946 ms
コンパイル使用メモリ 179,292 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2024-05-04 01:11:56
合計ジャッジ時間 4,173 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 3 ms
6,948 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 5 ms
6,944 KB
testcase_18 AC 6 ms
6,944 KB
testcase_19 AC 7 ms
6,944 KB
testcase_20 AC 9 ms
6,944 KB
testcase_21 AC 9 ms
6,940 KB
testcase_22 AC 9 ms
6,940 KB
testcase_23 AC 14 ms
10,240 KB
testcase_24 AC 12 ms
8,320 KB
testcase_25 AC 12 ms
7,808 KB
testcase_26 AC 20 ms
19,584 KB
testcase_27 AC 20 ms
19,712 KB
testcase_28 AC 19 ms
19,584 KB
testcase_29 AC 20 ms
19,712 KB
testcase_30 AC 20 ms
19,584 KB
testcase_31 AC 12 ms
6,940 KB
testcase_32 AC 11 ms
6,940 KB
testcase_33 AC 11 ms
6,944 KB
testcase_34 AC 11 ms
6,940 KB
testcase_35 AC 11 ms
6,944 KB
testcase_36 AC 11 ms
6,940 KB
testcase_37 AC 10 ms
6,940 KB
testcase_38 AC 11 ms
6,940 KB
testcase_39 AC 11 ms
6,944 KB
testcase_40 AC 11 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y))
#define chmax(x,y) x = max((x),(y))
#define popcount(x) __builtin_popcount(x)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e18;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;

template<int mod> struct ModInt{
    long long x=0; 
    constexpr ModInt(long long x=0):x((x%mod+mod)%mod){}
    constexpr ModInt operator+(const ModInt& r)const{return ModInt(*this)+=r;}
    constexpr ModInt operator-(const ModInt& r)const{return ModInt(*this)-=r;}
    constexpr ModInt operator*(const ModInt& r)const{return ModInt(*this)*=r;}
    constexpr ModInt operator/(const ModInt& r)const{return ModInt(*this)/=r;}
    constexpr ModInt& operator+=(const ModInt& r){ if((x+=r.x)>=mod) x-=mod; return *this;}
    constexpr ModInt& operator-=(const ModInt& r){ if((x-=r.x)<0) x+=mod; return *this;}
    constexpr ModInt& operator*=(const ModInt& r){ if((x*=r.x)>=mod) x%=mod; return *this;}
    constexpr ModInt& operator/=(const ModInt& r){ return *this*=r.inv();}
    constexpr bool operator==(const ModInt& r){ return x == r.x;}
    constexpr bool operator!=(const ModInt& r){ return x != r.x;}
    ModInt inv() const {
        long long s=x,sx=1,sy=0,t=mod,tx=0,ty=1;
        while(s%t!=0){
            long long temp=s/t,u=s-t*temp,ux=sx-temp*tx,uy=sy-temp*ty;
            s=t;sx=tx;sy=ty;
            t=u;tx=ux;ty=uy;
        }
        return ModInt(tx);
    }
    ModInt pow(long long n) const {
        ModInt a=1;
        ModInt b=*this;
        while(n>0){
            if(n&1) a*=b;
            b*=b;
            n>>=1;
        }
        return a;
    }
    friend constexpr ostream& operator<<(ostream& os,const ModInt<mod>& a) {return os << a.x;}
    friend constexpr istream& operator>>(istream& is,ModInt<mod>& a) {return is >> a.x;}
};
using mint = ModInt<MOD>;

struct combination{
    int N;
    vector<mint> fac,finv,inverse;
    combination(int N):N(N){
        fac.resize(N+1);finv.resize(N+1);
        fac[0]=1;
        for(int i=1;i<=N;++i){fac[i]=fac[i-1]*i;}
        finv[N]=fac[N].inv();
        for(int i=N-1;i>=0;--i){finv[i]=finv[i+1]*(i+1);}
    }
    void inverse_build(){
        inverse.resize(N+1);
        inverse[1]=1;
        for(int i=2;i<=N;++i){inverse[i]=finv[i]*fac[i-1];}
    }
    mint c(int n,int k){
        if(n<k||n<0||k<0) return 0;
        return fac[n]*finv[n-k]*finv[k];
    }
    mint p(int n,int k){
        if(n<k||n<0||k<0) return 0;
        return fac[n]*finv[n-k];
    }
};

int main(){
    int n,k;
    cin >> n >> k;
    vector<vector<int>> G(n);
    rep(i,n-1){
        int a,b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<int> sz(n,0);
    vector<vector<mint>> dp(n);
    auto dfs = [&](auto&& dfs,int i,int p) -> void {
        sz[i] = 1;
        dp[i] = {1,1};
        for(int v:G[i]){
            if(v == p) continue;
            dfs(dfs,v,i);
            vector<mint> merged(sz[i]+sz[v]+1,0);
            for(int k=0;k<sz[i];k++){
                for(int j=0;j<=sz[v];j++){
                    merged[j+k] += dp[i][k] * dp[v][j];
                }
            }
            merged[sz[i]+sz[v]] += dp[i][sz[i]] * dp[v][sz[v]];
            sz[i] += sz[v];
            dp[i] = merged;
        }
        return;
    };
    dfs(dfs,0,-1);
    cout << dp[0][k] << endl;
    return 0;
}
0