結果

問題 No.1227 I hate ThREE
ユーザー sahiyasahiya
提出日時 2020-12-17 18:53:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 1,939 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 118,304 KB
実行使用メモリ 50,660 KB
最終ジャッジ日時 2023-10-21 07:04:11
合計ジャッジ時間 6,845 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 16 ms
49,512 KB
testcase_04 AC 17 ms
50,068 KB
testcase_05 AC 20 ms
49,360 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 20 ms
20,668 KB
testcase_09 AC 16 ms
18,616 KB
testcase_10 AC 11 ms
14,516 KB
testcase_11 AC 119 ms
49,360 KB
testcase_12 AC 132 ms
50,584 KB
testcase_13 AC 5 ms
10,412 KB
testcase_14 AC 18 ms
18,612 KB
testcase_15 AC 28 ms
22,712 KB
testcase_16 AC 66 ms
32,960 KB
testcase_17 AC 39 ms
26,812 KB
testcase_18 AC 92 ms
39,108 KB
testcase_19 AC 71 ms
35,008 KB
testcase_20 AC 100 ms
41,160 KB
testcase_21 AC 4 ms
8,364 KB
testcase_22 AC 75 ms
35,008 KB
testcase_23 AC 196 ms
49,728 KB
testcase_24 AC 197 ms
50,584 KB
testcase_25 AC 204 ms
50,660 KB
testcase_26 AC 154 ms
49,356 KB
testcase_27 AC 164 ms
49,740 KB
testcase_28 AC 172 ms
50,396 KB
testcase_29 AC 170 ms
50,348 KB
testcase_30 AC 159 ms
49,356 KB
testcase_31 AC 158 ms
49,356 KB
testcase_32 AC 169 ms
50,208 KB
testcase_33 AC 156 ms
49,356 KB
testcase_34 AC 162 ms
49,504 KB
testcase_35 AC 154 ms
49,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef bitset<16> BS;

const ll MOD = 1E+09 + 7;
const ll INF = 1E18;
const int MAX_N = 1000;

ll N, C;

ll dp[MAX_N + 1][6 * MAX_N];
vector<ll> G[MAX_N];

ll tree(ll i, ll par, ll j);
ll modPow(ll x, ll a);

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

    cin >> N >> C;

    for (int i = 1; i <= N - 1; i++) {
        ll a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    fill(dp[0], dp[N + 1], -1);
    ll ans = 0;
    for (int i = 1; i <= min(C, 6 * N - 5); i++) {
        ans += tree(1, 0, i);
        ans %= MOD;
    }
    ans += (modPow(2, N - 1) * max(0LL, C - 6 * N + 5)) % MOD;
    ans %= MOD;

    /*
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
        }
    }
    */

    cout << ans << "\n";

    return 0;
}

ll tree(ll i, ll par, ll j)
{
    if (j < 1 || j > min(C, 6 * N - 5))
        return 0;
    if (dp[i][j] != -1)
        return dp[i][j];
    ll res = 1;
    for (auto c : G[i]) {
        if (c != par) {
            res *= (tree(c, i, j + 3) + tree(c, i, j - 3)) % MOD;
            res %= MOD;
        }
    }
    return dp[i][j] = res;
}

ll modPow(ll x, ll a)
{
    ll ans = 1;
    while (a > 0) {
        if (a & 1) {
            ans = (ans * x) % MOD;
        }
        x = (x * x) % MOD;
        a >>= 1;
    }
    return ans;
}
0