bzoj2226: [Spoj 5971] LCMSum

题解

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000002;
int fl[N],prime[N],phi[N],n,i,j,tot,T;
ll ans[N],tmp[N];
int main(){
    tmp[1]=1;
    for (i=2;i<N;i++){
        if (!fl[i]) prime[++tot]=i,phi[i]=i-1;
        for (j=1;i*prime[j]<N;j++){
            fl[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);
        }
        tmp[i]=(ll)phi[i]*i>>1;
    }
    for (i=1;i<N;i++)
        for (j=i;j<N;j+=i) ans[j]+=tmp[i];
    scanf("%d",&T);
    while (T--) scanf("%d",&n),printf("%lld\n",ans[n]*n);
}

猜你喜欢

转载自blog.csdn.net/xumingyang0/article/details/80674280