UVA 11426 GCD - Extreme (II) 欧拉函数

时间:2022-09-17 08:45:00

分析:枚举每个数的贡献,欧拉函数筛法

#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=4e6+;
const int INF=0x3f3f3f3f;
LL sum[N],phi[N];
int main()
{
phi[]=;
for(int i=;i<=N-;++i){
if(phi[i])continue;
for(int j=i;j<=N-;j+=i){
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
for(int i=;i<=N-;++i)sum[i]=sum[i-]+phi[i];
int n;
while(~scanf("%d",&n),n){
LL ans=;
for(int i=;i<=n;++i){
ans+=(LL)i*sum[n/i];
}
printf("%lld\n",ans);
}
return ;
}