LOJ2527 "HAOI2018" staining

Description

Topic Link

Given length \ (n-\) sequence, you need to use \ (m \) colors for staining. If one kind of coloring schemes appear exactly \ (S \) times the color \ (K \) species, then its price for \ (W_k \) . The sum of the cost to do everything possible to the staining protocol \ (1004535809 \) results modulo

\ (N \ 10 ^ 7, m \ 10 ^ 5, S \ the 150.0 \ the w_i <1004535809 \)

Solution

Taking into account if one kind of staining protocols appear in \ (S \) the number of colors a given time, and it will set the price, so we can first calculate exactly appear \ (S \) times the color \ (K \ ) several kinds of programs, and then multiplied by the corresponding \ (W_K \) is the result we want the

Violence first write \ (\ text {DP} \ ) equation set \ (dp_ {i, j, k} \) represents the former have been used \ (I \) on to the color sequence \ (J \) positions dyed, and wherein just stained \ (S \) times the color \ (K \) several solutions thereof, then there is
\ [dp_ {i, j, k} = \ dbinom {n-j + S} {S} dp_ {i-1 , jS, k-1} + \ sum \ limits_ {0 \ le c \ le j and c \ not = S} \ dbinom {n-j + c} {c} dp_ {i -1, jc, k} \]
answer is \ (\ SUM \ limits_ I = {0} {m} ^ {m dp_, n-, I} W_i \) , the complexity of the \ (O (n ^ 2m ^ 2) \)

Consider the generating function? The number of states too

Found not optimized, only the number of states is already \ (O (nm ^ 2) \) of

Back to the topic, we ask that appears \ (S \) times the color exactly \ (K \) several programs, so as to help us calculate the cost

exactly? Consider the generalized principle of inclusion-exclusion

In fact, and the general principles of inclusion and exclusion are no different, the problem is that the coefficient of

Inclusion and exclusion composition forms, one having \ (K \) PROPERTICS plan will we limit the need at least satisfy the \ (I \) when the kinds of properties are calculated to \ (\ binom {K} { i} \) times, and we want to obtain a set of \ (\ {F_n \} \) , such that
\ [[x == K] = \ sum \ limits_ {i = 0} ^ {x} \ dbinom {x} {i} F_i \]
binomial inversion
\ [f_n = \ sum \ limits_
{i = 0} ^ {n} (- 1) ^ {ni} \ dbinom {n} {i} [i == K] \] That
\ [f_n = (- 1)
^ {nK} \ dbinom {n} {K} \] return to the subject, we have now got the capacity repellent factor is calculated considering limitation occurs \ (S \) secondary colors are at least \ (I \) several programs, provided this step the program number \ (G_i \) , then there is
\ [g_i = \ dbinom {m } {i} \ frac {n!} {(S!) ^ i (n-iS)!} ( mi) ^ {n-iS} \]

Wherein \ (g_0 = m ^ n \ )

Now demand appears \ (S \) times the color exactly \ (K \) several kinds of programs is not a problem

Set \ (F_ {I, J} = (-. 1) ^ {ij of} \ dbinom {I} {J} \) , \ (C_i \) indicates that an \ (S \) times the color exactly \ (I \ ) number scheme species, then there is
\ [c_K = \ sum \ limits_
{i = 0} ^ {m} f_ {i, K} g_i \] directly substituted into \ (F \) and \ (G \) values, have
\ [c_K = \ sum \ limits_ {i = 0} ^ {m} (- 1) ^ {iK} \ dbinom {i} {K} \ dbinom {m} {i} \ frac {n!} {( !! S) ^ i (n
-iS)} (mi) ^ {n-iS} \] to simply change element
\ [c_i = \ sum \ limits_ {j = 0} ^ {m} (- 1) ^ {ji} \ dbinom {j}
{i} \ dbinom {m} {j} \ frac {n!} {(S!) ^ j (n-jS)!} (mj) ^ {n-jS} \] the number of combinations of violence expand to give
\ [c_i = \ frac {m n!!} \ sum \ limits_ {j = 0} ^ {m} {i!} (- 1) ^ {ji} \ frac {1} { (ji)!} \ frac {
1} {(mj)!} \ frac {1} {(S!) ^ j (n-jS)!} (mj) ^ {n-jS} \] redefinition \ ( = F_i (-. 1) ^ I \ FRAC. 1 {I} {!} \) ,\(g_j=\frac{1}{(m-j)!}\frac{1}{(S!)^j(n-jS)!}(m-j)^{n-jS}\)

