Solution -「洛谷 P4389」付公主的背包
\(\mathcal{Description}\)
Link.
容量为 \(n\),\(m\) 种物品的无限背包,求凑出每种容量的方案数,对 \(998244353\) 取模。
\(n,m\le10^5\)。
\(\mathcal{Solution}\)
感觉货币系统是这道题的弱化版 qwq。
还有这个博客园对齐公式自动编号的 feature 怎么去掉啊……
对于大小为 \(v\) 的物品,有生成函数:
\[G(x)=\sum_{i=0}^{+\infty}x^{iv}
\]
设物品 \(i\) 的生成函数为 \(G_i(x)\),则需要求:
\[\prod_{i=1}^nG_i(x)
\]
这个算不动,推一下式子:
\[\begin{align}\prod_{i=1}^nG_i(x)&=\exp\ln\prod_{i=1}^nG_i(x)\\&=\exp\sum_{i=1}^n\ln G_i(x)\end{align}
\]
\(\ln G(x)\) 拿出来:
\[\begin{align}\ln G(x)&=\ln\sum_{i=0}^{+\infty}x^{iv}\\&=\ln\frac{1}{1-x^v}\\&=-\ln(1-x^v)\\&=-\int\frac{d(1-x^v)}{1-x^v}\\&=\int\frac{vx^{v-1}}{1-x^v}dx\end{align}
\]
拿出积分里面的式子:
\[\begin{align}\frac{vx^{v-1}}{1-x^v}&=vx^{v-1}\sum_{i=0}^{+\infty}x^{iv}\\&=v\sum_{i=0}^{+\infty}x^{v(i+1)-1}\end{align}
\]
积分最后这项:
\[\begin{align}\int\frac{vx^{v-1}}{1-x^v}dx&=\int v\sum_{i=0}^{+\infty}x^{v(i+1)-1}dx\\&=v\sum_{i=0}^{+\infty}\frac{x^{v(i+1)}}{v(i+1)}\\&=\sum_{i=1}^{+\infty}\frac{x^{iv}}i\end{align}
\]
于是要求:
\[\exp\sum_{i=1}^n\sum_{j=1}^{+\infty}\frac{x^{jv_i}}j\bmod x^{m+1}
\]
先把 \(\exp\) 内的式子写出来——枚举 \(v\) 的值域,计算 \(v_i=v\) 时的贡献,最后 \(\exp\) 一下就好啦。
复杂度 \(\mathcal O(n+m\log m)\)。
\(\mathcal{Code}\)
#include <cmath>
#include <cstdio>
typedef long long LL;
const int MAXN = 1 << 18, MOD = 998244353;
int n, m, inv[MAXN + 5], buc[MAXN + 5], F[MAXN + 5], G[MAXN + 5];
inline int add ( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int sub ( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int mul ( LL a, const int b ) { return ( a *= b ) < MOD ? a : a % MOD; }
inline int rint () {
int x = 0; char s = getchar ();
for ( ; s < '0' || '9' < s; s = getchar () );
for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
return x;
}
inline int qkpow ( int a, int b ) {
int ret = 1;
for ( ; b; a = mul ( a, a ), b >>= 1 ) ret = mul ( ret, b & 1 ? a : 1 );
return ret;
}
namespace Poly {
const int G = 3;
inline int adjust ( const int n ) {
int ret = 0;
for ( int l = 1; l < n; l <<= 1, ++ ret );
return ret;
}
inline void NTT ( const int n, int* A, const int tp ) {
static int lstn = -1, rev[MAXN + 5] {};
if ( lstn ^ n ) {
int lgn = log ( n ) / log ( 2 ) + 0.5;
for ( int i = 0; i < n; ++ i ) rev[i] = ( rev[i >> 1] >> 1 ) | ( ( i & 1 ) << lgn >> 1 );
lstn = n;
}
for ( int i = 0; i < n; ++ i ) if ( i < rev[i] ) A[i] ^= A[rev[i]] ^= A[i] ^= A[rev[i]];
for ( int i = 2, stp = 1; i <= n; i <<= 1, stp <<= 1 ) {
int w = qkpow ( G, ( MOD - 1 ) / i );
if ( ! ~ tp ) w = qkpow ( w, MOD - 2 );
for ( int j = 0; j < n; j += i ) {
for ( int k = j, r = 1; k < j + stp; ++ k, r = mul ( r, w ) ) {
int ev = A[k], ov = mul ( r, A[k + stp] );
A[k] = add ( ev, ov ), A[k + stp] = sub ( ev, ov );
}
}
}
if ( ! ~ tp ) for ( int i = 0; i < n; ++ i ) A[i] = mul ( A[i], inv[n] );
}
inline void polyDer ( const int n, const int* A, int* R ) {
for ( int i = 1; i < n; ++ i ) R[i - 1] = mul ( i, A[i] );
R[n - 1] = 0;
}
inline void polyInt ( const int n, const int* A, int* R ) {
for ( int i = n - 1; ~ i; -- i ) R[i + 1] = mul ( inv[i + 1], A[i] );
R[0] = 0;
}
inline void polyInv ( const int n, const int* A, int* R ) {
static int tmp[MAXN + 5] {};
if ( n == 1 ) return void ( R[0] = qkpow ( A[0], MOD - 2 ) );
int len = 1 << adjust ( n << 1 );
polyInv ( n + 1 >> 1, A, R );
for ( int i = 0; i < n; ++ i ) tmp[i] = A[i];
NTT ( len, tmp, 1 ), NTT ( len, R, 1 );
for ( int i = 0; i < len; ++ i ) R[i] = mul ( sub ( 2, mul ( tmp[i], R[i] ) ), R[i] ), tmp[i] = 0;
NTT ( len, R, -1 );
for ( int i = n; i < len; ++ i ) R[i] = 0;
}
inline void polyLn ( const int n, const int* A, int* R ) {
static int tmp[2][MAXN + 5] {};
int len = 1 << adjust ( n << 1 );
polyDer ( n, A, tmp[0] ), polyInv ( n, A, tmp[1] );
NTT ( len, tmp[0], 1 ), NTT ( len, tmp[1], 1 );
for ( int i = 0; i < len; ++ i ) tmp[0][i] = mul ( tmp[0][i], tmp[1][i] );
NTT ( len, tmp[0], -1 ), polyInt ( n, tmp[0], R );
for ( int i = 0; i < len; ++ i ) tmp[0][i] = tmp[1][i] = 0;
for ( int i = n; i < len; ++ i ) R[i] = 0;
}
inline void polyExp ( const int n, const int* A, int* R ) {
static int tmp[MAXN + 5] {};
if ( n == 1 ) return void ( R[0] = 1 );
int len = 1 << adjust ( n << 1 );
polyExp ( n + 1 >> 1, A, R ), polyLn ( n, R, tmp );
tmp[0] = sub ( A[0] + 1, tmp[0] );
for ( int i = 1; i < n; ++ i ) tmp[i] = sub ( A[i], tmp[i] );
NTT ( len, tmp, 1 ), NTT ( len, R, 1 );
for ( int i = 0; i < len; ++ i ) R[i] = mul ( R[i], tmp[i] ), tmp[i] = 0;
NTT ( len, R, -1 );
for ( int i = n; i < len; ++ i ) R[i] = 0;
}
} // namespace Poly.
int main () {
inv[1] = 1;
for ( int i = 2; i <= MAXN; ++ i ) inv[i] = mul ( MOD - MOD / i, inv[MOD % i] );
n = rint (), m = rint ();
for ( int i = 1; i <= n; ++ i ) ++ buc[rint ()];
for ( int i = 1; i <= m; ++ i ) {
if ( ! buc[i] ) continue;
for ( int j = 1, lim = m / i, t; j <= lim; ++ j ) {
t = i * j;
F[t] = add ( F[t], mul ( inv[j], buc[i] ) );
}
buc[i] = 0;
}
Poly::polyExp ( m + 1, F, G );
for ( int i = 1; i <= m; ++ i ) printf ( "%d\n", G[i] );
return 0;
}