結果

問題 No.1227 I hate ThREE
ユーザー carrot46carrot46
提出日時 2020-09-13 11:14:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 3,422 bytes
コンパイル時間 1,686 ms
コンパイル使用メモリ 175,656 KB
実行使用メモリ 34,516 KB
最終ジャッジ日時 2023-09-02 13:08:33
合計ジャッジ時間 4,344 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 38 ms
33,140 KB
testcase_04 AC 39 ms
33,648 KB
testcase_05 AC 36 ms
31,840 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 7 ms
6,820 KB
testcase_09 AC 6 ms
5,960 KB
testcase_10 AC 4 ms
5,084 KB
testcase_11 AC 40 ms
31,536 KB
testcase_12 AC 44 ms
34,448 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 5 ms
5,776 KB
testcase_15 AC 8 ms
7,468 KB
testcase_16 AC 18 ms
14,400 KB
testcase_17 AC 11 ms
9,592 KB
testcase_18 AC 25 ms
19,792 KB
testcase_19 AC 20 ms
15,652 KB
testcase_20 AC 27 ms
21,344 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 20 ms
16,340 KB
testcase_23 AC 43 ms
33,364 KB
testcase_24 AC 45 ms
34,396 KB
testcase_25 AC 45 ms
34,516 KB
testcase_26 AC 41 ms
31,712 KB
testcase_27 AC 44 ms
33,336 KB
testcase_28 AC 44 ms
34,284 KB
testcase_29 AC 44 ms
34,344 KB
testcase_30 AC 41 ms
32,020 KB
testcase_31 AC 42 ms
32,040 KB
testcase_32 AC 44 ms
33,868 KB
testcase_33 AC 42 ms
31,856 KB
testcase_34 AC 43 ms
33,260 KB
testcase_35 AC 41 ms
31,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

template <unsigned long long mod > class modint{
public:
    ll x;
    constexpr modint(){x = 0;}
    constexpr modint(ll _x) : x((_x < 0 ? ((_x += (LLONG_MAX / mod) * mod) < 0 ? _x + (LLONG_MAX / mod) * mod : _x) : _x)%mod){}
    constexpr modint operator-(){
        return x == 0 ? 0 : mod - x;
    }
    constexpr modint& operator+=(const modint& a){
        if((x += a.x) >= mod) x -= mod;
        return *this;
    }
    constexpr modint operator+(const modint& a) const{
        return modint(*this) += a;
    }
    constexpr modint& operator-=(const modint& a){
        if((x -= a.x) < 0) x += mod;
        return *this;
    }
    constexpr modint operator-(const modint& a) const{
        return modint(*this) -= a;
    }
    constexpr modint& operator*=(const modint& a){
        (x *= a.x)%=mod;
        return *this;
    }
    constexpr modint operator*(const modint& a) const{
        return modint(*this) *= a;
    }
    constexpr modint pow(unsigned long long pw) const{
        modint res(1), comp(*this);
        while(pw){
            if(pw&1) res *= comp;
            comp *= comp;
            pw >>= 1;
        }
        return res;
    }
    //以下、modが素数のときのみ
    constexpr modint inv() const{
        return modint(*this).pow(mod - 2);
    }
    constexpr modint& operator/=(const modint &a){
        (x *= a.inv().x)%=mod;
        return *this;
    }
    constexpr modint operator/(const modint &a) const{
        return modint(*this) /= a;
    }
};
#define mod2 1000000007
using mint = modint<mod2>;

ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
using vm = vector<mint>;

ll dfs(int v, int p, ll len, vector<vm> &dp, mat &G){
    rep(i, min(N * 2 + 1, len)) dp[v][i] = 1;
    bool C_big = len > N * 2 + 1;
    ll child_size = 0;
    for(int to : G[v]){
        if(to != p){
            child_size += dfs(to, v, len, dp, G);
            rep(i, (int)dp[v].size()){
                if(!(C_big && i == N)){
                    mint temp(0);
                    if(i != 0) temp += dp[to][i - 1];
                    if(i != N * 2) temp += dp[to][i + 1];
                    dp[v][i] *= temp;
                }
            }
        }
    }
    if(C_big) dp[v][N] = mint(2).pow(child_size);
    ++child_size;
    return child_size;
}

int main() {
    cin>>N>>K;
    ll lw = K/3, hi = (K + 2) / 3;
    mat G(N, vec(0));
    rep(i, N - 1){
        int a, b;
        cin>>a>>b;
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    vector<vm> v_lw(N, vm(N * 2 + 1, 0)), v_hi(N, vm(N * 2 + 1, 0));
    dfs(0, -1, lw, v_lw, G);
    dfs(0, -1, hi, v_hi, G);
    int high_num = (K%3 ? K%3 : 3);
    mint res(0);
    rep(i, 3){
        res += (i < high_num) ? accumulate(ALL(v_hi[0]), mint(0)) + v_hi[0][N] * max(0LL, hi - (N * 2 + 1)) : accumulate(ALL(v_lw[0]), mint(0)) + v_lw[0][N] * max(0LL, lw - (N * 2 + 1));
    }
    cout<<res<<endl;
}


0