結果
問題 | No.2877 Gunegune Hyperion |
ユーザー | 👑 potato167 |
提出日時 | 2024-09-06 23:27:49 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 317 ms / 6,000 ms |
コード長 | 9,475 bytes |
コンパイル時間 | 4,282 ms |
コンパイル使用メモリ | 256,512 KB |
実行使用メモリ | 17,100 KB |
最終ジャッジ日時 | 2024-09-06 23:28:35 |
合計ジャッジ時間 | 8,608 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 43 ms
6,944 KB |
testcase_04 | AC | 74 ms
6,940 KB |
testcase_05 | AC | 63 ms
8,444 KB |
testcase_06 | AC | 61 ms
10,688 KB |
testcase_07 | AC | 157 ms
11,476 KB |
testcase_08 | AC | 155 ms
11,152 KB |
testcase_09 | AC | 95 ms
9,140 KB |
testcase_10 | AC | 179 ms
14,632 KB |
testcase_11 | AC | 151 ms
9,552 KB |
testcase_12 | AC | 14 ms
6,940 KB |
testcase_13 | AC | 30 ms
6,940 KB |
testcase_14 | AC | 24 ms
6,940 KB |
testcase_15 | AC | 151 ms
9,316 KB |
testcase_16 | AC | 317 ms
17,100 KB |
testcase_17 | AC | 77 ms
14,852 KB |
testcase_18 | AC | 46 ms
6,948 KB |
testcase_19 | AC | 49 ms
7,256 KB |
testcase_20 | AC | 3 ms
6,940 KB |
testcase_21 | AC | 179 ms
14,712 KB |
testcase_22 | AC | 63 ms
8,716 KB |
testcase_23 | AC | 126 ms
15,552 KB |
testcase_24 | AC | 179 ms
13,412 KB |
testcase_25 | AC | 201 ms
17,020 KB |
testcase_26 | AC | 48 ms
6,940 KB |
testcase_27 | AC | 73 ms
10,304 KB |
testcase_28 | AC | 130 ms
14,336 KB |
testcase_29 | AC | 200 ms
17,024 KB |
testcase_30 | AC | 128 ms
14,456 KB |
testcase_31 | AC | 200 ms
17,020 KB |
testcase_32 | AC | 62 ms
12,852 KB |
ソースコード
/* * [x ^ {a - 1}]f(x) := 最初に真横になるのが a 日目である確率 * [x ^ {a}]g(x) := 真横になった日から次に真横になるまで a 日間かかる確率 * 以上の二つがもとまっているとき、 * 「fg^{N - H} の (N - 1) 次までの係数の和」 * は * 「高さが H 未満になる確率」 * と同じになるので、1 からそれを引くと答えが求まる。 * K = 3 として、 * f, g (mod x ^{N}) は O(NK^2) * fg^{N - H} (mod x^{N}) は log-exp を用いて O(N log(N)) で求まる */ #line 1 "a.cpp" #include <bits/stdc++.h> using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; using ld=long double; const ll ILL=2167167167167167167; const int INF=2100000000; #define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++) #define all(p) p.begin(),p.end() template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>; template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template<class T> bool chmin(T &a,T b){if(a>b){a=b;return 1;}else return 0;} template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;} template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());} template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} bool yneos(bool a,bool upp=0){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;} template<class T> void vec_out(vector<T> &p,int ty=0){ if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";} else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}} template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;} template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;} template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;} int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;} template<class T> bool inside(T l,T x,T r){return l<=x&&x<r;} #include <atcoder/modint> using mint = atcoder::modint998244353; #line 2 "/Users/Shared/po167_library/fps/FPS_pow.hpp" #line 4 "/Users/Shared/po167_library/fps/FPS_pow.hpp" #include <atcoder/convolution> #line 4 "/Users/Shared/po167_library/fps/FPS_cyclic_convolution.hpp" namespace po167{ // |f| = |g| = 2 ^ n template<class T> std::vector<T> FPS_cyclic_convolution(std::vector<T> f, std::vector<T> g){ atcoder::internal::butterfly(f); atcoder::internal::butterfly(g); for (int i = 0; i < (int)f.size(); i++) f[i] *= g[i]; atcoder::internal::butterfly_inv(f); T iz = (T)(1) / (T)(f.size()); for (int i = 0; i < (int)f.size(); i++) f[i] *= iz; return f; } } #line 3 "/Users/Shared/po167_library/fps/FPS_differetial.hpp" namespace po167{ // return f' template <class T> std::vector<T> FPS_differential(std::vector<T> f){ if (f.empty()) return f; for (int i = 0; i < (int)f.size() - 1; i++){ f[i] = f[i + 1] * (T)(i + 1); } f.pop_back(); return f; } } #line 3 "/Users/Shared/po167_library/fps/FPS_integral.hpp" namespace po167{ template <class T> std::vector<T> FPS_integral(std::vector<T> f){ if (f.empty()) return f; std::vector<T> num_inv((int)f.size() + 1); num_inv[0] = 1; num_inv[1] = 1; auto m = T::mod(); for (int i = 2; i <= (int)f.size(); i++){ num_inv[i] = (0 - num_inv[m % i]) * (T)(m / i); } f.reserve((int)f.size() + 1); f.push_back(0); for (int i = (int)f.size() - 1; i > 0; i--){ f[i] = f[i - 1] * num_inv[i]; } f[0] = 0; return f; } } #line 4 "/Users/Shared/po167_library/fps/FPS_inv.hpp" namespace po167{ // return 1 / f template <class T> std::vector<T> FPS_inv(std::vector<T> f, int len = -1){ if (len == -1) len = f.size(); assert(f[0] != 0); std::vector<T> g = {1 / f[0]}; int s = 1; while(s < len){ // g = 2g_s - f(g_s)^2 (mod x ^ (2 * s)) // g = g - (fg - 1)g // (fg - 1) = 0 (mod x ^ (s)) std::vector<T> n_g(s * 2, 0); std::vector<T> f_s(s * 2, 0); g.resize(s * 2); for (int i = 0; i < s * 2; i++){ if (int(f.size()) > i) f_s[i] = f[i]; n_g[i] = g[i]; } atcoder::internal::butterfly(g); atcoder::internal::butterfly(f_s); for (int i = 0; i < s * 2; i++){ f_s[i] *= g[i]; } atcoder::internal::butterfly_inv(f_s); T iz = 1 / (T)(s * 2); for (int i = s; i < s * 2; i++){ f_s[i] *= iz; } for (int i = 0; i < s; i++){ f_s[i] = 0; } atcoder::internal::butterfly(f_s); for (int i = 0; i < s * 2; i++){ f_s[i] *= g[i]; } atcoder::internal::butterfly_inv(f_s); for (int i = s; i < s * 2; i++){ n_g[i] -= f_s[i] * iz; } std::swap(n_g, g); s *= 2; } g.resize(len); return g; } } #line 8 "/Users/Shared/po167_library/fps/FPS_exp.hpp" namespace po167{ template<class T> std::vector<T> FPS_exp(std::vector<T> f, int len = -1){ if (len == -1) len = f.size(); if (len == 0) return {}; if (len == 1) return {T(1)}; assert(!f.empty() && f[0] == 0); int s = 1; // simple std::vector<T> g = {T(1)}; while (s < len){ // g' / g // A * B std::vector<T> A = g, B = g; A = FPS_differential(A); B = FPS_inv(B, 2 * s); A.resize(2 * s); A = FPS_cyclic_convolution(A, B); A.pop_back(); A = FPS_integral(A); for (int i = 0; i < s; i++) A[i] = 0; for (int i = s; i < s * 2; i++) A[i] = (i < (int)f.size() ? f[i] : 0) - A[i]; // g_hat = g (1 - g + f) // g += B = g * A g.resize(2 * s); B = FPS_cyclic_convolution(A, g); for (int i = s; i < s * 2; i++) g[i] = B[i]; s *= 2; } g.resize(len); return g; } } #line 5 "/Users/Shared/po167_library/fps/FPS_log.hpp" namespace po167{ template<class T> std::vector<T> FPS_log(std::vector<T> f, int len = -1){ if (len == -1) len = f.size(); if (len == 0) return {}; if (len == 1) return {T(0)}; assert(!f.empty() && f[0] == 1); std::vector<T> res = atcoder::convolution(FPS_differential(f), FPS_inv(f, len)); res.resize(len - 1); return FPS_integral(res); } } #line 8 "/Users/Shared/po167_library/fps/FPS_pow.hpp" namespace po167{ template<class T> std::vector<T> FPS_pow(std::vector<T> f,long long M, int len = -1){ if (len == -1) len = f.size(); std::vector<T> res(len, 0); if (M == 0){ res[0] = 1; return res; } for (int i = 0; i < (int)f.size(); i++){ if (f[i] == 0) continue; if (i > (len - 1) / M) break; std::vector<T> g((int)f.size() - i); T v = (T)(1) / (T)(f[i]); for (int j = i; j < (int)f.size(); j++){ g[j - i] = f[j] * v; } long long zero = i * M; if (i) len -= i * M; g = FPS_log(g, len); for (T &x : g) x *= M; g = FPS_exp(g, len); v = (T)(1) / v; T c = 1; while (M){ if (M & 1) c = c * v; v = v * v; M >>= 1; } for (int i = 0; i < len; i++) res[i + zero] = g[i] * c; return res; } return res; } } #line 32 "a.cpp" void solve(); // CYAN / FREDERIC int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; rep(i, 0, t) solve(); } void solve(){ ll N, H; cin >> N >> H; vector base(2, vector<mint>(N)); vector<mint> inv(6); rep(i, 1, 6) inv[i] = (mint)(1) / (mint)(i); vector to(3, vector<mint>(3)); to[0][0] = inv[2]; to[0][1] = inv[2]; to[1][0] = inv[3]; to[1][1] = inv[3]; to[1][2] = inv[3]; to[2][1] = inv[3] * 2; to[2][2] = inv[3]; rep(i, 0, 2){ vector<mint> dp(3); if (i == 0) dp[0] = 1; else dp[1] = 2, dp[2] = 1; rep(j, 0, N - 1){ vector<mint> n_dp(3); rep(n, 0, 3) rep(m, 0, 3) n_dp[m] += dp[n] * to[n][m]; swap(n_dp, dp); for (int k = 0; k < 3; k += 3){ base[i][j + 1] += dp[k]; dp[k] = 0; } } } vector<mint> f(N), g = base[0]; rep(i, 0, 2){ if (i == 0) f[0] += inv[5] * 2; else rep(j, 0, N) f[j] += base[i][j] * inv[5]; } auto gp = po167::FPS_pow(g, N - H); f = atcoder::convolution(f, gp); f.resize(N); cout << (1 - vec_sum(f)).val() << "\n"; }