結果

問題 No.1227 I hate ThREE
ユーザー hedwig100hedwig100
提出日時 2020-09-12 22:03:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 3,150 bytes
コンパイル時間 3,737 ms
コンパイル使用メモリ 181,004 KB
実行使用メモリ 74,172 KB
最終ジャッジ日時 2023-08-30 19:18:13
合計ジャッジ時間 8,613 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,428 KB
testcase_04 AC 5 ms
4,752 KB
testcase_05 AC 10 ms
7,436 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 15 ms
11,672 KB
testcase_09 AC 12 ms
9,964 KB
testcase_10 AC 7 ms
7,132 KB
testcase_11 AC 150 ms
67,372 KB
testcase_12 AC 180 ms
73,940 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 12 ms
9,300 KB
testcase_15 AC 21 ms
13,824 KB
testcase_16 AC 53 ms
29,128 KB
testcase_17 AC 32 ms
18,048 KB
testcase_18 AC 86 ms
40,828 KB
testcase_19 AC 65 ms
31,920 KB
testcase_20 AC 95 ms
44,172 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 65 ms
33,056 KB
testcase_23 AC 234 ms
71,276 KB
testcase_24 AC 200 ms
73,864 KB
testcase_25 AC 237 ms
74,172 KB
testcase_26 AC 160 ms
67,336 KB
testcase_27 AC 173 ms
71,396 KB
testcase_28 AC 182 ms
73,368 KB
testcase_29 AC 181 ms
73,232 KB
testcase_30 AC 171 ms
69,104 KB
testcase_31 AC 166 ms
68,848 KB
testcase_32 AC 180 ms
72,816 KB
testcase_33 AC 167 ms
68,460 KB
testcase_34 AC 173 ms
70,724 KB
testcase_35 AC 163 ms
67,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

template<int Modulus> 
struct ModInt {
    long long x;
    ModInt(long long x = 0) :x((x%Modulus + Modulus)%Modulus) {}
    constexpr ModInt &operator+=(const ModInt a) {if ((x += a.x) >= Modulus) x -= Modulus; return *this;}
    constexpr ModInt &operator-=(const ModInt a) {if ((x += Modulus - a.x) >= Modulus) x -= Modulus; return *this;}
    constexpr ModInt &operator*=(const ModInt a) {(x *= a.x) %= Modulus; return *this;}
    constexpr ModInt &operator/=(const ModInt a) {return *this *= a.inverse();}

    constexpr ModInt operator+(const ModInt a) const {return ModInt(*this) += a.x;}
    constexpr ModInt operator-(const ModInt a) const {return ModInt(*this) -= a.x;}
    constexpr ModInt operator*(const ModInt a) const {return ModInt(*this) *= a.x;}
    constexpr ModInt operator/(const ModInt a) const {return ModInt(*this) /= a.x;}
    
    friend constexpr ostream& operator<<(ostream& os,const ModInt<Modulus>& a) {return os << a.x;}
    friend constexpr istream& operator>>(istream& is,ModInt<Modulus>& a) {return is >> a.x;}
    
    ModInt inverse() const {// x ^ (-1) 
        long long a = x,b = Modulus,p = 1,q = 0;
        while (b) {long long d = a/b; a -= d*b; swap(a,b); p -= d*q; swap(p,q);}
        return ModInt(p);
    }
    ModInt pow(long long N) {// x ^ N 
        ModInt a = 1;
        while (N) {
            if (N&1) a *= *this;
            *this *= *this;
            N >>= 1;
        }
        return a;
    }
};

using mint = ModInt<1000000007>;

int N,M;
vector<vector<int>> G(N);
vector<vector<mint>> dp;
vector<vector<int>> visited;

mint dfs(int v,int c,int p = -1) {
    if (visited[v][c] >= 0) return dp[v][c];
    visited[v][c] = 1;
    mint ans = 1;
    for (int e:G[v]) {
        if (e == p) continue;
        mint ret = 0;
        if (c - 3 > 0) ret += dfs(e,c-3,v);
        if (c + 3 <= M) ret += dfs(e,c+3,v);
        ans *= ret;
    }
    return dp[v][c] = ans;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll C;
    cin >> N >> C;
    M = min((ll)6*N + 10,C);
    G.resize(N);
    dp.assign(N,vector<mint>(M + 1,0));
    visited.assign(N,vector<int>(M + 1,-1));
    rep(i,N - 1) {
        int a,b; cin >> a >> b;
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int K = min((ll)3*N + 3,C);
    rep(i,K) dfs(0,i + 1);
    mint ans = 0;
    if (C <= K) {
        rep(i,C) ans += dp[0][i + 1];
        cout << ans << '\n';
    } else if (C < 2*K){
        rep(i,K) ans += dp[0][i + 1];
        rep(i,C - K) ans += dp[0][i + 1];
        cout << ans << '\n';
    } else {
        rep(i,K) ans += mint(2)*dp[0][i + 1];
        ans += mint(C - 2*K) * dp[0][K];
        cout << ans << '\n';
    }
    return 0;
}
0