結果
問題 | No.502 階乗を計算するだけ |
ユーザー | shobonvip |
提出日時 | 2023-06-11 04:45:21 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,320 bytes |
コンパイル時間 | 7,477 ms |
コンパイル使用メモリ | 333,684 KB |
実行使用メモリ | 13,668 KB |
最終ジャッジ日時 | 2024-06-11 00:48:53 |
合計ジャッジ時間 | 40,744 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 591 ms
12,528 KB |
testcase_01 | AC | 587 ms
12,648 KB |
testcase_02 | AC | 577 ms
12,400 KB |
testcase_03 | AC | 586 ms
12,400 KB |
testcase_04 | AC | 585 ms
12,396 KB |
testcase_05 | AC | 585 ms
12,392 KB |
testcase_06 | AC | 575 ms
12,524 KB |
testcase_07 | AC | 583 ms
12,396 KB |
testcase_08 | AC | 582 ms
12,652 KB |
testcase_09 | AC | 588 ms
12,520 KB |
testcase_10 | AC | 585 ms
12,524 KB |
testcase_11 | AC | 575 ms
12,396 KB |
testcase_12 | AC | 586 ms
12,336 KB |
testcase_13 | AC | 585 ms
12,652 KB |
testcase_14 | AC | 590 ms
12,396 KB |
testcase_15 | AC | 584 ms
12,520 KB |
testcase_16 | AC | 577 ms
12,528 KB |
testcase_17 | AC | 587 ms
12,396 KB |
testcase_18 | AC | 587 ms
12,520 KB |
testcase_19 | AC | 586 ms
12,396 KB |
testcase_20 | AC | 578 ms
12,396 KB |
testcase_21 | AC | 576 ms
12,524 KB |
testcase_22 | AC | 597 ms
12,524 KB |
testcase_23 | AC | 580 ms
12,396 KB |
testcase_24 | AC | 620 ms
12,400 KB |
testcase_25 | AC | 620 ms
12,396 KB |
testcase_26 | AC | 617 ms
12,400 KB |
testcase_27 | AC | 614 ms
12,400 KB |
testcase_28 | AC | 593 ms
12,396 KB |
testcase_29 | AC | 608 ms
12,448 KB |
testcase_30 | AC | 681 ms
12,520 KB |
testcase_31 | AC | 592 ms
12,396 KB |
testcase_32 | TLE | - |
testcase_33 | TLE | - |
testcase_34 | AC | 955 ms
12,484 KB |
testcase_35 | AC | 756 ms
12,136 KB |
testcase_36 | TLE | - |
testcase_37 | TLE | - |
testcase_38 | TLE | - |
testcase_39 | TLE | - |
testcase_40 | AC | 666 ms
12,392 KB |
testcase_41 | TLE | - |
testcase_42 | AC | 2 ms
5,376 KB |
testcase_43 | AC | 2 ms
5,376 KB |
testcase_44 | AC | 2 ms
5,376 KB |
testcase_45 | AC | 2 ms
5,376 KB |
testcase_46 | AC | 1 ms
5,376 KB |
testcase_47 | AC | 2 ms
5,376 KB |
testcase_48 | AC | 2 ms
5,376 KB |
testcase_49 | AC | 2 ms
5,376 KB |
testcase_50 | AC | 2 ms
5,376 KB |
testcase_51 | AC | 1 ms
5,376 KB |
ソースコード
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include<atcoder/all> using namespace std; using namespace atcoder; typedef modint1000000007 mint; typedef long long ll; mint Garner(vector<long long> m,vector<long long> r){ int n = m.size(); vector<long long> t(n); for(int i=0; i<n; i++){ long long cur = 0LL; long long cm = 1LL; for(int j=0; j<i; j++){ cur += t[j] * cm; cur %= m[i]; cm *= m[j]; cm %= m[i]; } cur = r[i] - cur; cur %= m[i]; if(cur<0)cur += m[i]; if(i==1)cur *= 416537774; if(i==2)cur *= 429847628; //cur *= inv_mod(cm,m[i]); cur %= m[i]; t[i] = cur; } mint ret = 0; mint cm = 1; for(int i=0; i<n; i++){ ret += cm * t[i]; cm *= m[i]; } return ret; } template <class T> vector<T> convolution_mint(vector<T> a,vector<T> b){ static constexpr unsigned long long M0 = 998244353; static constexpr unsigned long long M1 = 754974721; static constexpr unsigned long long M2 = 469762049; vector<long long> aa(a.size()),bb(b.size()); for(int i=0; i<a.size(); i++)aa[i] = a[i].val(); for(int i=0; i<b.size(); i++)bb[i] = b[i].val(); auto c0 = convolution<M0>(aa,bb); auto c1 = convolution<M1>(aa,bb); auto c2 = convolution<M2>(aa,bb); vector<mint> ret(c0.size()); vector<long long> m = {M0,M1,M2}; for(int i=0; i<c0.size(); i++){ vector<long long> r = {c0[i],c1[i],c2[i]}; vector<long long> t(3); for(int j=0; j<3; j++){ long long cur = 0LL; long long cm = 1LL; for(int k=0; k<j; k++){ cur += t[k] * cm; cur %= m[j]; cm *= m[k]; cm %= m[j]; } cur = r[j] - cur; cur %= m[j]; if(cur<0)cur += m[j]; if(j==1)cur *= 416537774; if(j==2)cur *= 429847628; cur %= m[j]; t[j] = cur; } mint cm = 1; for(int j=0; j<3; j++){ ret[i] += cm * t[j]; cm *= m[j]; } } return ret; } vector<mint> poly_inv(vector<mint> &a, int M = -314159265){ if (M == -314159265) M = (int)a.size(); else if (M <= 0) return {}; int n = a.size(); mint r = a[0].pow(mint::mod()-2); int m = 1; vector<mint> res = {r}; while (m < M){ vector<mint> f = a; f.resize(min(n, 2*m)); vector<mint> h = convolution_mint(f, res); for (int i=0; i<min(2*m, (int)h.size()); i++){ h[i] = -h[i]; } h[0] += 2; h.resize(2*m); h = convolution_mint(h, res); h.resize(2*m); swap(res, h); m <<= 1; } res.resize(M); return res; } int ceil_pow2(int n) { int x = 0; while ((1U << x) < (unsigned int)(n)) x++; return x; } vector<mint> multi_eval(vector<mint> x, vector<mint> a){ int n = x.size(); int siz = 1 << ceil_pow2(n); vector<vector<mint>> g(2*siz, vector<mint>{1}); for (int i=0; i<n; i++) g[i + siz] = {-x[i], 1}; for (int i=siz-1; i>0; i--) g[i] = convolution_mint(g[2*i], g[2*i+1]); vector<mint> f; for (int i=1; i<2*siz; i++){ if (i==1) f = a; else f = g[i>>1]; int fs = f.size(), gs = g[i].size(); int m = fs - gs + 1; vector<mint> v = {}, w = {}; if (m > 0){ vector<mint> ft(m); for (int j=0; j<m; j++) ft[j] = f[fs-1-j]; vector<mint> gt(gs); for (int j=0; j<gs; j++) gt[j] = g[i][gs-1-j]; v = convolution_mint(ft, poly_inv(gt, m)); v.resize(m); reverse(v.begin(), v.end()); w = convolution_mint(v, g[i]); } g[i] = f; for (int j=0; j<w.size(); j++){ g[i][j] -= w[j]; } while (g[i].size() > 1 && g[i][g[i].size() - 1] == 0){ g[i].pop_back(); } } vector<mint> ret(n); for (int i=0; i<n; i++){ ret[i] = g[i+siz][0]; } return ret; } int main(){ ll n; cin >> n; if (n >= mint::mod()){ cout << 0 << endl; return 0; } clock_t start = clock(); int m = 61200; deque<vector<mint>> que; for (int i=0; i<m; i++){ que.push_back({i+1, 1}); } while(que.size() > 1){ vector<mint> f = que.front(); que.pop_front(); vector<mint> g = que.front(); que.pop_front(); que.push_back(convolution_mint(f, g)); } vector<mint> f = que.front(); vector<mint> g(n/m); for (int i=0; i<n/m; i++){ g[i] = i*m; } vector<mint> muleval = multi_eval(g, f); mint ans = 1; for (int i=m*(n/m)+1; i<=n; i++){ ans *= i; } for (int i=0; i<n/m; i++){ ans *= muleval[i]; } cout << ans.val() << endl; clock_t end = clock(); // 終了時間 //std::cout << "duration = " << (double)(end - start) / CLOCKS_PER_SEC << "sec.\n"; }