其他分享
首页 > 其他分享> > P2568 GCD(莫比乌斯反演)

P2568 GCD(莫比乌斯反演)

作者:互联网

题目传送门
本题题意转化成为:
∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = = p ] \displaystyle\sum_{i=1}^{n}\displaystyle\sum_{j=1}^{n}[ gcd(i,j)==p] i=1∑n​j=1∑n​[gcd(i,j)==p]
这道题用莫比乌斯式子推其实也挺简单的说…………欧拉函数的那个还理解了半天我真菜

s t e p : 1 step:1 step:1提取公因式
∑ p = 1 p ∈ p r i m e , p < = n ∑ i = 1 [ n / p ] ∑ j = 1 [ n / p ] ε ( g c d ( i , j ) ) \displaystyle\sum_{p=1}^{p∈prime,p<=n}\displaystyle\sum_{i=1}^{[n/p]}\displaystyle\sum_{j=1}^{[n/p]}ε (gcd(i,j)) p=1∑p∈prime,p<=n​i=1∑[n/p]​j=1∑[n/p]​ε(gcd(i,j))

s t e p : 2 step:2 step:2利用迪利克雷卷积进行反演
∑ p = 1 p ∈ p r i m e , p < = n ∑ i = 1 [ n / p ] ∑ j = 1 [ n / p ] ∑ k ∣ i , k ∣ j μ ( k ) \displaystyle\sum_{p=1}^{p∈prime,p<=n}\displaystyle\sum_{i=1}^{[n/p]}\displaystyle\sum_{j=1}^{[n/p]}\displaystyle\sum_{k|i,k|j}^{}μ(k) p=1∑p∈prime,p<=n​i=1∑[n/p]​j=1∑[n/p]​k∣i,k∣j∑​μ(k)
s t e p : 3 step:3 step:3交换枚举顺序
∑ p = 1 p ∈ p r i m e , p < = n ∑ k = 1 [ n / p ] ∑ i = 1 [ n / ( p ∗ k ) ] ∑ j = 1 [ n / ( p ∗ k ) ] μ ( k ) \displaystyle\sum_{p=1}^{p∈prime,p<=n}\displaystyle\sum_{k=1}^{[n/p]}\displaystyle\sum_{i=1}^{[n/(p*k)]}\displaystyle\sum_{j=1}^{[n/(p*k)]}μ(k) p=1∑p∈prime,p<=n​k=1∑[n/p]​i=1∑[n/(p∗k)]​j=1∑[n/(p∗k)]​μ(k)
∑ p = 1 p ∈ p r i m e , p < = n ∑ k = 1 [ n / p ] μ ( k ) ∗ ( n / ( p ∗ k ) ) 2 \displaystyle\sum_{p=1}^{p∈prime,p<=n}\displaystyle\sum_{k=1}^{[n/p]}μ(k)*(n/(p*k))^2 p=1∑p∈prime,p<=n​k=1∑[n/p]​μ(k)∗(n/(p∗k))2
然后利用我们的整除分块就可以愉快的实现我们的代码了

#include<iostream>
#include<vector> 
using namespace std;
typedef long long ll;
const ll MAXN=1e7+10;
//莫比乌斯函数
ll mu[MAXN];
bool isnp[MAXN];
vector<ll> primes;
void init(ll n)
{
    mu[1] = 1;
    for (ll i = 2; i <= n; i++)
    {
        if (!isnp[i])
            primes.push_back(i), mu[i] = -1; // 质数为-1
        for (ll p : primes)
        {
            if (p * i > n)
                break;
            isnp[p * i] = 1;
            if (i % p == 0)
            {
                mu[p * i] = 0; // 有平方因数为0
                break;
            }
            else
                mu[p * i] = mu[p] * mu[i]; // 互质,利用积性函数性质
        }
    }
    for(ll i=1;i<=n;i++){
    	mu[i]+=mu[i-1];
	}

}
int main(){
	ll n,ans=0;
	init(10000000);
	cin>>n;
	for(ll i=0;primes[i]<=n;i++){
		//cout<<primes[i]<<endl;
		ll u=n/primes[i];
		ll l=1,r;
		for(ll l=1,r;l<=u;l=r+1){
			r=u/(u/l);
			ans+=(mu[r]-mu[l-1])*(u/l)*(u/l);
		}
	}
	cout<<ans<<endl;
}

 

标签:mu,GCD,ll,P2568,反演,step,MAXN,isnp,primes
来源: https://blog.csdn.net/m0_51841071/article/details/116399741