結果

問題 No.1227 I hate ThREE
ユーザー tkmst201tkmst201
提出日時 2020-09-11 22:52:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 2,000 ms
コード長 3,679 bytes
コンパイル時間 2,822 ms
コンパイル使用メモリ 232,492 KB
実行使用メモリ 85,760 KB
最終ジャッジ日時 2024-06-10 14:35:31
合計ジャッジ時間 7,655 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 56 ms
83,840 KB
testcase_04 AC 58 ms
84,608 KB
testcase_05 AC 65 ms
81,664 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 40 ms
31,616 KB
testcase_09 AC 35 ms
28,160 KB
testcase_10 AC 27 ms
22,400 KB
testcase_11 AC 149 ms
81,664 KB
testcase_12 AC 160 ms
85,632 KB
testcase_13 AC 14 ms
12,416 KB
testcase_14 AC 41 ms
27,392 KB
testcase_15 AC 56 ms
34,944 KB
testcase_16 AC 99 ms
52,992 KB
testcase_17 AC 68 ms
40,832 KB
testcase_18 AC 126 ms
63,104 KB
testcase_19 AC 103 ms
55,552 KB
testcase_20 AC 131 ms
65,792 KB
testcase_21 AC 8 ms
8,448 KB
testcase_22 AC 108 ms
56,704 KB
testcase_23 AC 234 ms
83,840 KB
testcase_24 AC 213 ms
85,504 KB
testcase_25 AC 235 ms
85,760 KB
testcase_26 AC 187 ms
81,792 KB
testcase_27 AC 194 ms
84,224 KB
testcase_28 AC 199 ms
85,248 KB
testcase_29 AC 193 ms
85,120 KB
testcase_30 AC 186 ms
82,560 KB
testcase_31 AC 182 ms
82,688 KB
testcase_32 AC 191 ms
84,992 KB
testcase_33 AC 183 ms
82,304 KB
testcase_34 AC 187 ms
83,712 KB
testcase_35 AC 189 ms
81,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

template<int M>
struct ModInt {
public:
	using value_type = long long;
	
	ModInt(value_type val = 0) : val(val < 0 ? (M - (-val % M)) % M : val % M) {}
	
	explicit operator bool() const noexcept { return val; }
	bool operator ==(const ModInt & rhs) const noexcept { return val == rhs.val; }
	bool operator !=(const ModInt & rhs) const noexcept { return !(*this == rhs); }
	ModInt operator +() const noexcept { return ModInt(*this); }
	ModInt operator -() const noexcept { return ModInt(0) -= *this; }
	ModInt operator +(const ModInt & rhs) const noexcept { return ModInt(*this) += rhs; }
	ModInt operator -(const ModInt & rhs) const noexcept { return ModInt(*this) -= rhs; }
	ModInt operator *(const ModInt & rhs) const noexcept { return ModInt(*this) *= rhs; }
	ModInt operator /(const ModInt & rhs) const noexcept { return ModInt(*this) /= rhs; }
	
	ModInt & operator +=(const ModInt & rhs) noexcept {
		val += rhs.val;
		if (val >= M) val -= M;
		return *this;
	}
	ModInt & operator -=(const ModInt & rhs) noexcept {
		if (val < rhs.val) val += M;
		val -= rhs.val;
		return *this;
	}
	ModInt & operator *=(const ModInt & rhs) noexcept {
		val = val * rhs.val % M;
		return *this;
	}
	ModInt & operator /=(const ModInt & rhs) noexcept {
		*this *= rhs.inverse();
		return *this;
	}
	
	ModInt pow(value_type n) const {
		ModInt res = 1, x = val;
		if (n < 0) { x = x.inverse(); n = -n; }
		while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
		return res;
	}
	
	ModInt inverse() const {
		long long a = val, a1 = 1, a2 = 0, b = M, b1 = 0, b2 = 1;
		while (b > 0) {
			value_type q = a / b, r = a % b;
			value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
			a = b; b = r;
			a1 = b1; b1 = nb1;
			a2 = b2; b2 = nb2;
		}
		assert(a == 1);
		return a1;
	}
	
	const value_type & get() const noexcept { return val; }
	static decltype(M) get_mod() noexcept { return M; }
	
	friend std::ostream & operator <<(std::ostream & os, const ModInt & rhs) { return os << rhs.val; }
	friend std::istream & operator >>(std::istream & is, ModInt & rhs) {
		value_type x;
		is >> x;
		rhs = ModInt(x);
		return is;
	}
private:
	value_type val;
};
using mint = ModInt<MOD>;

int main() {
	int N, C;
	cin >> N >> C;
	vector<vector<int>> g(N);
	REP(i, N - 1) {
		int u, v;
		scanf("%d %d", &u, &v);
		--u; --v;
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	
	vector<vector<int>> done(N, vector<int>(7000));
	vector<vector<mint>> dp(N, vector<mint>(7000)); // [i][j] := 頂点 i の値が j であるときの部分木 i のパターン数
	
	auto dfs = [&](auto && self, int u, int c, int par) -> mint {
		if (done[u][c]) return dp[u][c];
		done[u][c] = true;
		
		mint p = 1;
		for (int v : g[u]) if (v != par) {
			mint sum = 0;
			if (c + 3 <= C) sum += self(self, v, c + 3, u);
			if (c - 3 >= 1) sum += self(self, v, c - 3, u);
			p *= sum;
		}
		return dp[u][c] = p;
	};
	
	mint ans;
	if (C < 7000) {
		FOR(i, 1, C + 1) ans += dfs(dfs, 0, i, -1);
	}
	else {
		FOR(i, 1, 3501) ans += dfs(dfs, 0, i, -1) * 2;
		ans += dfs(dfs, 0, 3500, -1) * (C - 7000);
	}
	cout << ans << endl;
	return 0;
}
0