結果
問題 | No.2747 Permutation Adjacent Sum |
ユーザー | ponjuice |
提出日時 | 2024-04-13 18:29:45 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 455 ms / 3,000 ms |
コード長 | 9,581 bytes |
コンパイル時間 | 1,637 ms |
コンパイル使用メモリ | 111,036 KB |
実行使用メモリ | 30,720 KB |
最終ジャッジ日時 | 2024-10-03 00:19:36 |
合計ジャッジ時間 | 14,433 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 269 ms
17,904 KB |
testcase_01 | AC | 168 ms
13,184 KB |
testcase_02 | AC | 224 ms
14,892 KB |
testcase_03 | AC | 171 ms
13,336 KB |
testcase_04 | AC | 278 ms
18,176 KB |
testcase_05 | AC | 455 ms
30,440 KB |
testcase_06 | AC | 291 ms
18,480 KB |
testcase_07 | AC | 263 ms
17,972 KB |
testcase_08 | AC | 338 ms
19,972 KB |
testcase_09 | AC | 413 ms
26,832 KB |
testcase_10 | AC | 187 ms
13,744 KB |
testcase_11 | AC | 266 ms
17,912 KB |
testcase_12 | AC | 162 ms
12,784 KB |
testcase_13 | AC | 205 ms
14,148 KB |
testcase_14 | AC | 316 ms
18,208 KB |
testcase_15 | AC | 405 ms
26,264 KB |
testcase_16 | AC | 271 ms
18,064 KB |
testcase_17 | AC | 390 ms
24,548 KB |
testcase_18 | AC | 377 ms
23,448 KB |
testcase_19 | AC | 176 ms
13,640 KB |
testcase_20 | AC | 279 ms
18,140 KB |
testcase_21 | AC | 344 ms
20,128 KB |
testcase_22 | AC | 356 ms
21,592 KB |
testcase_23 | AC | 249 ms
17,476 KB |
testcase_24 | AC | 255 ms
17,632 KB |
testcase_25 | AC | 220 ms
14,828 KB |
testcase_26 | AC | 319 ms
18,560 KB |
testcase_27 | AC | 346 ms
20,788 KB |
testcase_28 | AC | 339 ms
19,712 KB |
testcase_29 | AC | 279 ms
18,392 KB |
testcase_30 | AC | 297 ms
30,592 KB |
testcase_31 | AC | 298 ms
30,720 KB |
testcase_32 | AC | 294 ms
30,592 KB |
testcase_33 | AC | 301 ms
30,592 KB |
testcase_34 | AC | 297 ms
30,592 KB |
testcase_35 | AC | 162 ms
12,900 KB |
testcase_36 | AC | 162 ms
12,780 KB |
testcase_37 | AC | 162 ms
12,896 KB |
testcase_38 | AC | 163 ms
12,772 KB |
testcase_39 | AC | 163 ms
12,772 KB |
testcase_40 | AC | 160 ms
12,900 KB |
testcase_41 | AC | 166 ms
12,776 KB |
ソースコード
// O(klogk + √p logp) #include<iostream> #include<vector> #include<cmath> #include<cassert> using namespace std; using ll = long long; template< int mod > struct ModInt { int x; ModInt() : x(0) {} ModInt(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int) (1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(ll n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { ll t; is >> t; a = ModInt< mod >(t); return (is); } static int get_mod() { return mod; } ll get(){ return x; } }; using modint = ModInt<998244353>; template< typename T > struct Combination { vector< T > _fact, _rfact, _inv; Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) { _fact[0] = _rfact[sz] = _inv[0] = 1; for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i; _rfact[sz] /= _fact[sz]; for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1); for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1]; } inline T fact(int k) const { return _fact[k]; } inline T rfact(int k) const { return _rfact[k]; } inline T inv(int k) const { return _inv[k]; } T P(int n, int r) const { if(r < 0 || n < r) return 0; return fact(n) * rfact(n - r); } T C(int p, int q) const { if(q < 0 || p < q) return 0; return fact(p) * rfact(q) * rfact(p - q); } T H(int n, int r) const { if(n < 0 || r < 0) return (0); return r == 0 ? 1 : C(n + r - 1, r); } }; template< typename T > T lagrange_polynomial(const vector< T > &y, ll t) { int N = y.size() - 1; Combination< T > comb(N); if(t <= N) return y[t]; T ret(0); vector< T > dp(N + 1, 1), pd(N + 1, 1); for(int i = 0; i < N; i++) dp[i + 1] = dp[i] * (t - i); for(int i = N; i > 0; i--) pd[i - 1] = pd[i] * (t - i); for(int i = 0; i <= N; i++) { T tmp = y[i] * dp[i] * pd[i] * comb.rfact(i) * comb.rfact(N - i); if((N - i) & 1) ret -= tmp; else ret += tmp; } return ret; } namespace FastFourierTransform { using real = double; struct C { real x, y; C() : x(0), y(0) {} C(real x, real y) : x(x), y(y) {} inline C operator+(const C &c) const { return C(x + c.x, y + c.y); } inline C operator-(const C &c) const { return C(x - c.x, y - c.y); } inline C operator*(const C &c) const { return C(x * c.x - y * c.y, x * c.y + y * c.x); } inline C conj() const { return C(x, -y); } }; const real PI = acosl(-1); int base = 1; vector< C > rts = { {0, 0}, {1, 0} }; vector< int > rev = {0, 1}; void ensure_base(int nbase) { if(nbase <= base) return; rev.resize(1 << nbase); rts.resize(1 << nbase); for(int i = 0; i < (1 << nbase); i++) { rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1)); } while(base < nbase) { real angle = PI * 2.0 / (1 << (base + 1)); for(int i = 1 << (base - 1); i < (1 << base); i++) { rts[i << 1] = rts[i]; real angle_i = angle * (2 * i + 1 - (1 << base)); rts[(i << 1) + 1] = C(cos(angle_i), sin(angle_i)); } ++base; } } void fft(vector< C > &a, int n) { assert((n & (n - 1)) == 0); int zeros = __builtin_ctz(n); ensure_base(zeros); int shift = base - zeros; for(int i = 0; i < n; i++) { if(i < (rev[i] >> shift)) { swap(a[i], a[rev[i] >> shift]); } } for(int k = 1; k < n; k <<= 1) { for(int i = 0; i < n; i += 2 * k) { for(int j = 0; j < k; j++) { C z = a[i + j + k] * rts[j + k]; a[i + j + k] = a[i + j] - z; a[i + j] = a[i + j] + z; } } } } vector< ll > multiply(const vector< int > &a, const vector< int > &b) { int need = (int) a.size() + (int) b.size() - 1; int nbase = 1; while((1 << nbase) < need) nbase++; ensure_base(nbase); int sz = 1 << nbase; vector< C > fa(sz); for(int i = 0; i < sz; i++) { int x = (i < (int) a.size() ? a[i] : 0); int y = (i < (int) b.size() ? b[i] : 0); fa[i] = C(x, y); } fft(fa, sz); C r(0, -0.25 / (sz >> 1)), s(0, 1), t(0.5, 0); for(int i = 0; i <= (sz >> 1); i++) { int j = (sz - i) & (sz - 1); C z = (fa[j] * fa[j] - (fa[i] * fa[i]).conj()) * r; fa[j] = (fa[i] * fa[i] - (fa[j] * fa[j]).conj()) * r; fa[i] = z; } for(int i = 0; i < (sz >> 1); i++) { C A0 = (fa[i] + fa[i + (sz >> 1)]) * t; C A1 = (fa[i] - fa[i + (sz >> 1)]) * t * rts[(sz >> 1) + i]; fa[i] = A0 + A1 * s; } fft(fa, sz >> 1); vector< ll > ret(need); for(int i = 0; i < need; i++) { ret[i] = llround(i & 1 ? fa[i >> 1].y : fa[i >> 1].x); } return ret; } }; template< typename T > struct ArbitraryModConvolution { using real = FastFourierTransform::real; using C = FastFourierTransform::C; ArbitraryModConvolution() = default; vector< T > multiply(const vector< T > &a, const vector< T > &b, int need = -1) { if(need == -1) need = a.size() + b.size() - 1; int nbase = 0; while((1 << nbase) < need) nbase++; FastFourierTransform::ensure_base(nbase); int sz = 1 << nbase; vector< C > fa(sz); for(int i = 0; i < (int)a.size(); i++) { fa[i] = C(a[i].x & ((1 << 15) - 1), a[i].x >> 15); } fft(fa, sz); vector< C > fb(sz); if(a == b) { fb = fa; } else { for(int i = 0; i < (int)b.size(); i++) { fb[i] = C(b[i].x & ((1 << 15) - 1), b[i].x >> 15); } fft(fb, sz); } real ratio = 0.25 / sz; C r2(0, -1), r3(ratio, 0), r4(0, -ratio), r5(0, 1); for(int i = 0; i <= (sz >> 1); i++) { int j = (sz - i) & (sz - 1); C a1 = (fa[i] + fa[j].conj()); C a2 = (fa[i] - fa[j].conj()) * r2; C b1 = (fb[i] + fb[j].conj()) * r3; C b2 = (fb[i] - fb[j].conj()) * r4; if(i != j) { C c1 = (fa[j] + fa[i].conj()); C c2 = (fa[j] - fa[i].conj()) * r2; C d1 = (fb[j] + fb[i].conj()) * r3; C d2 = (fb[j] - fb[i].conj()) * r4; fa[i] = c1 * d1 + c2 * d2 * r5; fb[i] = c1 * d2 + c2 * d1; } fa[j] = a1 * b1 + a2 * b2 * r5; fb[j] = a1 * b2 + a2 * b1; } fft(fa, sz); fft(fb, sz); vector< T > ret(need); for(int i = 0; i < need; i++) { ll aa = llround(fa[i].x); ll bb = llround(fb[i].x); ll cc = llround(fa[i].y); aa = T(aa).x, bb = T(bb).x, cc = T(cc).x; ret[i] = aa + (bb << 15) + (cc << 30); } return ret; } }; template< typename T > T factorial(ll n) { if(n >= T::get_mod()) return 0; ArbitraryModConvolution< T > fft; ll d = 1 << 15; Combination< T > comb(2 * d); vector< T > seq({1, d + 1}); seq.reserve(d + 1); int sz = 1; while(sz < d) { vector< T > aux(sz, 1), f(sz * 4), g(sz * 4); for(int i = 0; i <= sz; i++) { f[i] = comb.rfact(i) * comb.rfact(sz - i) * seq[i]; if(((sz + i) & 1)) f[i] = -f[i]; } vector< T > pf(f), as; as.emplace_back(sz + 1); as.emplace_back(T(sz) / d); as.emplace_back(T(sz) / d + sz + 1); for(int idx = 0; idx < 3; idx++) { for(int i = 0; i < sz * 4; i++) f[i] = pf[i]; for(int i = 1; i < sz * 2 + 2; i++) g[i] = T(1) / (as[idx] - sz + i - 1); f = fft.multiply(f, g); f.resize(sz * 4); T prod = 1; for(int i = 0; i <= sz; i++) prod *= as[idx] - i; for(int i = 0; i <= sz; i++) { f[sz + i + 1] *= prod; prod *= as[idx] + i + 1; prod /= as[idx] - (sz - i); } if(idx == 0) { for(int i = 0; i < sz; i++) aux[i] = f[sz + i + 1]; } else if(idx == 1) { for(int i = 0; i <= sz; i++) seq[i] *= f[sz + i + 1]; } else { for(int i = 0; i < sz; i++) aux[i] *= f[sz + i + 1]; } } for(auto x : aux) seq.emplace_back(x); sz <<= 1; } T res = 1; ll l = min(d, (n + 1) / d); for(ll i = 0; i < l; i++) res *= seq[i]; for(ll i = l * d + 1; i <= n; i++) res *= i; return res; } int main(){ ll n,k; cin >> n >> k; ll siz = k + 3; vector<modint> k1(siz,0 ), k2(siz, 0); for(int i = 1; i < siz; i++) { k1[i] = modint(i).pow(k) + k1[i-1]; k2[i] = modint(i).pow(k+1) + k2[i-1]; } cout << factorial<modint>(n-1) * 2 * (lagrange_polynomial(k1, n) * n - lagrange_polynomial(k2, n)) << endl; }