luoguP4491 [HAOI2018]染色 广义容斥原理 + FFT

1326433-20181225143347813-526254494.png


非常明显的摆了一个NTT模数....

题目中求恰好\(k\),那么考虑求至少\(k\)

\(g(k)\)表示至少\(k\)中颜色出现了恰好\(S\)

那么,\[g(k) = \binom{M}{k} \frac{N!}{(S!)^k (N-Sk)!} * (M-k)^{N-Sk}\]

根据广义容斥原理,记\(f(i)\)表示恰好\(k\)种颜色出现了恰好\(k\)

那么,\[f(i) = \sum \limits_{k = i}^M (-1)^{k - i} \binom{k}{i} g(k)\]

化成卷积式

\[f(i) * i! = \sum \limits_{k = i}^M \frac{(-1)^{k - i}}{(k - i)!} k! g(k)\]

\(F_i = \frac{(-1)^{i}}{i!}\)\(G_i = i! g(i)\)

\(H_i\)表示\(f(i) * i\),那么

\[H_i = \sum \limits_{j = i}^M F(k - i) * G(k)\]

反转下标,有

\[H_{n - i}' = \sum \limits_{i = 0}^{n - i} F(k) * G'(n - i - k)\]

\(NTT\)即可,复杂度\(O(n \log n)\)


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
    
#define gc getchar
inline int read() {
    int p = 0, w = 1; char c = gc();
    while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }
    while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
    return p * w;
}

const int sid = 3e5 + 5;
const int cid = 1e7 + 5;
const int mod = 1004535809;

inline int mul(int a, int b) { return 1ll * a * b % mod; }
inline int fp(int a, int k) { 
    int ret = 1; 
    for( ; k; k >>= 1, a = mul(a, a))
        if(k & 1) ret = mul(ret, a);
    return ret;
}

int N, M, S, n, lg;
int fac[cid], inv[cid];
int rev[sid], f[sid], g[sid], w[sid], W[sid];

inline int C(int n, int m) {
    if(n < m) return 0;
    return mul(fac[n], mul(inv[m], inv[n - m]));
}

inline void NTT(int *a) {
    for(ri i = 0; i < n; i ++)
        if(i < rev[i]) swap(a[i], a[rev[i]]);
    for(ri i = 1; i < n; i <<= 1)
    for(ri j = 0, kj = n / (i << 1); j < n; j += (i << 1))
    for(ri k = j, kp = 0; k < i + j; k ++, kp += kj) {
        int x = a[k], y = mul(w[kp], a[i + k]); 
        a[k] = (x + y >= mod) ? x + y - mod : x + y;
        a[i + k] = (x - y < 0) ? x - y + mod : x - y;
    }
}

inline void calc() {
    n = 1; lg = 0;
    while(n <= M + M) n <<= 1, lg ++;
    rep(i, 0, n) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lg - 1));
    int g_ = fp(3, (mod - 1) / n);
    w[0] = 1; 
    rep(i, 1, n) w[i] = mul(w[i - 1], g_);
    
    int lim = max(N, n);
    fac[0] = fac[1] = inv[0] = inv[1] = 1;
    rep(i, 2, lim) {
        fac[i] = mul(fac[i - 1], i);
        inv[i] = mul(inv[mod % i], mod - mod / i);
    }
    rep(i, 2, lim) inv[i] = mul(inv[i], inv[i - 1]);
        
    rep(i, 0, M - 1) f[i] = mul(inv[i], (i & 1) ? mod - 1: 1);
    rep(i, 0, M) if(N >= S * i)
        g[i] = 1ll*fac[i]*C(M,i)%mod*fac[N]%mod*fp(inv[S],i)%mod*inv[N-S*i]%mod*fp(M-i,N-S*i)%mod;
    reverse(g, g + M + 1);

    NTT(f); NTT(g);
    rep(i, 0, n) f[i] = mul(f[i], g[i]);
    NTT(f); 
    int ivn = fp(n, mod - 2);
    reverse(f + 1, f + n); reverse(f, f + M + 1);
    rep(i, 0, n) f[i] = mul(f[i], mul(ivn, inv[i]));
        
    int ans = 0;
    rep(i, 0, M) ans = (ans + mul(f[i], W[i])) % mod;
    printf("%d\n", ans);

}

int main() {
    N = read(); M = read(); S = read();
    rep(i, 0, M) W[i] = read();
    calc();
    return 0;
}

转载于:https://www.cnblogs.com/reverymoon/p/10173833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值