結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 37 ms
32,828 KB
testcase_04 AC 38 ms
33,620 KB
testcase_05 AC 35 ms
31,680 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 7 ms
6,736 KB
testcase_09 AC 5 ms
6,100 KB
testcase_10 AC 4 ms
5,104 KB
testcase_11 AC 38 ms
31,676 KB
testcase_12 AC 42 ms
34,520 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 5 ms
5,768 KB
testcase_15 AC 9 ms
7,800 KB
testcase_16 AC 17 ms
14,348 KB
testcase_17 AC 10 ms
9,700 KB
testcase_18 AC 24 ms
19,768 KB
testcase_19 AC 18 ms
15,660 KB
testcase_20 AC 26 ms
21,568 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 20 ms
16,332 KB
testcase_23 AC 41 ms
33,080 KB
testcase_24 AC 44 ms
34,456 KB
testcase_25 AC 43 ms
34,460 KB
testcase_26 AC 39 ms
31,852 KB
testcase_27 AC 42 ms
33,552 KB
testcase_28 AC 43 ms
34,248 KB
testcase_29 AC 42 ms
34,288 KB
testcase_30 AC 40 ms
32,028 KB
testcase_31 AC 40 ms
32,032 KB
testcase_32 AC 42 ms
33,920 KB
testcase_33 AC 40 ms
31,888 KB
testcase_34 AC 41 ms
32,868 KB
testcase_35 AC 38 ms
31,828 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>;

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

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