• 各种反演细节梳理&模板


    炫酷反演魔术课件byVFK
    stO FDF Orz(证明全有%%%)

    莫比乌斯反演

    (F(n)=sumlimits_{d|n}f(d)Rightarrow f(n)=sumlimits_{d|n}mu(frac n d)F(d))
    (F(n)=sumlimits_{n|d}f(d)Rightarrow f(n)=sumlimits_{n|d}mu(frac d n)F(d))
    推带(gcd)的题常用式子:(实际上是借用了积性函数的式子)
    ([gcd(i,j)==1]=sumlimits_{d|gcd(i,j)}mu(d))
    (gcd(i,j)=sumlimits_{d|i,d|j}varphi(d))


    洛谷P3455 [POI2007]ZAP-Queries
    (f(n)=sumlimits_{i=1}^asumlimits_{j=1}^b[gcd(i,j)==n])
    1.老实反演
    (F(n)=sumlimits_{n|d}f(d)=lfloorfrac a n floorlfloorfrac b n floor)
    (f(n)=sumlimits_{n|d}mu(frac{d}{n})lfloorfrac{a}{d} floorlfloorfrac{b}{d} floor=sumlimits_{d=1}^{lceilfrac{min(a,b)}{n} ceil}mu(d)lfloorfrac a{nd} floorlfloorfrac b{nd} floor)
    2.套式子
    (f(n)=sumlimits_{i=1}^{lfloorfrac{a}{n} floor}sumlimits_{j=1}^{lfloorfrac{b}{n} floor}[gcd(i,j)==1]=sumlimits_{d|gcd(i,j)}sumlimits_{i=1}^{lfloorfrac{a}{n} floor}sumlimits_{j=1}^{lfloorfrac{b}{n} floor}mu(d)=sumlimits_{d=1}^{lfloorfrac{min(a,b)}{n} floor}mu(d)lfloorfrac a{nd} floorlfloorfrac b{nd} floor)
    整除分块,时间复杂度(O(sqrt n))

    #include<bits/stdc++.h>
    #define LL long long
    #define RG register
    #define R RG int
    #define G if(++ip==ie)if(fread(ip=buf,1,SZ,stdin))
    using namespace std;
    const int SZ=1<<19,N=50009;
    int u[N],pr[N],p;bool f[N];
    char buf[SZ],*ie=buf+SZ,*ip=ie-1;
    inline int in(){
    	G;while(*ip<'-')G;
    	R x=*ip&15;G;
    	while(*ip>'-'){x*=10;x+=*ip&15;G;}
    	return x;
    }
    int main(){
    	u[1]=f[1]=1;
    	for(R i=2;i<N;++i){
    		if(!f[i])u[pr[++p]=i]=-1;
    		for(R j=1;j<=p&&i*pr[j]<N;++j){
    			f[i*pr[j]]=1;
    			if(i%pr[j]==0)break;
    			u[i*pr[j]]=-u[i];
    		}
    		u[i]+=u[i-1];
    	}
    	for(R n=in();n;--n){
    		R a=in(),b=in(),d=in(),lim=min(a,b)/d,ans=0;
    		for(R l=1,r;l<=lim;l=r+1){
    			r=min(a/(a/l),b/(b/l));
    			ans+=(u[r]-u[l-1])*(a/(d*l))*(b/(d*l));
    		}
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    

    洛谷P1829 [国家集训队]Crash的数字表格 / JZPTAB

    [sum_{i=1}^nsum_{j=1}^mfrac{ij}{gcd(i,j)}\=sum_{k=1}^nsum_{i=1}^nsum_{j=1}^mfrac{ij}{k}[gcd(i,j)==k]\=sum_{k=1}^nsum_{i=1}^frac{n}{k}sum_{j=1}^frac{m}{k}ijk[gcd(i,j)==1]\=sum_{k=1}^nkcdotsum_{i=1}^frac{n}{k}sum_{j=1}^frac{m}{k}sum_{d|gcd(i,j)}mu(d)ij\=sum_{k=1}^nkcdotsum_{d=1}^frac{n}{k}mu(d)sum_{i=1}^frac{n}{kd}sum_{j=1}^frac{m}{kd}d^2ij\=sum_{k=1}^nkcdotsum_{d=1}^frac{n}{k}d^2mu(d)frac{frac{n}{kd}(frac{n}{kd}+1)}{2}frac{frac{m}{kd}(frac{m}{kd}+1)}{2} ]

    #include<bits/stdc++.h>
    #define LL long long
    #define RG register
    #define R RG int
    #define YL 20101009
    using namespace std;
    const int N=1e7+9;
    int p,pr[N/10],mu[N],s[N];bitset<N>np;
    inline LL Sum(LL x){
    	return x*(x+1)%YL*((YL+1)>>1)%YL;
    }
    LL Calc(R n,R m){
    	LL ans=0;
    	for(R d=1,r;d<=n;d=r+1){
    		r=min(n/(n/d),m/(m/d));
    		ans=(ans+(s[r]-s[d-1]+YL)*Sum(n/d)%YL*Sum(m/d))%YL;
    	}
    	return ans;
    }
    int main(){
    	R n,m;LL ans=0;
    	cin>>n>>m;
    	if(n>m)swap(n,m);
    	s[1]=1;
    	for(R i=2;i<=n;++i){
    		if(!np[i])pr[++p]=i,mu[i]=-1;
    		s[i]=(s[i-1]+(LL)i*i%YL*mu[i]+YL)%YL;
    		for(R j=1;j<=p&&i*pr[j]<=n;++j){
    			np[i*pr[j]]=1;
    			if(i%pr[j]==0)break;
    			mu[i*pr[j]]=-mu[i];
    		}
    	}
    	for(R k=1,r;k<=n;k=r+1){
    		r=min(n/(n/k),m/(m/k));
    		ans=(ans+(Sum(r)-Sum(k-1)+YL)*Calc(n/k,m/k))%YL;
    	}
    	cout<<ans<<endl;
    	return 0;
    }
    

    然鹅BZOJ的JZPTAB有多次询问咋办?
    详细题解by hbyer
    有一个新的套路:枚举重复出现的(kd)(令其为(t)

    [sum_{t=1}^nfrac{frac{n}{t}(frac{n}{t}+1)}{2}frac{frac{m}{t}(frac{m}{t}+1)}{2}sum_{d|t}tdmu(d) ]

    后面那个(sum_{d|t}tdmu(d))看成关于(t)的函数,暴力推一推发现是个积性函数,筛一下就好了。


    洛谷P3704 [SDOI2017]数字表格
    跟上一题一样的套路

    [prodlimits_{i=1}^nprodlimits_{j=1}^mFib_{gcd(i,j)}=prodlimits_{k=1}^nFib_k^{sumlimits_{d=1}^nmu(d)frac{n}{kd}frac{m}{kd}}=prodlimits_{t=1}^nprodlimits_{k|t}Fib_k^{mu(frac{t}{k})frac{n}{t}frac{m}{t}}\=prodlimits_{t=1}^nleft(prodlimits_{k|t}Fib_k^{mu(frac{t}{k})} ight)^{frac{n}{t}frac{m}{t}} ]

    中间那块预处理前缀积,外面数论分块即可。

    二项式反演

    不常用但是好看:(f(n)=sumlimits_{i=0}^n(-1)^iinom{n}{i}g(i)Rightarrow g(n)=sumlimits_{i=0}^n(-1)^iinom{n}{i}f(i))
    正好难算,至多好算:(f(n)=sumlimits_{i=0}^ninom{n}{i}g(i)Rightarrow g(n)=sumlimits_{i=0}^n(-1)^{n-i}inom{n}{i}f(i))
    正好难算,至少好算:(f(k)=sumlimits_{i=k}^ninom{i}{k}g(i)Rightarrow g(k)=sumlimits_{i=k}^n(-1)^{i-k}inom{i}{k}f(i))

    错排

    (f(n))为至多(n)元素错排方案数也就是全排列方案数(n!)(g(n))(n)元素错排方案数。
    通过枚举排列中错位的元素个数(i),有(f(n)=sumlimits_{i=0}^ninom{n}{i}g(i))
    直接套第二个式子即可得(g(n)=sumlimits_{i=0}^n(-1)^{n-i}inom{n}{i}f(i)=n!sumlimits_{i=0}^n(-1)^ifrac{1}{i!})

    又一个例题

    洛谷P4859 已经没有什么好害怕的了
    套第三个式子。然并卵关键要想到如何DP和转化

    #include<bits/stdc++.h>
    #define LL long long
    #define RG register
    #define R RG int
    using namespace std;
    const LL N=2009,YL=1e9+9;
    int a[N],b[N],ff[N],gg[N],F[N],I[N];
    int Pow(LL b,R k,LL a=1){
        for(;k;k>>=1,b=b*b%YL)
            if(k&1)a=a*b%YL;
        return a;
    }
    int main(){
        R n,k,ans=0,*f=ff,*g=gg;
        cin>>n>>k;k=(n+k)/2;
        for(R i=1;i<=n;++i)cin>>a[i];
        for(R i=1;i<=n;++i)cin>>b[i];
        sort(a+1,a+n+1);
        sort(b+1,b+n+1);
        f[0]=g[0]=F[0]=1;
        for(R i=1;i<=n;++i){
            R pos=lower_bound(b+1,b+n+1,a[i])-b-1;
            for(R j=1;j<=i;++j)
                g[j]=(f[j]+(LL)f[j-1]*max(pos-j+1,0))%YL;
            swap(f,g);
        }
        for(R i=1;i<=n;++i)F[i]=(LL)F[i-1]*i%YL;
        I[n]=Pow(F[n],YL-2);
        for(R i=n;i;--i)I[i-1]=(LL)I[i]*i%YL;
        for(R i=k;i<=n;++i)
            ans=(ans+(LL)((i-k)&1?YL-1:1)*F[i]%YL*I[k]%YL*I[i-k]%YL*F[n-i]%YL*f[i])%YL;
        cout<<ans<<endl;
        return 0;
    }
    

    斯特林反演

    十分像二项式反演。。。
    正好难算,至多好算:(f(n)=sumlimits_{i=0}^nS(n,i)g(i)Rightarrow g(n)=sumlimits_{i=0}^n(-1)^{n-i}s(n,i)f(i))
    正好难算,至少好算:(f(k)=sumlimits_{i=k}^nS(i,k)g(i)Rightarrow g(k)=sumlimits_{i=k}^n(-1)^{i-k}s(i,k)(i))
    BZOJ4671 异或图
    套第二个式子。然并卵关键要想到子集划分计数和线性基
    完整题解:stO yyb Orz

    #include<bits/stdc++.h>
    #define LL long long
    #define RG register
    #define R RG int
    using namespace std;
    const int S=69,N=19;
    char str[S];bool g[S][N][N];
    int s,n,c[N];
    LL ans,lb[S],fac[N];
    void calc(R t){
    	R cnt=0;memset(lb,0,sizeof(lb));
    	for(R k=1;k<=s;++k){
    		R p=0;LL now=0;
    		for(R i=1;i<=n;++i)
    			for(R j=i+1;j<=n;++j)
    				if(c[i]!=c[j])now|=(LL)g[k][i][j]<<p++;
    		for(R i=0;i<p;++i)
    			if(1ll<<i&now){
    				if(lb[i])now^=lb[i];
    				else{lb[i]=now;++cnt;break;}
    			}
    	}
    	ans+=(1&t?1:-1)*fac[t-1]*(1ll<<(s-cnt));
    }
    void dfs(R x,R t){
    	if(x>n)return calc(t-1);
    	for(R&i=c[x]=1;i<=t;++i)dfs(x+1,t+(t==i));
    }
    int main(){
    	cin>>s;
    	for(R k=1;k<=s;++k){
    		cin>>str;n=(1+sqrt(1+8*strlen(str)))/2;
    		for(R i=1,p=0;i<=n;++i)
    			for(R j=i+1;j<=n;++j)
    				g[k][i][j]=str[p++]&1;
    	}
    	for(R i=fac[0]=1;i<=n;++i)fac[i]=fac[i-1]*i;
    	dfs(1,1);
    	cout<<ans<<endl;
    	return 0;
    }
    

    最值反演(min-max容斥)

    (max{S}=sumlimits_{Tsubseteq S}(-1)^{|T|+1}min{T})
    (min{S}=sumlimits_{Tsubseteq S}(-1)^{|T|+1}max{T})
    在期望意义下仍然成立,即
    (E(max{S})=sumlimits_{Tsubseteq S}(-1)^{|T|+1}E(min{T}))
    (E(min{S})=sumlimits_{Tsubseteq S}(-1)^{|T|+1}E(max{T}))


    推广到求第(k)大,只有大小(ge k)的子集产生贡献
    (max_k{S}=sumlimits_{Tsubseteq S}(-1)^{|T|-k}inom{|T|-1}{k-1}min{T})
    (min_k{S}=sumlimits_{Tsubseteq S}(-1)^{|T|-k}inom{|T|-1}{k-1}max{T})


    洛谷P3175 [HAOI2015]按位或
    裸题,FWT之后套第三个式子即可

    #include<bits/stdc++.h>
    #define R register int
    using namespace std;
    double EPS=1e-10,a[1<<20];
    int c[1<<20];
    int main(){
    	R n,m,s=0;
    	cin>>n;m=1<<n;
    	for(R i=0;i<m;++i){
    		cin>>a[i];
    		if(a[i]>EPS)s|=i;
    		c[i]=c[i>>1]^(1&i);
    	}
    	if(s!=m-1)return puts("INF"),0;
    	for(R i=1;i<m;i<<=1)
    		for(R j=0;j<m;j+=i<<1)
    			for(R k=0;k<i;++k)
    				a[k+j+i]+=a[k+j];
    	double ans=0;
    	for(R i=1;i<m;++i)
    		if(a[i]>EPS)ans+=(c[i]?1:-1)/(1-a[i^(m-1)]);
    	return printf("%.10lf
    ",ans),0;
    }
    

    洛谷P4707 重返现世
    套第五个式子,然并卵关键要想到如何DP出容斥系数

    #include<bits/stdc++.h>
    #define LL long long
    #define RG register
    #define R RG int
    #define G if(++ip==ie)if(fread(ip=buf,1,SZ,stdin))
    using namespace std;
    const int SZ=1<<19,N=10009,YL=998244353;
    char buf[SZ],*ie=buf+SZ,*ip=ie-1;
    int f[N][12];LL inv[N];
    inline int in(){
    	G;while(*ip<'-')G;
    	R x=*ip&15;G;
    	while(*ip>'-'){x*=10;x+=*ip&15;G;}
    	return x;
    }
    inline int add(R x,R y){static int z;z=x+y;return z<YL?z:z-YL;}
    inline int sub(R x,R y){static int z;z=x-y;return z<0?z+YL:z;}
    int main(){
    	R n=in(),s=n-in()+1,m=in();
    	f[0][0]=1;
    	for(R i=1;i<=n;++i){
    		R p=in();
    		for(R j=m-p;~j;--j)
    			for(R k=s;k;--k)
    				f[j+p][k]=add(f[j+p][k],sub(f[j][k-1],f[j][k]));
    	}
    	inv[1]=1;LL ans=f[1][s];
    	for(R i=2;i<=m;++i){
    		inv[i]=(YL-YL/i)*inv[YL%i]%YL;
    		ans=(ans+f[i][s]*inv[i])%YL;
    	}
    	cout<<ans*m%YL<<endl;
    	return 0;
    }
    

    子集反演

    感觉就跟高维差分/IFWT一样?
    正好难算,至多好算:(f(S)=sumlimits_{Tsubseteq S}g(T)Rightarrow g(S)=sumlimits_{Tsubseteq S}(-1)^{|S|-|T|}f(T))
    正好难算,至少好算:(f(S)=sumlimits_{Ssubseteq T}g(T)Rightarrow g(S)=sumlimits_{Ssubseteq T}(-1)^{|T|-|S|}f(T))


    还有推广到多重集的子集反演。
    (mu(S)=(-1)^{|S|}[S中没有重复元素])
    (原来莫比乌斯函数就是把整数看作其质因数集合而得来的反演系数啊)
    (f(S)=sumlimits_{Tsubseteq S}g(T)Rightarrow g(S)=sumlimits_{Tsubseteq S}mu(S-T)f(T))
    (f(S)=sumlimits_{Ssubseteq T}g(T)Rightarrow g(S)=sumlimits_{Ssubseteq T}mu(T-S)f(T))


    子集卷积:已知(a,b),求(c)使得(c_S=sumlimits_{Tsubseteq S}a_Tb_{S-T})(注意和高维前缀和不一样!)
    与高维前缀和的区别主要在于集合大小的限制,只有(|T|+|S-T|=|S|)才能产生贡献。
    考虑把数组按集合大小分解,(C_{|S|,S}=sumlimits_{Tsubseteq S}A_{|T|,T}B_{|S-T|,S-T})
    于是枚举(i,j),将(A_i,B_j)的或卷积贡献到(C_{i+j})中。最后把(C)反演回来就是我们要的(c)

    单位根反演

    不会
    洛谷日报——单位根反演
    单位根与其若干应用 by YCB

  • 相关阅读:
    Majority Element
    Longest Increasing Subsequence
    Count Primes
    Valid Parentheses
    Largest Rectangle in Histogram
    Linked List Cycle II
    Linked List Cycle
    Evaluate Reverse Polish Notation
    Longest Valid Parentheses
    适配总结
  • 原文地址:https://www.cnblogs.com/flashhu/p/10324146.html
Copyright © 2020-2023  润新知