正题
P1891
题目大意
有T个询问,每个询问给出n,求∑i=1nlcm(i,n)\sum_{i=1}^nlcm(i,n)i=1∑nlcm(i,n)
解题思路
∑i=1nlcm(i,n)\sum_{i=1}^nlcm(i,n)i=1∑nlcm(i,n)
n∑i=1ni/gcd(i,n)n\sum_{i=1}^ni/gcd(i,n)ni=1∑ni/gcd(i,n)
n∑d∣n1d∑i=1ni[gcd(i,n)=d]n\sum_{d|n}\frac{1}{d}\sum_{i=1}^ni\ [gcd(i,n)=d]nd∣n∑d1i=1∑ni [gcd(i,n)=d]
对互质的数求和
n∑d∣n1d×φ(nd)n2n\sum_{d|n}\frac{1}{d}\times \frac{\varphi(\frac{n}{d})n}{2}nd∣n∑d1×2φ(dn)n
n∑d∣nφ(d)×d2n\sum_{d|n}\frac{\varphi(d)\times d}{2}nd∣n∑2φ(d)×d
code
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 1000100
using namespace std;
ll T,n,w,p[N],prime[N],phi[N],g[N];
const ll MX=1e6;
void work()
{phi[1]=1;for(ll i=2;i<=MX;++i){if(!p[i]){prime[++w]=i;phi[i]=i-1;}for(ll j=1;j<=w&&i*prime[j]<=MX;++j){p[i*prime[j]]=1;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(ll i=1;i<=MX;++i)for(ll j=1;i*j<=MX;++j)g[i*j]+=(i==1?1:i*phi[i]/2);//1只有一个数return;
}
int main()
{work();scanf("%lld",&T);while(T--){scanf("%lld",&n);printf("%lld\n",n*g[n]);}return 0;
}