第二类斯特林数·行
题目链接:luogu P5395
题目大意
第二类斯特林数是把 n 个不同元素放入 m 个相同的集合中,保证每个集合非空的方案数。
给你 n,对于 0~n 的每个 m 都求第二类斯特林数。
思路
考虑第二类斯特林数的性质,就是 \(n\) 个不同元素放入 \(m\) 个相同集合。
考虑通过它的容斥来列出一个式子。
那我们首先考虑吧把集合变成不同的,乘上 \(m!\)
然后考虑枚举多少个不是空的:
\(m!S(n,m)=\sum\limits_{i=0}^m(-1)^{m-i}C(m,i)i^n\)
(\(i^n\) 是每个元素放入哪个桶)
化简式子:
\(m!S(n,m)=\sum\limits_{i=0}^m(-1)^{m-i}\dfrac{m!}{i!(m-i)!}i^n\)
\(m!S(n,m)=m!\sum\limits_{i=0}^m\dfrac{(-1)^{m-i}}{(m-i)!}\dfrac{i^n}{i!}\)
\(S(n,m)=\sum\limits_{i=0}^m\dfrac{(-1)^{m-i}}{(m-i)!}\dfrac{i^n}{i!}\)
那这就是一个卷积的形式,我们直接上 NTT 即可。
(它这个模数是可以 NTT 的)
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mo 167772161
#define ll long long
#define clr(f, x) memset(f, 0, sizeof(int) * (x))
#define cpy(f, g, x) memcpy(f, g, sizeof(int) * (x))
using namespace std;
const int N = 5e5 + 100;
const int pN = N * 8;
int n, jc[N], inv[N], invs[N], S[N];
int f[pN], g[pN];
int add(int x, int y) {return x + y >= mo ? x + y - mo : x + y;}
int dec(int x, int y) {return x < y ? x - y + mo : x - y;}
int mul(int x, int y) {return 1ll * x * y % mo;}
int ksm(int x, ll y) {
int re = 1;
while (y) {
if (y & 1) re = mul(re, x);
x = mul(x, x); y >>= 1;
}
return re;
}
int C(int n, int m) {
if (n < 0 || m < 0 || n < m) return 0;
return mul(mul(jc[n], invs[m]), invs[n - m]);
}
struct Poly {
int an[pN], G = 3, Gv;
void Init() {
jc[0] = 1; for (int i = 1; i < N; i++) jc[i] = mul(jc[i - 1], i);
inv[0] = inv[1] = 1; for (int i = 2; i < N; i++) inv[i] = mul(inv[mo % i], mo - mo / i);
invs[0] = 1; for (int i = 1; i < N; i++) invs[i] = mul(invs[i - 1], inv[i]);
Gv = ksm(G, mo - 2);
}
void get_an(int limit, int l_size) {
for (int i = 0; i < limit; i++)
an[i] = (an[i >> 1] >> 1) | ((i & 1) << (l_size - 1));
}
void NTT(int *f, int limit, int op) {
for (int i = 0; i < limit; i++)
if (i < an[i]) swap(f[i], f[an[i]]);
for (int mid = 1; mid < limit; mid <<= 1) {
int Wn = ksm(op == 1 ? G : Gv, (mo - 1) / (mid << 1));
for (int j = 0, R = (mid << 1); j < limit; j += R)
for (int w = 1, k = 0; k < mid; k++, w = mul(w, Wn)) {
int x = f[j | k], y = mul(w, f[j | mid | k]);
f[j | k] = add(x, y); f[j | mid | k] = dec(x, y);
}
}
if (op == -1) {
int limv = ksm(limit, mo - 2);
for (int i = 0; i < limit; i++) f[i] = mul(f[i], limv);
}
}
void px(int *f, int *g, int limit) {
for (int i = 0; i < limit; i++)
f[i] = mul(f[i], g[i]);
}
void times(int *f, int *g, int n, int m, int T) {
int limit = 1, l_size = 0; while (limit < n + m) limit <<= 1, l_size++;
get_an(limit, l_size);
static int tmp[pN]; clr(f + n, limit - n); cpy(tmp, g, m); clr(tmp + m, limit - m);
NTT(f, limit, 1); NTT(tmp, limit, 1); px(f, tmp, limit); NTT(f, limit, -1);
clr(f + T, limit - 1); clr(tmp, limit);
}
}P;
void getS() {
for (int i = 0; i <= n; i++) {
f[i] = mul((i & 1) ? mo - 1 : 1, invs[i]);
g[i] = mul(ksm(i, n), invs[i]);
}
P.times(f, g, n + 1, n + 1, n + 1);
for (int i = 0; i <= n; i++)
S[i] = f[i];
}
int main() {
P.Init();
scanf("%d %d", &n);
getS();
for (int i = 0; i <= n; i++) printf("%d ", S[i]);
return 0;
}
标签:invs,return,int,luogu,mo,容斥,NTT,mul,inv
From: https://www.cnblogs.com/Sakura-TJH/p/luogu_P5395.html