結果
| 問題 | No.1227 I hate ThREE |
| コンテスト | |
| ユーザー |
tkmst201
|
| 提出日時 | 2020-09-11 22:52:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 300 ms / 2,000 ms |
| コード長 | 3,679 bytes |
| 記録 | |
| コンパイル時間 | 3,286 ms |
| コンパイル使用メモリ | 225,268 KB |
| 最終ジャッジ日時 | 2025-01-14 11:15:43 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:95:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
95 | scanf("%d %d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~
ソースコード
#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;
}
tkmst201