[HEOI2016/TJOI2016]求和

时间:2024-01-13 09:55:32

嘟嘟嘟



好多人(神仙)都说这是NTT例题,然后我就做了……

做这题,需要一下前置技能:

1.第二类斯特林数

2.NTT

3.没有公式恐惧症



额……不会斯特林数的话(就像我),知道通项公式也行。

这个博客挺好:第二类斯特林数总结

然后就是一顿暴推了。



首先如果直接往原式里带通项公式的话好像搞不出来,这时候需要用点技巧,换一下枚举顺序:

\[f(n) = \sum _ {j = 0} ^ {n} 2 ^ j * (j!) \sum _ {i = 0} ^ {n} S(i, j)
\]

讲道理\(i\)应该从\(j\)枚举到\(n\),但因为\(S(i, j) = 0(i < j)\),所以就改成了从\(0\)枚举。

然后代入第二类斯特林数通项公式\(S(n, m) = \frac{1}{m!} \sum _ {k = 0} ^ {m} (-1) ^ k C(m, k) (m - k) ^ n\):

\[\begin{align*}
f(n)
&= \sum_{j = 0} ^ {n} 2 ^ j * (j!) \sum_{i = 0} ^ {n} \sum_{k = 0} ^ {j} (-1) ^ k C(j, k) * (j - k) ^ i \\
&= \sum_{j = 0} ^ {n} 2 ^ j * (j!) \sum_{k = 0} ^ {j} (-1) ^ k C(j, k) * \sum_{i = 0} ^ {n} (j - k) ^ i \\
&= \sum_{j = 0} ^ {n} 2 ^ j * (j!) \sum_{k = 0} ^ {j} \frac{(-1) ^ k}{k!} * \frac{\sum_{i = 0} ^ {n} (j - k) ^ n}{(j - k)!}
\end{align*}\]

发现\(\sum _ {i = 0} ^ {n} (j - k) ^ n\)是一个等比数列,\(O(1)\)可解。

然后令\(A(t) = \frac{(-1) ^ t}{t!}\),\(B(t) = \frac{\sum _ {i = 0} ^ {n} t ^ n}{t!}\)。这俩都可以\(O(n)\)预处理出来。

于是上式变成了

\[f(n) = \sum _ {j = 0} ^ {n} 2 ^ j * (j!) \sum _ {k = 0} ^ {j} A(k) * B(j - k)
\]

后面的\(\sum\)是一个卷积的形式,NTT就行,于是这题就完事了。



(反正自己没推出来)

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const ll mod = 998244353;
const ll G = 3;
const int maxn = 1e5 + 5;
const int maxl = 4e6 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, rev[maxl];
ll fac[maxn], inv_f[maxn], inv[maxn];
ll A[maxl], B[maxl]; In ll quickpow(ll a, ll b)
{
ll ret = 1;
for(; b; b >>= 1, a = a * a % mod)
if(b & 1) ret = ret * a % mod;
return ret;
} In void init()
{
fac[0] = fac[1] = 1;
for(int i = 2; i <= n; ++i) fac[i] = fac[i - 1] * i % mod;
inv_f[n] = quickpow(fac[n], mod - 2);
for(int i = n - 1; i >= 0; --i) inv_f[i] = inv_f[i + 1] * (i + 1) % mod;
inv[0] = inv[1] = 1;
for(int i = 2; i <= n; ++i) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
for(int i = 0, flg = 1; i <= n; ++i, flg *= (-1)) A[i] = (inv_f[i] * flg + mod) % mod;
B[0] = 1; B[1] = n + 1;
for(int i = 2; i <= n; ++i) B[i] = (quickpow(i, n + 1) - 1 + mod) % mod * inv[i - 1] % mod * inv_f[i] % mod;
} In void ntt(ll* a, int len, bool flg)
{
for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
for(int i = 1; i < len; i <<= 1)
{
ll ng = quickpow(G, (mod - 1) / (i << 1));
for(int j = 0; j < len; j += (i << 1))
{
ll g = 1;
for(int k = 0; k < i; ++k, g = g * ng % mod)
{
ll tp1 = a[k + j], tp2 = a[k + j + i] * g % mod;
a[k + j] = (tp1 + tp2) % mod, a[k + j + i] = (tp1 - tp2 + mod) % mod;
}
}
}
if(flg) return;
ll inv = quickpow(len, mod - 2); reverse(a + 1, a + len);
for(int i = 0; i < len; ++i) a[i] = a[i] * inv % mod;
} int main()
{
n = read();
init();
int len = 1, lim = 0;
while(len <= n + n) len <<= 1, ++lim;
for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
ntt(A, len, 1); ntt(B, len, 1);
for(int i = 0; i < len; ++i) A[i] = A[i] * B[i] % mod;
ntt(A, len, 0);
ll ans = 0, tp = 1;
for(int i = 0; i <= n; ++i, tp = (tp + tp) % mod) ans = (ans + fac[i] * tp % mod * A[i] % mod) % mod;
write(ans), enter;
return 0;
}