結果
問題 | No.1667 Forest |
ユーザー | Sooh317 |
提出日時 | 2021-09-03 22:30:02 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 774 ms / 3,000 ms |
コード長 | 3,024 bytes |
コンパイル時間 | 4,602 ms |
コンパイル使用メモリ | 252,392 KB |
最終ジャッジ日時 | 2025-01-24 06:20:39 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 15 |
ソースコード
/** * author: Sooh * created: 03.09.2021 22:07:42 **/ #include<bits/stdc++.h> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; //using mint = modint998244353; //using mint = modint1000000007; #endif #pragma region template using ll = long long; template <class T> using V = vector<T>; #define rep(i,n) for(int i=0;i<n;++i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define pb push_back #define eb emplace_back #define fi first #define se second #define debug(var) do{std::cerr << #var << " : ";view(var);}while(0) template<typename T> void view(T e){std::cerr << e << std::endl;} template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << std::endl;} template<typename T> void view(const std::vector<std::vector<T> >& vv){cerr << endl;int cnt = 0;for(const auto& v : vv){cerr << cnt << "th : "; view(v); cnt++;} cerr << endl;} ll power(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a;} a = a * a; p >>= 1;} return ret;} ll modpow(ll a, ll p, ll mod){ll ret = 1; while(p){if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;} ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0; while (b) {ll t = a / b ;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;} template<class T, class K>bool chmax(T &a, const K b) { if (a<b) { a=b; return 1; } return 0; } template<class T, class K>bool chmin(T &a, const K b) { if (b<a) { a=b; return 1; } return 0; } #pragma endregion int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; const int inf = 1001001001; const ll INF = 1001001001001001001ll; //const double pi = acos(-1); //const ll mod = 998244353; //const ll mod = 1000000007; ll n, mod; const int MAX = 1000; long long fact[MAX], finv[MAX], inv[MAX]; void Comb_init(){ fact[0] = fact[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for(int i = 2; i < MAX; i++){ fact[i] = fact[i-1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } long long comb(long long n, long long k){ if(n < k) return 0; if(n < 0 || k < 0) return 0; return fact[n] * finv[k] % mod * finv[n - k] % mod; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); //cout << fixed << setprecision(20); cin >> n >> mod; Comb_init(); V<V<ll>> dp(n + 1, V<ll>(n)); auto add = [&](ll &a, ll b)->void{ a += b; if(a >= mod) a -= mod; }; dp[0][0] = 1; for(int v = 0; v < n; v++) for(int e = 0; e <= max(0, v - 1); e++){ for(int nv = 1; nv + v <= n; nv++){ int ne = nv - 1; ll choose = comb(n - v - 1, nv - 1); if(nv >= 2) add(dp[v + nv][e + ne], (dp[v][e] * choose) % mod * modpow(nv, nv - 2, mod) % mod); else add(dp[v + nv][e + ne], dp[v][e]); } } rep(i,n){ cout << dp[n][i] << '\n'; } }