原题
求(sum^n_{i=1}lcm(i,n))
(sum^n_{i=1}lcm(i,n))
(=sum^n_{i=1}frac{n*i}{gcd(i,n)})
(=n*sum_{d|n}sum_{iin[1,n],gcd(i,n)=d}frac{i}{d})
(=n*sum_{d|n}sum_{iin[1,n/d],gcd(i,n/d)=1}i)
根据欧拉函数的性质,n>1时,(sum_{iin[1,n]gcd(i,n)=1}i=frac{phi(n)*n}{2})
所以答案为(n*sum_{d|n}frac{phi(d)*d}{2})
预处理一个欧拉函数就好了(不然会TLE哦)
#include<cstdio>
#include<cmath>
#define N 1000010
typedef long long ll;
using namespace std;
int t,n,m,phi[N],p[N];
bool f[N];
ll read()
{
ll ans=0,fu=1;
char j=getchar();
for (;j<'0' || j>'9';j=getchar()) if (j=='-') fu=-1;
for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
return ans*fu;
}
void init(ll x)
{
phi[1]=1;
for (int i=2;i<=x;i++)
{
if (!f[i]) phi[i]=i-1,p[++p[0]]=i;
for (int j=1;j<=p[0] && p[j]*i<=x;j++)
{
f[p[j]*i]=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]];
}
}
}
ll work(int x) { if (x==1) return 1LL; return (ll)x*phi[x]/2; }
int main()
{
init(1000000);
t=read();
while(t--)
{
n=read();
m=sqrt(n);
ll ans=0;
for (int i=1;i<=m;i++)
if (n%i==0)
ans+=work(i),ans+=work(n/i);
if (m*m==n) ans-=work(m);
printf("%lld
",ans*n);
}
return 0;
}