当前位置:网站首页>P1390 sum of common divisors (Mobius inversion)
P1390 sum of common divisors (Mobius inversion)
2022-06-11 07:09:00 【qq_ forty-six million five hundred and eighty thousand two hund】
P1390 Sum of common divisors
The question :
Given n, seek
∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) \sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j) i=1∑nj=i+1∑ngcd(i,j)
among gcd(i,j) Express i and j Maximum common divisor of .
Solution:
Law 1 :
∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) = ∑ d = 1 n d ∑ i = 1 n ∑ j = i + 1 n [ g c d ( i , j ) = d ] = ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = d ] − ∑ i = 1 n ∑ j = i i [ g c d ( i , j ) = d ] 2 = ∑ d = 1 n d ∑ i = 1 n ∑ j = 1 n [ g c d ( i d , j d ) = 1 ] − ∑ i = 1 n ∑ j = i i [ g c d ( i d , j d ) = 1 ] 2 = ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 n d [ g c d ( i , j ) = 1 ] − ∑ i = 1 n d ∑ j = i i [ g c d ( i , j ) = 1 ] 2 = ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 n d ∑ k ∣ g c d ( i , j ) μ ( k ) − ∑ i = 1 n d ∑ j = i i ∑ k ∣ g c d ( i , j ) μ ( k ) 2 = ∑ d = 1 n d ∑ k = 1 n d μ ( k ) ⌊ n k d ⌋ 2 − ⌊ n k d ⌋ 2 \sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j) \\ =\sum_{d=1}^{n}d\sum_{i=1}^{n}\sum_{j=i+1}^{n}[gcd(i,j)=d] \\ =\sum_{d=1}^{n}d\frac{\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(i,j)=d]-\sum_{i=1}^{n}\sum_{j=i}^{i}[gcd(i,j)=d]}{2} \\ =\sum_{d=1}^{n}d\frac{\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(\frac{i}{d},\frac{j}{d})=1]-\sum_{i=1}^{n}\sum_{j=i}^{i}[gcd(\frac{i}{d},\frac{j}{d})=1]}{2} \\ =\sum_{d=1}^{n}d\frac{\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{\frac{n}{d}}[gcd(i,j)=1]-\sum_{i=1}^{\frac{n}{d}}\sum_{j=i}^{i}[gcd(i,j)=1]}{2} \\ =\sum_{d=1}^{n}d\frac{\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{\frac{n}{d}}\sum_{k|gcd(i,j)}\mu(k)-\sum_{i=1}^{\frac{n}{d}}\sum_{j=i}^{i}\sum_{k|gcd(i,j)}\mu(k)}{2} \\ =\sum_{d=1}^{n}d\sum_{k=1}^{\frac{n}{d}}\mu(k)\frac{\lfloor\frac{n}{kd}\rfloor^2-\lfloor\frac{n}{kd}\rfloor}{2} i=1∑nj=i+1∑ngcd(i,j)=d=1∑ndi=1∑nj=i+1∑n[gcd(i,j)=d]=d=1∑nd2∑i=1n∑j=1n[gcd(i,j)=d]−∑i=1n∑j=ii[gcd(i,j)=d]=d=1∑nd2∑i=1n∑j=1n[gcd(di,dj)=1]−∑i=1n∑j=ii[gcd(di,dj)=1]=d=1∑nd2∑i=1dn∑j=1dn[gcd(i,j)=1]−∑i=1dn∑j=ii[gcd(i,j)=1]=d=1∑nd2∑i=1dn∑j=1dn∑k∣gcd(i,j)μ(k)−∑i=1dn∑j=ii∑k∣gcd(i,j)μ(k)=d=1∑ndk=1∑dnμ(k)2⌊kdn⌋2−⌊kdn⌋
Law two :
hypothesis f ( d ) = ∑ i = 1 n ∑ j = i + 1 n [ g c d ( i , j ) = d ] f(d)=\sum_{i=1}^{n}\sum_{j=i+1}^{n}[gcd(i,j)=d] f(d)=∑i=1n∑j=i+1n[gcd(i,j)=d]
So according to F ( n ) = ∑ n ∣ d f ( d ) F(n)=\sum_{n|d}f(d) F(n)=∑n∣df(d)
F ( k ) = ∑ k ∣ d f ( d ) = ∑ k ∣ d ∑ i = 1 n ∑ j = i + 1 n [ g c d ( i , j ) = d ] = ∑ i = 1 n ∑ j = i + 1 n [ k ∣ g c d ( i , j ) ] = ∑ i = 1 n ∑ j = 1 n [ k ∣ g c d ( i , j ) ] − ∑ i = 1 n ∑ j = i i [ k ∣ g c d ( i , j ) ] = ⌊ n k ⌋ 2 − ⌊ n k ⌋ 2 F(k)=\sum_{k|d}f(d) \\ =\sum_{k|d}\sum_{i=1}^{n}\sum_{j=i+1}^{n}[gcd(i,j)=d] \\ =\sum_{i=1}^{n}\sum_{j=i+1}^{n}[k|gcd(i,j)] \\ =\sum_{i=1}^{n}\sum_{j=1}^{n}[k|gcd(i,j)]-\sum_{i=1}^{n}\sum_{j=i}^{i}[k|gcd(i,j)] \\ =\frac{\lfloor\frac{n}{k}\rfloor^2-\lfloor\frac{n}{k}\rfloor}{2} F(k)=k∣d∑f(d)=k∣d∑i=1∑nj=i+1∑n[gcd(i,j)=d]=i=1∑nj=i+1∑n[k∣gcd(i,j)]=i=1∑nj=1∑n[k∣gcd(i,j)]−i=1∑nj=i∑i[k∣gcd(i,j)]=2⌊kn⌋2−⌊kn⌋
Then according to f ( d ) = ∑ d ∣ k μ ( k d ) F ( k ) f(d)=\sum_{d|k}\mu(\frac{k}{d})F(k) f(d)=∑d∣kμ(dk)F(k)
f ( d ) = ∑ d ∣ k μ ( k d ) F ( k ) = ∑ d ∣ k μ ( k d ) ⌊ n k ⌋ 2 − ⌊ n k ⌋ 2 f(d)=\sum_{d|k}\mu(\frac{k}{d})F(k) \\ =\sum_{d|k}\mu(\frac{k}{d})\frac{\lfloor\frac{n}{k}\rfloor^2-\lfloor\frac{n}{k}\rfloor}{2} f(d)=d∣k∑μ(dk)F(k)=d∣k∑μ(dk)2⌊kn⌋2−⌊kn⌋
Make k=td
∑ i = 1 n ∑ j = i + 1 n g c d ( i , j ) = ∑ d = 1 n d ∑ i = 1 n ∑ j = i + 1 n [ g c d ( i , j ) = d ] = ∑ d = 1 n d ∑ d ∣ k μ ( k d ) ⌊ n k ⌋ 2 − ⌊ n k ⌋ 2 = ∑ d = 1 n d ∑ t = 1 n d μ ( t ) ⌊ n t d ⌋ 2 − ⌊ n t d ⌋ 2 \sum_{i=1}^{n}\sum_{j=i+1}^{n}gcd(i,j) \\ =\sum_{d=1}^{n}d\sum_{i=1}^{n}\sum_{j=i+1}^{n}[gcd(i,j)=d] \\ =\sum_{d=1}^{n}d\sum_{d|k}\mu(\frac{k}{d})\frac{\lfloor\frac{n}{k}\rfloor^2-\lfloor\frac{n}{k}\rfloor}{2} \\ =\sum_{d=1}^{n}d\sum_{t=1}^{\frac{n}{d}}\mu(t)\frac{\lfloor\frac{n}{td}\rfloor^2-\lfloor\frac{n}{td}\rfloor}{2} i=1∑nj=i+1∑ngcd(i,j)=d=1∑ndi=1∑nj=i+1∑n[gcd(i,j)=d]=d=1∑ndd∣k∑μ(dk)2⌊kn⌋2−⌊kn⌋=d=1∑ndt=1∑dnμ(t)2⌊tdn⌋2−⌊tdn⌋
Finally, set up a number theory and divide it into blocks
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int N=2e6;
int mu[N+5],prime[N+5],notPrime[N+5],cnt=0;
int sum[N];
void initMu()
{
mu[1]=1;
for(int i=2;i<=N;i++)
{
if(!notPrime[i])
{
prime[cnt++]=i;
mu[i]=-1;
}
for(int j=0;j<cnt&&1ll*i*prime[j]<=N;j++)
{
notPrime[i*prime[j]]=1;
if(i%prime[j])mu[i*prime[j]]=-mu[i];
else{
mu[i*prime[j]]=0;break;}
}
}
for(int i=1;i<=N;i++)sum[i]=sum[i-1]+mu[i];
}
int n;
ll res=0;
int main()
{
initMu();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
ll ans=0;
int x=n/i;
for(int l=1,r;l<=x;l=r+1)
{
r=x/(x/l);
ans=ans+1ll*(x/l)*(x/l-1)/2*(sum[r]-sum[l-1]);
}
//cout<<i*ans<<endl;
res+=1ll*i*ans;
}
printf("%lld",res);
return 0;
}
Another Solution
about ∑ d = 1 n d ∑ d ∣ k μ ( k d ) ⌊ n k ⌋ 2 − ⌊ n k ⌋ 2 \sum_{d=1}^{n}d\sum_{d|k}\mu(\frac{k}{d})\frac{\lfloor\frac{n}{k}\rfloor^2-\lfloor\frac{n}{k}\rfloor}{2} ∑d=1nd∑d∣kμ(dk)2⌊kn⌋2−⌊kn⌋, according to i d ∗ μ = φ id*\mu=\varphi id∗μ=φ
∑ d = 1 n ∑ d ∣ k i d ( d ) ∗ μ ( k d ) ⌊ n k ⌋ 2 − ⌊ n k ⌋ 2 = ∑ k = 1 n ∑ d ∣ k i d ( d ) ∗ μ ( k d ) ⌊ n k ⌋ 2 − ⌊ n k ⌋ 2 = ∑ k = 1 n φ ( k ) ⌊ n k ⌋ 2 − ⌊ n k ⌋ 2 \sum_{d=1}^{n}\sum_{d|k}id(d)*\mu(\frac{k}{d})\frac{\lfloor\frac{n}{k}\rfloor^2-\lfloor\frac{n}{k}\rfloor}{2} \\ =\sum_{k=1}^{n}\sum_{d|k}id(d)*\mu(\frac{k}{d})\frac{\lfloor\frac{n}{k}\rfloor^2-\lfloor\frac{n}{k}\rfloor}{2} \\ =\sum_{k=1}^n\varphi(k)\frac{\lfloor\frac{n}{k}\rfloor^2-\lfloor\frac{n}{k}\rfloor}{2} d=1∑nd∣k∑id(d)∗μ(dk)2⌊kn⌋2−⌊kn⌋=k=1∑nd∣k∑id(d)∗μ(dk)2⌊kn⌋2−⌊kn⌋=k=1∑nφ(k)2⌊kn⌋2−⌊kn⌋
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
const int N=2e6;
int phi[N+5],prime[N+5],notPrime[N+5],cnt=0;
ll sum[N];
void initMu()
{
phi[1]=1;
for(int i=2;i<=N;i++)
{
if(!notPrime[i])
{
prime[cnt++]=i;
phi[i]=i-1;
}
for(int j=0;j<cnt&&1ll*i*prime[j]<=N;j++)
{
notPrime[i*prime[j]]=1;
if(i%prime[j])phi[i*prime[j]]=phi[i]*(prime[j]-1);
else{
phi[i*prime[j]]=phi[i]*prime[j];break;}
}
}
for(int i=1;i<=N;i++)sum[i]=sum[i-1]+phi[i];
}
int n;
ll res=0;
int main()
{
initMu();
scanf("%d",&n);
for(int l=1,r;l<=n;l=r+1)
{
r=n/(n/l);
res+=1ll*(n/l)*(n/l-1)/2*(sum[r]-sum[l-1]);
}
printf("%lld",res);
return 0;
}
边栏推荐
- [deploy private warehouse based on harbor] 3 deploy harbor
- The difference between arrow function and ordinary function
- Drawing with qpainter
- Starting from scratch (IV) enemy aircraft flying out of the border disappear automatically
- Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
- Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
- Server parameter adjustment record
- . Net C Foundation (6): namespace - scope with name
- Leetcode-647. Palindromic Substrings
- 421. maximum XOR value of two numbers in the array
猜你喜欢
![[Xunwei dry goods] opencv test of Godson 2k1000 development board](/img/94/312bb1f0d5e8d49506f659ad23cd3a.jpg)
[Xunwei dry goods] opencv test of Godson 2k1000 development board

