莫比乌斯反演经典例题(2)

链接:P3327 [SDOI2015]约数个数和 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题意:多组数据给定 n,m,求 ∑ i = 1 n ∑ j = 1 m d ( i ∗ j ) \sum_{i=1}^{n}\sum_{j=1}^{m}d(i*j) i=1nj=1md(ij)。其中 d 函数为约数和个数函数。
题解:

  1. 首先要了解约数和个数的另一表达形式 d ( i ∗ j ) = ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = = 1 ] d(i*j)=\sum_{x|i}\sum_{y|j}[gcd(x,y)==1] d(ij)=xiyj[gcd(x,y)==1]

    证明:对于任意质数因子 p,我们有 p c p^c pc 按先在 i 里取出 p c p^c pc ,如果 i 中最多只能取出 p a p^a pa ,那么在 j 中需取出 p c − a p^{c-a} pca ,对于每一个 c ,其取法是唯一的。对于每一种取法,其得到的 c 也是唯一的。那么可以这么表示,在 i 中取出 p a p^a pa 时表示取出 p a p^a pa ,而在 j 中取出 b 时表示 p a + b p^{a+b} pa+b,a 为 i 中 p 因子最高次幂。因此对于任意质数因子 p ,只会从 i 或者 j 中取出,而不会重叠。因此 g c d ( x , y ) gcd(x,y) gcd(x,y) 为 1 。这样通过枚举 x,y ,可以不重不漏的计算出 d ( i ∗ j ) d(i*j) d(ij) 的所有因数个数。(取自某位大佬,稍加修改)

  2. 从 1 可将原始化为 d ( i ∗ j ) = ∑ i = 1 n ∑ j = 1 m ∑ x ∣ i ∑ y ∣ j [ g c d ( x , y ) = = 1 ] d(i*j)=\sum_{i=1}^{n}\sum_{j=1}^{m}\sum_{x|i}\sum_{y|j}[gcd(x,y)==1] d(ij)=i=1nj=1mxiyj[gcd(x,y)==1]

  3. 对于 2 式,转化为直接枚举 x,y ,有 d ( i ∗ j ) = ∑ i = 1 n ∑ x ∣ i ∑ j = 1 m ∑ y ∣ j [ g c d ( x , y ) = = 1 ] = ∑ x = 1 n ∑ y = 1 m [ g c d ( x , y ) = = 1 ] ⌊ n x ⌋ ⌊ m y ⌋ d(i*j)=\sum_{i=1}^{n}\sum_{x|i}\sum_{j=1}^{m}\sum_{y|j}[gcd(x,y)==1]=\sum_{x=1}^{n}\sum_{y=1}^{m}[gcd(x,y)==1] \lfloor \frac{n}{x} \rfloor \lfloor \frac{m}{y} \rfloor d(ij)=i=1nxij=1myj[gcd(x,y)==1]=x=1ny=1m[gcd(x,y)==1]xnym 。这么化简的原因是对于每个 x 或者 y ,其都会在它的倍数出现时贡献次数。

  4. 此时可以令 f ( z ) = ∑ x = 1 n ∑ y = 1 m [ g c d ( x , y ) = = d ] ⌊ n x ⌋ ⌊ m y ⌋ f(z)=\sum_{x=1}^{n}\sum_{y=1}^{m}[gcd(x,y)==d] \lfloor \frac{n}{x} \rfloor \lfloor \frac{m}{y} \rfloor f(z)=x=1ny=1m[gcd(x,y)==d]xnym,令 F ( z ) = ∑ z ∣ d f ( d ) F(z)=\sum_{z|d}f(d) F(z)=zdf(d)

  5. 对于 F(z) 而言是容易快速求出答案的,因为 F(z) 求的是其 g c d gcd gcd 的倍数,那么 F ( z ) = ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = = k ∗ z ] ⌊ n i ⌋ ⌊ m j ⌋ = ∑ i = 1 n / z ∑ j = 1 m / z [ g c d ( i , j ) = = k ] ⌊ n i ⌋ ⌊ m j ⌋ F(z)=\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)==k*z]\lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor=\sum_{i=1}^{n/z}\sum_{j=1}^{m/z}[gcd(i,j)==k]\lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor F(z)=i=1nj=1m[gcd(i,j)==kz]injm=i=1n/zj=1m/z[gcd(i,j)==k]injm ,其中 k 为任意数,则 g c d gcd gcd 为任意数可直接化去。有 F ( z ) = ∑ i = 1 n / z ∑ j = 1 m / z ⌊ n i ⌋ ⌊ m j ⌋ F(z)=\sum_{i=1}^{n/z}\sum_{j=1}^{m/z}\lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor F(z)=i=1n/zj=1m/zinjm

  6. 对于 5 ,其实还可以化简,由于 ⌊ n i ⌋ \lfloor \frac{n}{i} \rfloor in 与 j 无关,则两个除法满足分配率有 F ( z ) = ( ∑ i = 1 n / z ⌊ n i ⌋ ) ( ∑ j = 1 m / z ⌊ m j ⌋ ) F(z)=(\sum_{i=1}^{n/z}\lfloor \frac{n}{i} \rfloor)(\sum_{j=1}^{m/z}\lfloor \frac{m}{j} \rfloor) F(z)=(i=1n/zin)(j=1m/zjm)

  7. 对于 F ( z ) F(z) F(z) 可以预处理出来,枚举每一个 n ,然后整出分块求出其答案。

  8. 此时回到 4,有反演形式 f ( z ) = ∑ z ∣ d μ ( d z ) F ( d ) f(z)=\sum_{z|d}\mu(\frac{d}{z})F(d) f(z)=zdμ(zd)F(d) 。则所求为 f ( 1 ) = ∑ i = 1 m i n ( n , m ) μ ( i ) F ( i ) f(1)=\sum_{i=1}^{min(n,m)}\mu(i)F(i) f(1)=i=1min(n,m)μ(i)F(i) 。对于某些 i 的取值 ,F 函数值是一样的,则可以整出分块来求答案。总复杂度为 O ( n n + T ( n ) ) O(n\sqrt{n}+T\sqrt(n)) O(nn +T( n))

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<functional>
#include<queue>
#include<unordered_map>
#include<map>
#include<set>

