
题目链接:洛谷
题目大意:现在有$n$个物品,每种物品体积为$v_i$,对任意$s\in [1,m]$,求背包恰好装$s$体积的方案数(完全背包问题)。
数据范围:$n,m\leq 10^5$
这道题,看到数据范围就知道是生成函数。
$$Ans=\prod_{i=1}^n\frac{1}{1-x^{v_i}}$$
但是这个式子直接乘会tle,我们考虑进行优化。
看见这个连乘的式子,应该是要上$\ln$.
$$Ans=\exp(\sum_{i=1}^n\ln(\frac{1}{1-x^{v_i}}))$$
接下来的问题就是如何快速计算$\ln(\frac{1}{1-x^{v_i}})$。
$$\ln(f(x))=\int f'f^{-1}dx$$
所以
$$\ln(\frac{1}{1-x^v})=\int\sum_{i=1}^{+\infty}vix^{vi-1}*(1-x^v)dx$$
$$=\int(\sum_{i=1}^{+\infty}vix^{vi-1}-\sum_{i=2}^{+\infty}v(i-1)x^{vi-1})dx$$
$$=\int(\sum_{i=1}^{+\infty}vx^{vi-1})dx$$
$$=\sum_{i=1}^{+\infty}\frac{1}{i}x^{vi}$$
然后就可以直接代公式了。
#include<cstdio>
#include<algorithm>
#define Rint register int
using namespace std;
typedef long long LL;
const int N = , P = , G = , Gi = ;
int n, m, cnt[N], A[N];
inline int kasumi(int a, int b){
int res = ;
while(b){
if(b & ) res = (LL) res * a % P;
a = (LL) a * a % P;
b >>= ;
}
return res;
}
int R[N];
inline void NTT(int *A, int limit, int type){
for(Rint i = ;i < limit;i ++)
if(i < R[i]) swap(A[i], A[R[i]]);
for(Rint mid = ;mid < limit;mid <<= ){
int Wn = kasumi(type == ? G : Gi, (P - ) / (mid << ));
for(Rint j = ;j < limit;j += mid << ){
int w = ;
for(Rint k = ;k < mid;k ++, w = (LL) w * Wn % P){
int x = A[j + k], y = (LL) w * A[j + k + mid] % P;
A[j + k] = (x + y) % P;
A[j + k + mid] = (x - y + P) % P;
}
}
}
if(type == -){
int inv = kasumi(limit, P - );
for(Rint i = ;i < limit;i ++)
A[i] = (LL) A[i] * inv % P;
}
}
int ans[N];
inline void poly_inv(int *A, int deg){
static int tmp[N];
if(deg == ){
ans[] = kasumi(A[], P - );
return;
}
poly_inv(A, (deg + ) >> );
int limit = , L = -;
while(limit <= (deg << )){limit <<= ; L ++;}
for(Rint i = ;i < limit;i ++)
R[i] = (R[i >> ] >> ) | ((i & ) << L);
for(Rint i = ;i < deg;i ++) tmp[i] = A[i];
for(Rint i = deg;i < limit;i ++) tmp[i] = ;
NTT(tmp, limit, ); NTT(ans, limit, );
for(Rint i = ;i < limit;i ++)
ans[i] = ( - (LL) tmp[i] * ans[i] % P + P) % P * ans[i] % P;
NTT(ans, limit, -);
for(Rint i = deg;i < limit;i ++) ans[i] = ;
}
int Ln[N];
inline void get_Ln(int *A, int deg){
static int tmp[N];
poly_inv(A, deg);
for(Rint i = ;i < deg;i ++)
tmp[i - ] = (LL) i * A[i] % P;
tmp[deg - ] = ;
int limit = , L = -;
while(limit <= (deg << )){limit <<= ; L ++;}
for(Rint i = ;i < limit;i ++)
R[i] = (R[i >> ] >> ) | ((i & ) << L);
NTT(ans, limit, ); NTT(tmp, limit, );
for(Rint i = ;i < limit;i ++) Ln[i] = (LL) ans[i] * tmp[i] % P;
NTT(Ln, limit, -);
for(Rint i = deg + ;i < limit;i ++) Ln[i] = ;
for(Rint i = deg;i;i --) Ln[i] = (LL) Ln[i - ] * kasumi(i, P - ) % P;
for(Rint i = ;i < limit;i ++) tmp[i] = ans[i] = ;
Ln[] = ;
}
int Exp[N];
inline void get_Exp(int *A, int deg){
if(deg == ){
Exp[] = ;
return;
}
get_Exp(A, (deg + ) >> );
get_Ln(Exp, deg);
for(Rint i = ;i < deg;i ++) Ln[i] = (A[i] + (i == ) - Ln[i] + P) % P;
int limit = , L = -;
while(limit <= (deg << )){limit <<= ; L ++;}
for(Rint i = ;i < limit;i ++)
R[i] = (R[i >> ] >> ) | ((i & ) << L);
NTT(Exp, limit, ); NTT(Ln, limit, );
for(Rint i = ;i < limit;i ++) Exp[i] = (LL) Exp[i] * Ln[i] % P;
NTT(Exp, limit, -);
for(Rint i = deg;i < limit;i ++) Exp[i] = ;
for(Rint i = ;i < limit;i ++) Ln[i] = ans[i] = ;
}
int main(){
scanf("%d%d", &n, &m);
for(Rint i = ;i <= n;i ++){
int x;
scanf("%d", &x);
++ cnt[x];
}
for(Rint i = ;i <= m;i ++){
if(!cnt[i]) continue;
for(Rint j = i;j <= m;j += i)
A[j] = (A[j] + (LL) cnt[i] * kasumi(j / i, P - ) % P) % P;
}
get_Exp(A, m + );
for(Rint i = ;i <= m;i ++)
printf("%d\n", Exp[i]);
}
luogu4389