Object. Specific implementation and difference between create() and new

Web API、DOM

【LeetCode】-- 17.电话号码的字母组合
![[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen](/img/93/d1014b07c924195e062dc83d69b14a.png)
[probability theory and mathematical statistics] Dr. monkey's notes p41-44 statistics related topics, determination of three distributions, properties, statistics subject to normal distribution in gen

Stack -- one of two common linear structures of linear structure

Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation

Education expert Mr. wangzhongze: family education focuses on self growth

matplotlib的cmap

Modular notes
随机推荐
Completed in May, 22
Esp32 learning notes (49) - esp-wifi-mesh interface use
生物序列智能分析平台blog(1)
421. maximum XOR value of two numbers in the array
Records how cookies are carried in cross domain requests
3.1 naming rules of test functions
The gap between the parent box and the child box
[MATLAB image fusion] particle swarm optimization adaptive multispectral image fusion [including source code phase 004]
Shutter restraint container assembly
Leetcode-141. Linked List Cycle
Stack -- one of two common linear structures of linear structure
Required reading 1: the larger the pattern, the better they know how to speak
Whether the ZABBIX monitoring host is online
Comparison of DOM tags of wechat applet development (native and uniapp)
[deploy private warehouse based on harbor] 3 deploy harbor
一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试
Cv2.rectangle() picture frame
Atom, the top stream editor, will leave the historical stage on December 15
337. house raiding III
saltstack的常用模块