using namespace std;
using ll=long long;
using P=pair<int,int>;
const ll inf=1e18;

struct MobiusInversion{
	static constexpr int maxn=5e4+5;
	int cnt;
	vector<int>pri,vs,sm,mu;
	MobiusInversion(int x=maxn):pri(x+5),vs(x+5),sm(x+5),mu(x+5){
		vs[0]=vs[1]=1,mu[1]=1;
		for(int i=2;i<=x;i++)
		{
			if(!vs[i])pri[++cnt]=i,mu[i]=-1;
			for(int j=1;j<=cnt&&i*pri[j]<=x;j++)
			{
				vs[i*pri[j]]=1;
				if(i%pri[j]==0)break;
				mu[i*pri[j]]=-mu[i];
			}
			mu[i]+=mu[i-1];
		}

		for(int i=1;i<=x;i++)
		{
			for(int l=1,r;l<=i;l=r+1)
			{
				r=i/(i/l);
				sm[i]+=(i/l)*(r-l+1);
			}
		}

	}

	ll getans(int n,int m){
		ll ans=0;
		if(n>m)swap(n,m);
		for(int l=1,r;l<=n;l=r+1)
		{
			r=min(n/(n/l),m/(m/l));
			ans+=1ll*(mu[r]-mu[l-1])*sm[n/l]*sm[m/l];
		}
		return ans;
	}

}tt;

void solve()
{
	int n,m; cin>>n>>m;
	cout<<tt.getans(n,m)<<"\n";
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t; cin>>t;
	while(t--)solve();
	return 0;
} 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值