【BZOJ5306】【HAOI2018】染色(容斥原理,NTT)

Description

click me


Solution

直接算不太方便,考虑容斥。
易得容斥系数满足:

wt=i=0t(ti)fi w t = ∑ i = 0 t ( t i ) f i

其实有了这个式子我们就可以 O(m2) O ( m 2 ) 求出容斥系数了,考虑优化。

我们将所有式子都列出来:
w0=f0 w 0 = f 0
w1=f0+f1 w 1 = f 0 + f 1
w2=f0+2f1+f2 w 2 = f 0 + 2 f 1 + f 2

解这些方程:
f0=w0 f 0 = w 0
f1=w0+w1 f 1 = − w 0 + w 1
f2=w02w1+w2 f 2 = w 0 − 2 w 1 + w 2

找规律得: fi=j=0i(1)ij(ij)wj=i!j=0i(1)ij(ij)!×wjj! f i = ∑ j = 0 i ( − 1 ) i − j ( i j ) w j = i ! ∑ j = 0 i ( − 1 ) i − j ( i − j ) ! × w j j !
发现是个卷积的形式,NTT即可。

最后的答案为:

i=0min{m,ns}(mi)nis(mi)nisfi(s!)i ∑ i = 0 min { m , ⌊ n s ⌋ } ( m i ) n i s _ ( m − i ) n − i s f i ( s ! ) i


Code

/************************************************
 * Au: Hany01
 * Date: Jun 1st, 2018
 * Prob: [BZOJ5306][HAOI2018] 染色
 * Email: [email protected]
************************************************/

#include

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1004535809)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
#define g0 (3)

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 10000005, maxm = 100005, maxs = 155;

int n, m, s;
LL fac[maxn], ifac[maxn], Ans, w[maxm], a[maxm << 2], b[maxm << 2], powg[maxm << 2], invg[maxm << 2], cnt, rev[maxm << 2], nn, invg0, invn;

inline LL Pow(LL a, LL b) {
    LL Ans = 1;
    for ( ; b; b >>= 1, (a *= a) %= Mod) if (b & 1) (Ans *= a) %= Mod;
    return Ans;
}

inline LL C(int n, int m) { return fac[n] * ifac[n - m] % Mod * ifac[m] % Mod; }

inline LL ndown(int k) { return fac[n] * ifac[n - k] % Mod; }

inline void NTT(LL *a, int n, int type)
{
    rep(i, n) if (i < rev[i]) swap(a[i], a[rev[i]]);
    for (int i = 2; i <= n; i <<= 1) {
        LL wn = type ? powg[i] : invg[i];
        for (int j = 0; j < n; j += i) {
            LL w = 1;
            rep(k, i >> 1) {
                LL x = a[j + k], y = a[j + k + (i >> 1)] * w % Mod;
                a[j + k] = (x + y) % Mod, a[j + k + (i >> 1)] = (x - y) % Mod;
                (w *= wn) %= Mod;
            }
        }
    }
}

int main()
{
#ifdef hany01
    File("bzoj5306");
#endif

    n = read(), m = read(), s = read(), fac[0] = 1;
    static int N = min(n / s, m), nm = max(n, m);
    For(i, 0, m) w[i] = read();
    For(i, 1, nm) fac[i] = fac[i - 1] * (LL)i % Mod;
    ifac[nm] = Pow(fac[nm], Mod - 2);
    Fordown(i, nm, 1) ifac[i - 1] = ifac[i] * (LL)i % Mod;


    For(i, 0, N) a[i] = ((i & 1) ? -1 : 1) * ifac[i], b[i] = w[i] * ifac[i];
    for (nn = 1, cnt = 0; nn <= (N << 1); nn <<= 1, ++ cnt);
    rep(i, nn) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1));
    invg0 = Pow(g0, Mod - 2);
    for (int i = 1; i <= nn; i <<= 1) powg[i] = Pow(g0, (Mod - 1) / i), invg[i] = Pow(invg0, (Mod - 1) / i);
    NTT(a, nn, 1), NTT(b, nn, 1);
    rep(i, nn) (a[i] *= b[i]) %= Mod;
    NTT(a, nn, 0), invn = Pow(nn, Mod - 2);
    rep(i, nn) (a[i] *= invn * fac[i] % Mod) %= Mod;


    For(i, 0, N) (Ans += C(m, i) * ndown(i * s) % Mod * Pow(ifac[s], i) % Mod * a[i] % Mod * Pow(m - i, n - i * s) % Mod) %= Mod;
    printf("%lld\n", (Ans + Mod) % Mod);

    return 0;
}
//柴门闻犬吠,风雪夜归人。
//    -- 刘长卿《逢雪宿芙蓉山主人》

你可能感兴趣的:(BZOJ,省选,容斥原理,FFT)