結果
| 問題 |
No.1227 I hate ThREE
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-09-12 22:03:55 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 209 ms / 2,000 ms |
| コード長 | 3,150 bytes |
| コンパイル時間 | 2,357 ms |
| コンパイル使用メモリ | 175,676 KB |
| 実行使用メモリ | 74,112 KB |
| 最終ジャッジ日時 | 2025-01-02 14:37:59 |
| 合計ジャッジ時間 | 7,123 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 33 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;
template<int Modulus>
struct ModInt {
long long x;
ModInt(long long x = 0) :x((x%Modulus + Modulus)%Modulus) {}
constexpr ModInt &operator+=(const ModInt a) {if ((x += a.x) >= Modulus) x -= Modulus; return *this;}
constexpr ModInt &operator-=(const ModInt a) {if ((x += Modulus - a.x) >= Modulus) x -= Modulus; return *this;}
constexpr ModInt &operator*=(const ModInt a) {(x *= a.x) %= Modulus; return *this;}
constexpr ModInt &operator/=(const ModInt a) {return *this *= a.inverse();}
constexpr ModInt operator+(const ModInt a) const {return ModInt(*this) += a.x;}
constexpr ModInt operator-(const ModInt a) const {return ModInt(*this) -= a.x;}
constexpr ModInt operator*(const ModInt a) const {return ModInt(*this) *= a.x;}
constexpr ModInt operator/(const ModInt a) const {return ModInt(*this) /= a.x;}
friend constexpr ostream& operator<<(ostream& os,const ModInt<Modulus>& a) {return os << a.x;}
friend constexpr istream& operator>>(istream& is,ModInt<Modulus>& a) {return is >> a.x;}
ModInt inverse() const {// x ^ (-1)
long long a = x,b = Modulus,p = 1,q = 0;
while (b) {long long d = a/b; a -= d*b; swap(a,b); p -= d*q; swap(p,q);}
return ModInt(p);
}
ModInt pow(long long N) {// x ^ N
ModInt a = 1;
while (N) {
if (N&1) a *= *this;
*this *= *this;
N >>= 1;
}
return a;
}
};
using mint = ModInt<1000000007>;
int N,M;
vector<vector<int>> G(N);
vector<vector<mint>> dp;
vector<vector<int>> visited;
mint dfs(int v,int c,int p = -1) {
if (visited[v][c] >= 0) return dp[v][c];
visited[v][c] = 1;
mint ans = 1;
for (int e:G[v]) {
if (e == p) continue;
mint ret = 0;
if (c - 3 > 0) ret += dfs(e,c-3,v);
if (c + 3 <= M) ret += dfs(e,c+3,v);
ans *= ret;
}
return dp[v][c] = ans;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll C;
cin >> N >> C;
M = min((ll)6*N + 10,C);
G.resize(N);
dp.assign(N,vector<mint>(M + 1,0));
visited.assign(N,vector<int>(M + 1,-1));
rep(i,N - 1) {
int a,b; cin >> a >> b;
--a; --b;
G[a].push_back(b);
G[b].push_back(a);
}
int K = min((ll)3*N + 3,C);
rep(i,K) dfs(0,i + 1);
mint ans = 0;
if (C <= K) {
rep(i,C) ans += dp[0][i + 1];
cout << ans << '\n';
} else if (C < 2*K){
rep(i,K) ans += dp[0][i + 1];
rep(i,C - K) ans += dp[0][i + 1];
cout << ans << '\n';
} else {
rep(i,K) ans += mint(2)*dp[0][i + 1];
ans += mint(C - 2*K) * dp[0][K];
cout << ans << '\n';
}
return 0;
}