显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=4000005;
void phi_table(int);
bool notp[maxn];
long long sp[maxn],s[maxn];
int n,phi[maxn],prime[maxn/10];
int main(){
freopen("gcd_extreme.in","r",stdin);
freopen("gcd_extreme.out","w",stdout);
phi_table(4000000);
while(scanf("%d",&n)==1&&n){
long long ans=0;
for(int i=1,last;i<=n;i=last+1){
last=n/(n/i);
ans+=(s[last]-s[i-1])*sp[n/i];
}
printf("%lld\n",ans);
}
return 0;
}
void phi_table(int n){
for(int i=2;i<=n;i++){
if(!notp[i]){
prime[++prime[0]]=i;
phi[i]=i-1;
}
for(int j=1;j<=prime[0]&&i*prime[j]<=n;j++){
notp[i*prime[j]]=true;
if(i%prime[j])phi[i*prime[j]]=phi[i]*(prime[j]-1);
else{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
}
}
for(int i=1;i<=n;i++){
s[i]=s[i-1]+i;
sp[i]=sp[i-1]+phi[i];
}
}