• 【LOJ】#2527. 「HAOI2018」染色


    题解

    简单容斥题

    至少选了(k)个颜色恰好出现(S)次方案数是
    (F[k] = inom{M}{k} frac{N!}{(S!)^{k}(N - i * S)!}(M - k)^{N - i * S})

    然后恰好(k)个颜色恰好出现(k)次就是
    (g[k] = sum_{j = k}^{M} (-1)^{k - j} inom{j}{k}F[j])
    然后就是
    (g[k]*k! = sum_{j = k}^{M}frac{(-1)^{j - k}}{(j - k)!} F[j]*j!)
    后面的只要把其中一个指数取反,就可以NTT卷积优化了

    代码

    #include <bits/stdc++.h>
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define pdi pair<db,int>
    #define mp make_pair
    #define pb push_back
    #define enter putchar('
    ')
    #define space putchar(' ')
    #define eps 1e-8
    #define MAXN 100005
    #define mo 974711
    //#define ivorysi
    using namespace std;
    typedef long long int64;
    typedef double db;
    template<class T>
    void read(T &res) {
        res = 0;char c = getchar();T f = 1;
        while(c < '0' || c > '9') {
    	if(c == '-') f = -1;
    	c = getchar();
        }
        while(c >= '0' && c <= '9') {
    	res = res * 10 + c - '0';
    	c = getchar();
        }
        res *= f;
    }
    template<class T>
    void out(T x) {
        if(x < 0) {x = -x;putchar('-');}
        if(x >= 10) {
    	out(x / 10);
        }
        putchar('0' + x % 10);
    }
    const int MOD = 1004535809;
    const int MAXL = 1 << 20;
    int W[MAXL + 5],a[MAXN],N,M,S,F[MAXN];
    int fac[10000005],invfac[10000005];
    int f[1000005],g[1000005];
    int inc(int a,int b) {
        return a + b >= MOD ? a + b - MOD : a + b;
    }
    int mul(int a,int b) {
        return 1LL * a * b % MOD;
    }
    int C(int n,int m) {
        if(n < m) return 0;
        return mul(fac[n],mul(invfac[m],invfac[n - m]));
    }
    int fpow(int x,int c) {
        int res = 1,t = x;
        while(c) {
    	if(c & 1) res = mul(res,t);
    	t = mul(t,t);
    	c >>= 1;
        }
        return res;
    }
    void NTT(int *p,int L,int on) {
        for(int i = 1 , j = L >> 1 ; i < L - 1 ; ++i) {
    	if(i < j) swap(p[i],p[j]);
    	int k = L >> 1;
    	while(j >= k) {
    	    j -= k;
    	    k >>= 1;
    	}
    	j += k;
        }
        for(int h = 2 ; h <= L ; h <<= 1) {
    	int wn = W[(MAXL + on * MAXL / h) % MAXL];
    	for(int k = 0 ; k < L ; k += h) {
    	    int w = 1;
    	    for(int j = k ; j < k + h / 2 ; ++j) {
    		int u = p[j],t = mul(w,p[j + h / 2]);
    		p[j] = inc(u,t);
    		p[j + h / 2] = inc(u,MOD - t);
    		w = mul(w,wn);
    	    }
    	}
        }
        if(on == -1) {
    	int InvL = fpow(L,MOD - 2);
    	for(int i = 0 ; i < L ; ++i) p[i] = mul(InvL,p[i]);
        }
    }
    void Solve() {
        read(N);read(M);read(S);
        for(int i = 0 ; i <= M ; ++i) read(a[i]);
        fac[0] = 1;
        for(int i = 1 ; i <= 10000000 ; ++i) fac[i] = mul(fac[i - 1],i);
        invfac[10000000] = fpow(fac[10000000],MOD - 2);
        for(int i = 10000000 - 1 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
        W[0] = 1;
        W[1] = fpow(3,(MOD - 1) / MAXL);
        for(int i = 2 ; i < MAXL ; ++i) W[i] = mul(W[i - 1],W[1]);
        int t = 1;
        for(int i = 0 ; i <= M ; ++i) {
    	if(N / S < i) break;
    	F[i] = mul(C(M,i) , mul(fac[N] , mul(t , invfac[N - i * S])));
    	F[i] = mul(F[i],fpow(M - i,N - i * S));
    	t = mul(t,invfac[S]);
        }
        for(int i = 0 ; i <= M ; ++i) f[i] = mul(F[i],fac[i]);
        t = 1;
        for(int i = 0 ; i <= M ; ++i) {
    	g[M - i] = mul(t,invfac[i]);
    	t = mul(t,MOD - 1);
        }
        t = 1;
        while(t <= 2 * M) t <<= 1;
        NTT(f,t,1);NTT(g,t,1);
        for(int i = 0 ; i < t ; ++i) g[i] = mul(g[i],f[i]);
        NTT(g,t,-1);
       
        int ans = 0;
        for(int i = 0 ; i <= M ; ++i) {
    	g[i + M] = mul(g[i + M],invfac[i]);
    	ans = inc(ans,mul(g[i + M],a[i]));
        }
        
        out(ans);enter;
    }
    int main() {
    #ifdef ivorysi
        freopen("f1.in","r",stdin);
    #endif
        Solve();
    }
    
  • 相关阅读:
    zookeeper基础笔记
    基于spring@aspect注解的aop实现
    Struts2中的开启AsyncContext的方法
    在执行gem install redis时 : ERROR: Error installing redis: redis requires Ruby version >= 2.2.2
    ConcurrentHashMap原理笔记
    Java并发Condition原理分析
    CountDownLatch实现原理
    ThreadPoolExecutor 线程池原理分析
    HashMap原理
    线程池的用法
  • 原文地址:https://www.cnblogs.com/ivorysi/p/10048651.html
Copyright © 2020-2023  润新知