Then
\ [c_i = \ frac {m
! N!} {I!} \ Sum \ limits_ {j = i} ^ {m} f_ {ji} g_j \] The (F \) \ flipped to give \ (f ' \) , then
\ [c_i = \ frac {m
! n!} {i!} \ sum \ limits_ {j = i} ^ {m} f '_ {m + ij} g_j \] can be directly written
\ [C_i = \ {FRAC m! n-!} I {!} \ SUM \ limits_ J = {0} ^ {m} + I F '{_} G_J ij of m + \]
\ (\ text NTT} {\) to complexity \ (O (n + m \ log m) \)

code show as below:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
const int M=1e7+10;
const int mod=1004535809;
const int G=3;
const int invG=334845270;
int n,m,s,w[N],fac[M],inv[M],A,d[N],f[N<<2],g[N<<2],k,INV,c[N],ans;
inline void Add(int &x,int y){x+=y;x-=x>=mod? mod:0;}
inline int MOD(int x){x-=x>=mod? mod:0;return x;}
inline int Minus(int x){x+=x<0? mod:0;return x;}
inline int fas(int x,int p){int res=1;while(p){if(p&1)res=1ll*res*x%mod;p>>=1;x=1ll*x*x%mod;}return res;}
inline void Preprocess(){
    int t=max(n,max(m,s));
    fac[0]=1;for(register int i=1;i<=t;i++)fac[i]=1ll*fac[i-1]*i%mod;
    inv[t]=fas(fac[t],mod-2);inv[0]=inv[1]=1;
    for(register int i=t-1;i>=2;i--)inv[i]=1ll*inv[i+1]*(i+1)%mod;
}
inline void NTT(int *a,int f){
    for(register int i=0,j=0;i<k;i++){
        if(i>j)swap(a[i],a[j]);
        for(register int l=k>>1;(j^=l)<l;l>>=1);}
    for(register int i=1;i<k;i<<=1){
        int w=fas(~f? G:invG,(mod-1)/(i<<1));
        for(register int j=0;j<k;j+=(i<<1)){
            int e=1;
            for(register int p=0;p<i;p++,e=1ll*e*w%mod){
                int x=a[j+p],y=1ll*a[j+p+i]*e%mod;
                a[j+p]=MOD(x+y);a[j+p+i]=MOD(x-y+mod);
            }
        }
    }
}
int main(){
    scanf("%d%d%d",&n,&m,&s);Preprocess();
    for(register int i=0;i<=m;i++)scanf("%d",&w[i]);
    A=1ll*fac[n]*fac[m]%mod;
    for(register int i=0;i<=m;i++)d[i]=1ll*A*inv[i]%mod;
    for(register int i=0;i<=m;i++)
        f[i]=1ll*((i&1)? mod-1:1)*inv[i]%mod;
    for(register int i=0;i<=m;i++)
        if(n-i*s>=0)g[i]=1ll*inv[m-i]*fas(inv[s],i)%mod*inv[n-i*s]%mod*fas(m-i,n-i*s)%mod;else break;
    reverse(f,f+m+1);k=1;while(k<=m+m)k<<=1;
    NTT(f,1);NTT(g,1);
    for(register int i=0;i<k;i++)f[i]=1ll*f[i]*g[i]%mod;
    NTT(f,-1);INV=fas(k,mod-2);
    for(register int i=0;i<k;i++)f[i]=1ll*f[i]*INV%mod;
    for(register int i=0;i<=m;i++)c[i]=1ll*d[i]*f[i+m]%mod;
    for(register int i=0;i<=m;i++)Add(ans,1ll*c[i]*w[i]%mod);
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/ForwardFuture/p/11544014.html