結果

問題 No.1227 I hate ThREE
ユーザー tkmst201tkmst201
提出日時 2020-09-11 22:52:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 3,679 bytes
コンパイル時間 3,384 ms
コンパイル使用メモリ 228,896 KB
実行使用メモリ 85,560 KB
最終ジャッジ日時 2023-08-30 14:37:53
合計ジャッジ時間 8,785 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 66 ms
83,476 KB
testcase_04 AC 67 ms
84,364 KB
testcase_05 AC 73 ms
81,272 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 46 ms
31,404 KB
testcase_09 AC 41 ms
28,272 KB
testcase_10 AC 31 ms
22,056 KB
testcase_11 AC 169 ms
81,568 KB
testcase_12 AC 177 ms
85,228 KB
testcase_13 AC 18 ms
12,108 KB
testcase_14 AC 48 ms
27,172 KB
testcase_15 AC 63 ms
34,612 KB
testcase_16 AC 107 ms
52,792 KB
testcase_17 AC 75 ms
40,624 KB
testcase_18 AC 138 ms
62,816 KB
testcase_19 AC 114 ms
55,388 KB
testcase_20 AC 148 ms
65,688 KB
testcase_21 AC 10 ms
8,068 KB
testcase_22 AC 120 ms
56,488 KB
testcase_23 AC 266 ms
83,980 KB
testcase_24 AC 248 ms
85,324 KB
testcase_25 AC 275 ms
85,560 KB
testcase_26 AC 200 ms
81,340 KB
testcase_27 AC 212 ms
84,024 KB
testcase_28 AC 213 ms
84,996 KB
testcase_29 AC 210 ms
85,060 KB
testcase_30 AC 203 ms
82,440 KB
testcase_31 AC 206 ms
82,344 KB
testcase_32 AC 217 ms
84,724 KB
testcase_33 AC 202 ms
82,152 KB
testcase_34 AC 208 ms
83,500 KB
testcase_35 AC 201 ms
81,460 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