結果

問題 No.1227 I hate ThREE
ユーザー outlineoutline
提出日時 2021-03-22 04:36:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 3,515 bytes
コンパイル時間 1,474 ms
コンパイル使用メモリ 143,844 KB
実行使用メモリ 120,528 KB
最終ジャッジ日時 2023-08-14 23:47:35
合計ジャッジ時間 8,064 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 5 ms
4,600 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 81 ms
43,372 KB
testcase_09 AC 71 ms
38,688 KB
testcase_10 AC 54 ms
30,300 KB
testcase_11 AC 219 ms
114,656 KB
testcase_12 AC 228 ms
120,472 KB
testcase_13 AC 27 ms
16,032 KB
testcase_14 AC 68 ms
37,096 KB
testcase_15 AC 89 ms
48,192 KB
testcase_16 AC 139 ms
74,208 KB
testcase_17 AC 106 ms
56,636 KB
testcase_18 AC 167 ms
88,316 KB
testcase_19 AC 145 ms
77,704 KB
testcase_20 AC 173 ms
92,276 KB
testcase_21 AC 15 ms
10,372 KB
testcase_22 AC 149 ms
79,068 KB
testcase_23 AC 224 ms
118,100 KB
testcase_24 AC 228 ms
120,364 KB
testcase_25 AC 228 ms
120,528 KB
testcase_26 AC 217 ms
114,752 KB
testcase_27 AC 225 ms
118,424 KB
testcase_28 AC 228 ms
120,068 KB
testcase_29 AC 226 ms
119,988 KB
testcase_30 AC 220 ms
116,304 KB
testcase_31 AC 222 ms
116,032 KB
testcase_32 AC 226 ms
119,404 KB
testcase_33 AC 218 ms
115,836 KB
testcase_34 AC 228 ms
117,556 KB
testcase_35 AC 219 ms
114,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct mint {
    int x;
    mint() : x(0) {}
    mint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    mint& operator+=(const mint& p){
        if((x += p.x) >= mod)   x -= mod;
        return *this;
    }
    mint& operator-=(const mint& p){
        if((x -= p.x) < 0)  x += mod;
        return *this;
    }
    mint& operator*=(const mint& p){
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    mint& operator/=(const mint& p){
        *this *= p.inverse();
        return *this;
    }
    mint operator-() const { return mint(-x); }
    mint operator+(const mint& p) const { return mint(*this) += p; }
    mint operator-(const mint& p) const { return mint(*this) -= p; }
    mint operator*(const mint& p) const { return mint(*this) *= p; }
    mint operator/(const mint& p) const { return mint(*this) /= p; }
    bool operator==(const mint& p) const { return x == p.x; }
    bool operator!=(const mint& p) const { return x != p.x; }
    mint pow(int64_t n) const {
        mint res = 1, mul = x;
        while(n > 0){
            if(n & 1)   res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    mint inverse() const { return pow(mod - 2); }
    friend ostream& operator<<(ostream& os, const mint& p){
        return os << p.x;
    }
    friend istream& operator>>(istream& is, mint& p){
        int64_t val;
        is >> val;
        p = mint(val);
        return is;
    }
};

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

    int N, C;
    cin >> N >> C;
    vector<vector<int>> g(N);
    for(int i = 1; i < N; ++i){
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].emplace_back(b);
        g[b].emplace_back(a);
    }
    vector<vector<mint>> dp(N, vector<mint>(min(30000, C) + 1, 1));
    auto dfs = [&](auto&& self, int from = 0, int par = -1) -> void {
        for(int to : g[from]){
            if(to == par)   continue;
            self(self, to, from);
            for(int i = 1; i <= min(30000, C); ++i){
                mint mul = 0;
                if(i - 3 >= 1)  mul += dp[to][i - 3];
                if(i + 3 <= min(30000, C))  mul += dp[to][i + 3];
                dp[from][i] *= mul;
            }
        }
    };
    dfs(dfs);
    mint ans = 0;
    if(C < 30000){
        for(int i = 1; i <= C; ++i){
            ans += dp[0][i];
        }
    }
    else{
        for(int i = 1; i < 3001; ++i)   ans += dp[0][i];
        for(int i = 30000; i > 27000; --i)  ans += dp[0][i];
        ans += dp[0][15000] * (C - 6000);
    }
    cout << ans << endl;

    return 0;
}
0