Luogu 4491 [HAOI2018]染色

时间:2023-03-09 07:43:54
Luogu 4491 [HAOI2018]染色

BZOJ 5306

考虑计算恰好出现$s$次的颜色有$k$种的方案数。

首先可以设$lim = min(m, \left \lfloor \frac{n}{s} \right \rfloor)$,我们在计算的时候只要算到这个$lim$就可以了。

设$f(k)$表示出现$s$次的颜色至少有$k$种的方案数,则

$$f(k) = \binom{m}{k}\binom{n}{ks}\frac{(ks)!}{(s!)^k}(m - k)^{n - ks}$$

就是先选出$k$个颜色和$ks$个格子放这些颜色,这样子总的方案数是全排列除以限排列,剩下的颜色随便放。

处理一下组合数,整个$f$可以在$O(nlogn)$时间内算出来。

设$g(k)$表示出现$s$次的颜色刚好有$k$种的方案数,考虑到对于$\forall i < j$,$g(j)$在$f(i)$中被计算了$\binom{j}{i}$次,所以有

$$f(k) = \sum_{i = k}^{lim}\binom{i}{k}g(i)$$

直接二项式反演回来,

$$g(k) = \sum_{i = k}^{lim}(-1)^{i - k}\binom{i}{k}f(i)$$

拆开组合数,

$$g(k) = \sum_{i = k}^{lim}(-1)^{i - k}\frac{i!}{k!(i - k)!}f(i) = \frac{1}{k!}\sum_{i = k}^{lim}\frac{(-1)^{i - k}}{(i - k)!}(f(i) * (i!))$$

设$A(i) = f(i) * (i!)$,$B(i) = \frac{(-1)^{i}}{i!}$

$$g(k)* (k!) = \sum_{i = k}^{lim}A(i)B(i - k)$$

咕,并不是卷积。

把$B$翻转,再设$B'(i) = B(lim - i)$

$$g(k)* (k!) = \sum_{i = k}^{lim}A(i)B'(lim + k - i)$$

注意到$A*B'$的第$lim + k$项的系数是$\sum_{i = 0}^{lim + k}A(i)B'(lim + k - i)$,但是需要满足

$$ 0 \leq i \leq lim$$

$$ 0 \leq lim + k - i \leq lim $$

$$k \leq i \leq lim$$

刚好满足。

时间复杂度$O(n + mlogm)$。

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector <ll> poly; const int N = 1e7 + ;
const int M = 1e5 + ; int n, m, s;
ll w[M], fac[N], ifac[N], f[M], g[M], sum[M]; inline void deb(poly c) {
for (int i = ; i < (int)c.size(); i++)
printf("%lld%c", c[i], " \n"[i == (int)c.size() - ]);
} namespace Poly {
const int L = << ;
const ll P = 1004535809LL; int lim, pos[L]; template <typename T>
inline void inc(T &x, T y) {
x += y;
if (x >= P) x -= P;
} template <typename T>
inline void sub(T &x, T y) {
x -= y;
if (x < ) x += P;
} inline void reverse(poly &c) {
for (int i = , j = (int)c.size() - ; i < j; i++, j--) swap(c[i], c[j]);
} inline ll fpow(ll x, ll y) {
ll res = ;
for (; y > ; y >>= ) {
if (y & ) res = res * x % P;
x = x * x % P;
}
return res;
} inline void prework(int len) {
int l = ;
for (lim = ; lim < len; lim <<= , ++l);
for (int i = ; i < lim; i++)
pos[i] = (pos[i >> ] >> ) | ((i & ) << (l - ));
} inline void ntt(poly &c, int opt) {
c.resize(lim, );
for (int i = ; i < lim; i++)
if (i < pos[i]) swap(c[i], c[pos[i]]);
for (int i = ; i < lim; i <<= ) {
ll wn = fpow(, (P - ) / (i << ));
if (opt == -) wn = fpow(wn, P - );
for (int len = i << , j = ; j < lim; j += len) {
ll w = ;
for (int k = ; k < i; k++, w = w * wn % P) {
ll x = c[j + k], y = c[j + k + i] * w % P;
c[j + k] = (x + y) % P, c[j + k + i] = (x - y + P) % P;
}
}
} if (opt == -) {
ll inv = fpow(lim, P - );
for (int i = ; i < lim; i++) c[i] = c[i] * inv % P;
}
} inline poly mul(const poly x, const poly y) {
poly u = x, v = y, res;
prework(x.size() + y.size() - );
ntt(u, ), ntt(v, );
for (int i = ; i < lim; i++) res.push_back(u[i] * v[i] % P);
ntt(res, -);
res.resize(x.size() + y.size() - );
return res;
} } using Poly :: P;
using Poly :: fpow;
using Poly :: mul;
using Poly :: inc;
using Poly :: reverse; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for (; ch > ''|| ch < ''; ch = getchar())
if (ch == '-') op = -;
for (; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void prework(int len) {
fac[] = ;
for (int i = ; i <= len; i++) fac[i] = fac[i - ] * i % P;
ifac[len] = fpow(fac[len], P - );
for (int i = len - ; i >= ; i--) ifac[i] = ifac[i + ] * (i + ) % P;
} inline ll getC(int x, int y) {
return fac[x] * ifac[y] % P * ifac[x - y] % P;
} int main() {
read(n), read(m), read(s);
for (int i = ; i <= m; i++) read(w[i]); int rep = min(m, n / s);
prework(max(n, m)); for (int i = ; i <= rep; i++)
f[i] = getC(m, i) * getC(n, i * s) % P * fac[i * s] % P * fpow(ifac[s], i) % P * fpow(m - i, n - i * s) % P; /* for (int i = 0; i <= rep; i++)
printf("%lld%c", f[i], " \n"[i == rep]); */ poly a, b;
a.resize(rep + , ), b.resize(rep + , );
for (int i = ; i < rep + ; i++) {
a[i] = f[i] * fac[i] % P;
b[i] = ifac[i];
if (i & ) b[i] = (P - b[i]) % P;
}
reverse(b); // deb(a), deb(b); poly c = mul(a, b); // deb(c); /* for (int i = rep; i >= 0; i--) {
sum[i] = sum[i + 1];
inc(sum[i], c[i]);
} for (int i = 0; i <= rep; i++)
printf("%lld%c", sum[i], " \n"[i == rep]); */ ll ans = ;
for (int i = ; i <= rep; i++) {
g[i] = ifac[i] * c[rep + i] % P;
inc(ans, w[i] * g[i] % P);
} printf("%lld\n", ans);
return ;
}