##Description
Given n, calculate the sum LCM(1,n) + LCM(2,n) + … + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.
给出n,计算LCM(1,n)+LCM(2,n)+…+LCM(n,n)的和,LCM(i,n)是i和n的最小公倍数。
##Input
The first line contains T the number of test cases. Each of the next T lines contain an integer n.
第一行输入T表示数据组数,接下来T行每行一个数为n。
##Output
Output T lines, one for each test case, containing the required sum.
输出T行,每行为最小公倍数的和
##Sample Input
3
1
2
5
##Sample Output
1
4
55
##Constraints
1 <= T <= 300000
1 <= n <= 1000000
##Solution
对于每个n对于每个n对于每个n
Ans=∑i=1nlcm(i,n)Ans=\sum_{i=1}^nlcm(i,n)Ans=∑i=1nlcm(i,n)
Ans=∑i=1nnigcd(i,n)Ans=\sum_{i=1}^n\frac{ni}{gcd(i,n)}Ans=∑i=1ngcd(i,n)ni
Ans=n∑i=1nigcd(i,n)Ans=n\sum_{i=1}^n\frac{i}{gcd(i,n)}Ans=n∑i=1ngcd(i,n)i
Ans=n∑d∣n∑i=1nid[gcd(i,nd)=d]Ans=n\sum_{d|n}\sum_{i=1}^{n}\frac{i}{d}[gcd(i,\frac{n}{d})=d]Ans=n∑d∣n∑i=1ndi[gcd(i,dn)=d]
Ans=n∑d∣n∑i=1di[gcd(i,d)=1]Ans=n\sum_{d|n}\sum_{i=1}^{d}i[gcd(i,d)=1]Ans=n∑d∣n∑i=1di[gcd(i,d)=1]
设f(d)=∑i=1di[gcd(i,d)=1],则Ans=n∑d∣nf(d)设f(d)=\sum_{i=1}^{d}i[gcd(i,d)=1],则Ans=n\sum_{d|n}f(d)设f(d)=∑i=1di[gcd(i,d)=1],则Ans=n∑d∣nf(d)
f(d)即1 d中与d互质的数之和,f(d)=ϕ(d)∗d2f(d)即1~d中与d互质的数之和,f(d)=\frac{\phi(d)*d}{2}f(d)即1 d中与d互质的数之和,f(d)=2ϕ(d)∗d
所以,Ans=n∑d∣nϕ(d)∗d2所以,Ans=n\sum_{d|n}\frac{\phi(d)*d}{2}所以,Ans=n∑d∣n2ϕ(d)∗d
ϕ(i)可以用线性筛解决,可以O(nlog2n)预处理出所有答案,要记得开longlong\phi(i)可以用线性筛解决,可以O(nlog_2^n)预处理出所有答案,要记得开long longϕ(i)可以用线性筛解决,可以O(nlog2n)预处理出所有答案,要记得开longlong
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 1000010
#define M 1000000
#define LL long long
LL phi[N],t[N],p[N],tt,n;
LL ans[N];
void pre()
{
memset(t,0,sizeof(t));
phi[1]=1;
for (int i=2;i<=M;i++)
{
if (t[i]==0)
{
phi[i]=i-1;
p[++p[0]]=i;
}
for (int j=1;j<=p[0] && p[j]*i<=M;j++)
{
t[i*p[j]]=1;
if (i%p[j]==0)
{
phi[i*p[j]]=phi[i]*p[j];
break;
}
else phi[i*p[j]]=phi[i]*phi[p[j]];
}
}
memset(ans,0,sizeof(ans));
LL tot;
for (int i=1;i<=M;i++)
{
if (i==1) tot=1;
else tot=phi[i]*i/2;
for (int j=i;j<=M;j+=i)
ans[j]+=tot;
}
}
int main()
{
pre();
scanf("%lld",&tt);
for (int ii=1;ii<=tt;ii++)
{
scanf("%lld",&n);
printf("%lld\n",ans[n]*n);
}
return 0;
}