結果

問題 No.1227 I hate ThREE
ユーザー betrue12
提出日時 2020-09-11 23:06:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,319 bytes
コンパイル時間 3,335 ms
コンパイル使用メモリ 203,936 KB
最終ジャッジ日時 2025-01-14 11:29:53
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
    a = (a+b) % MOD;
}
void mul(int64_t& a, int64_t b){
    a = a*b % MOD;
}

int main(){
    int N, C;
    cin >> N >> C;
    vector<int> edges[1000];
    for(int i=0; i<N-1; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    C--;

    int64_t ans = 0;
    for(int r=0; r<3; r++){
        int M = (C-r)/3 + 1;

        vector<vector<int64_t>> dp(N, vector<int64_t>(2*N+1, -1));
        auto rec = [&](auto&& rec, int i, int p, int v)->int64_t{
            if(v < 0 || v >= M) return 0;
            if(dp[i][v] != -1) return dp[i][v];
            int64_t res = 1;
            for(int j : edges[i]) if(j != p){
                mul(res, rec(rec, j, i, v-1) + rec(rec, j, i, v+1));
            }
            dp[i][v] = res;
            return res;
        };
        for(int i=0; i<=N; i++) dp[0][i] = rec(rec, 0, -1, i);

        int64_t res = 0;
        if(M <= 2*N){
            for(int i=0; i<M; i++) add(res, dp[0][min(i, M-1-i)]);
        }else{
            for(int i=0; i<N; i++) add(res, 2*dp[0][i]);
            add(res, (M-2*N)*dp[0][N]);
        }
        add(ans, res);
    }
    cout << ans << endl;
    return 0;
}
0