結果

問題 No.1227 I hate ThREE
ユーザー KudeKude
提出日時 2020-09-13 10:44:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 3,288 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 177,012 KB
実行使用メモリ 11,476 KB
最終ジャッジ日時 2023-09-02 12:28:17
合計ジャッジ時間 3,827 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 18 ms
5,972 KB
testcase_09 AC 15 ms
5,448 KB
testcase_10 AC 13 ms
5,024 KB
testcase_11 AC 56 ms
10,776 KB
testcase_12 AC 61 ms
11,028 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 12 ms
4,376 KB
testcase_16 AC 18 ms
4,380 KB
testcase_17 AC 14 ms
4,380 KB
testcase_18 AC 21 ms
4,376 KB
testcase_19 AC 18 ms
4,376 KB
testcase_20 AC 21 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 19 ms
4,376 KB
testcase_23 AC 31 ms
11,476 KB
testcase_24 AC 30 ms
8,792 KB
testcase_25 AC 32 ms
11,448 KB
testcase_26 AC 27 ms
4,380 KB
testcase_27 AC 28 ms
4,376 KB
testcase_28 AC 28 ms
4,380 KB
testcase_29 AC 28 ms
4,376 KB
testcase_30 AC 27 ms
4,376 KB
testcase_31 AC 27 ms
4,376 KB
testcase_32 AC 28 ms
4,376 KB
testcase_33 AC 27 ms
4,376 KB
testcase_34 AC 27 ms
4,376 KB
testcase_35 AC 27 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

#define MOD 1000000007
//#define MOD 998244353
struct mint {
	int i;
	mint() : i(0) {}
	mint(int x) {
		i = int(x % MOD);
		if (i < 0) i += MOD;
	}
	template<class T> mint(T x) {
		i = int(x % MOD);
		if (i < 0) i += MOD;
	}
	mint operator+(const mint x) const {return i + x.i;}
	mint operator-(const mint x) const {return i - x.i;}
	mint operator*(const mint x) const {return (long long)i * x.i;}
	mint operator/(const mint x) const {return (long long)i * x.pow(MOD - 2).i;}
	mint inv() {return pow(MOD - 2);}
	template<class T> mint pow(T p) const {
		long long r = 1;
		long long t = i;
		for(; p > 0; p >>= 1) {
			if (p & 1) r = r * t % MOD;
			t = t * t % MOD;
		}
		return r;
	}
	template<class T1, class T2> static mint pow(T1 a, T2 b) {
		long long r = 1;
		long long t = (long long)(a % MOD);
		for(; b > 0; b >>= 1) {
			if (b & 1) r = r * t % MOD;
			t = t * t % MOD;
		}
		return r;
	}
	mint& operator+=(const mint x) {
		i = (i + x.i) % MOD;
		return *this;
	}
	mint& operator-=(const mint x) {
		i = i - x.i;
		if (i < 0) i += MOD;
		return *this;
	}
	mint& operator*=(const mint x) {
		i = (int)((long long)i * x.i % MOD);
		return *this;
	}
	mint& operator/=(const mint x) {
		i = (long long)i * x.pow(MOD - 2).i % MOD;
		return *this;
	}
};

std::ostream& operator<<(std::ostream& os, const mint& m) {
	return os << m.i;
}



VVI to;
int n, c;
vector<mint> init_v;
bool first_init = true;
int sz;

vector<mint> dfs(int u=0, int p=-1) {
    vector<mint> dp(sz);
    vector<vector<mint>> cdp;
    for(int v: to[u]) {
        if (v == p) continue;
        cdp.push_back(dfs(v, u));
    }
	if (cdp.size() == 0 && first_init) {
		first_init = false;
		return init_v;
	}
    rep(i, sz) {
        mint t = 1;
        for(auto& d: cdp) {
            mint acc = 0;
            if (i - 1 >= 0) acc += d[i-1];
            if (i + 1 < sz) acc += d[i+1];
            t *= acc;
        }
        dp[i] = t;
    }
    return dp;
}

int main() {
    cin >> n >> c;
    to.resize(n);
    rep(i, n-1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }
    if (c <= 6100) {
		auto f = [&]() {
			init_v = vector<mint>(sz);
			rep(i, sz) init_v[i] = 1;
			first_init = true;
			auto a = dfs();
			mint ret = 0;
			rep(i, sz) ret += a[i];
			return ret;
		};
		int c1 = c / 3;
		int r = c % 3;
		sz = c1;
		mint ans = f() * (3 - r);
		sz++;
		ans += f() * r;
		cout << ans << endl;
	} else {
		sz = 2000;
		init_v = vector<mint>(sz);
		rep(i, 999) init_v[i] = 1;
		first_init = true;
		auto v_bounded = dfs();
		mint s_bounded = 0;
		rep(i, sz) s_bounded += v_bounded[i];
		init_v = vector<mint>(sz);
		init_v[999] = 1;
		first_init = true;
		auto v_unbounded = dfs();
		mint s_unbounded = 0;
		rep(i, sz) s_unbounded += v_unbounded[i];
		mint ans = s_bounded * 6;
		int c1 = c / 3;
		int r = c % 3;
		ans += s_unbounded * (c1 - 999 * 2) * (3 - r);
		ans += s_unbounded * (c1 + 1 - 999 * 2) * r;
		cout << ans << endl;
	}
}
0