题意: 求 ∑ i = 1 n ∑ j = 1 n g c d ( i , j ) \sum_{i=1}^{n} \sum_{j=1}^{n} gcd\left ( i,j \right ) i=1∑nj=1∑ngcd(i,j)
我们直接枚举 g c d gcd gcd。
∑ i = 1 n ∑ j = 1 n g c d ( i , j ) = ∑ d = 1 n ∑ i = 1 n ∑ j = 1 n [ g c d ( i , j ) = = d ] ⋅ d = ∑ d = 1 n ∑ i = 1 n d ∑ j = 1 n d [ g c d ( i , j ) = = 1 ] ⋅ d = ∑ d = 1 n d ∑ i = 1 n d ∑ j = 1 n d ∑ k ∣ g c d ( i , j ) μ ( k ) = ∑ d = 1 n d ∑ k = 1 n d ∑ k ∣ i i ≤ n d ∑ k ∣ j j ≤ n d μ ( k ) = ∑ d = 1 n d ∑ k = 1 n d μ ( k ) ⋅ ( n k d ) 2 \sum_{i=1}^{n} \sum_{j=1}^{n} gcd\left ( i,j \right ) \\ =\sum_{d=1}^{n} \sum_{i=1}^{n} \sum_{j=1}^{n} \left [gcd\left ( i,j \right ) == d \right ]\cdot d \\ = \sum_{d=1}^{n} \sum_{i=1}^{\frac{n}{d}} \sum_{j=1}^{\frac{n}{d}} \left [gcd\left ( i,j \right ) == 1 \right ]\cdot d \\ = \sum_{d=1}^{n} d\sum_{i=1}^{\frac{n}{d}} \sum_{j=1}^{\frac{n}{d}} \sum_{k|gcd(i,j)}\mu\left(k\right)\\ = \sum_{d=1}^{n} d\sum_{k = 1}^{\frac{n}{d}}\sum_{k|i}^{i \le \frac{n}{d}} \sum_{k|j}^{ j \le \frac{n}{d}} \mu\left(k\right) \\ = \sum_{d=1}^{n} d\sum_{k =1}^{\frac{n}{d}}\mu(k)\cdot\left( \frac{n}{kd}\right)^2 \\ i=1∑nj=1∑ngcd(i,j)=d=1∑ni=1∑nj=1∑n[gcd(i,j)==d]⋅d=d=1∑ni=1∑dnj=1∑dn[gcd(i,j)==1]⋅d=d=1∑ndi=1∑dnj=1∑dnk∣gcd(i,j)∑μ(k)=d=1∑ndk=1∑dnk∣i∑i≤dnk∣j∑j≤dnμ(k)=d=1∑ndk=1∑dnμ(k)⋅(kdn)2
这里我们由套路设 T = k d T=kd T=kd ,那么原式可以继续化简:
∑ d = 1 n d ∑ k = 1 n d μ ( k ) ⋅ ( n k d ) 2 = ∑ d = 1 n ∑ d ∣ T T ≤ n μ ( T d ) ⋅ ( n T ) 2 = ∑ T = 1 n ( n T ) 2 ( ∑ d ∣ T d μ ( T d ) ) \sum_{d=1}^{n} d\sum_{k =1}^{\frac{n}{d}}\mu(k)\cdot\left( \frac{n}{kd}\right)^2\\ =\sum_{d=1}^{n}\sum_{d|T}^{T\le n} \mu\left(\frac{T}{d}\right)\cdot\left ( \frac{n}{T} \right ) ^2\\ =\sum_{T=1}^{n} \left ( \frac{n}{T} \right ) ^2\left(\sum_{d|T}d\mu\left(\frac{T}{d}\right)\right) d=1∑ndk=1∑dnμ(k)⋅(kdn)2=d=1∑nd∣T∑T≤nμ(dT)⋅(Tn)2=T=1∑n(Tn)2⎝⎛d∣T∑dμ(dT)⎠⎞
而 ∑ d ∣ T d μ ( T d ) = d ∗ μ = φ \sum_{d|T}d\mu\left(\frac{T}{d}\right)=d*\mu = \varphi d∣T∑dμ(dT)=d∗μ=φ
其中, ∗ * ∗表示卷积, f ∗ g = ∑ d ∣ n f ( d ) g ( n d ) f*g= \sum_{d|n}f\left(d\right)g\left(\frac{n}{d}\right) f∗g=∑d∣nf(d)g(dn)。
所以原式简化为 ∑ T = 1 n ( n T ) 2 φ ( T ) \sum_{T=1}^{n} \left(\frac{n}{T}\right)^2\varphi(T) T=1∑n(Tn)2φ(T)
前面的可以用数论分块做,然后预处理一波 φ \varphi φ的前缀和,总的时间复杂度 O ( n ) O(n) O(n)。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define pii pair
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1.0);
const int maxn = 1e5 + 10;
bool isp[maxn];
int cnt,p[maxn];
int phi[maxn];
void getp(){
//线性筛求欧拉函数
isp[1] = phi[1] = 1;
for(int i = 2; i <= 1e5; i++){
if(!isp[i]){
p[++cnt] = i;
phi[i] = i - 1;
}
for(int j = 1; j <= cnt && i * p[j] <= 1e5; j++){
isp[i*p[j]] = 1;
if(i % p[j] != 0){
phi[i * p[j]] = (p[j] - 1) * phi[i];
}
else{
phi[i * p[j]] = p[j] * phi[i];
break;
}
}
}
for(int i = 1; i <= 1e5; i++) phi[i] = phi[i-1] + phi[i];
}
void solve(){
getp();
ll n;
scanf("%lld",&n);
ll ans = 0, l = 1, r = 0;
while(r < n){
//数论分块
r = n/(n/l);
ans += (phi[r] - phi[l-1])*1ll*(n/l)*(n/l);
l = r + 1;
}
printf("%lld\n",ans);
}
int main()
{
solve();
return 0;
}
对于 g c d = k gcd = k gcd=k,如果 g c d ( i , j ) = 1 gcd(i,j)=1 gcd(i,j)=1,那么 g c d ( k i , k j ) = k gcd(ki,kj) = k gcd(ki,kj)=k,即 k k k的个数和 ( i , j ) (i,j) (i,j)一一对应。所以产生的总贡献为 2 ∑ d = 1 n k φ ( d ) − 1 2\sum_{d = 1}^{\frac{n}{k}}\varphi(d) - 1 2d=1∑knφ(d)−1
预处理出 φ \varphi φ的前缀和,然后遍历一波k即可。时间复杂度 O ( n ) O(n) O(n)。
代码懒得写了QAQ,直接抄题解了
#include
#define LL long long
const int N=100050;
int prime[N],cnt=0,phi[N];
LL n,sum[N],ans=0;
void init()
{
phi[1]=1;
for(int i=2;i<=n;i++)
{
if (!phi[i]) prime[++cnt]=i,phi[i]=i-1;
for(int j=1;j<=cnt;j++)
{
if (prime[j]*i>n) break;
if (i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+phi[i];
}
int main()
{
scanf("%lld",&n);
init();
for(LL i=1;i<=n;i++)
ans+=(sum[n/i]*2-1)*i;
printf("%lld",ans);
return 0;
}