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

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

#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+5;
const int INF=0x3f3f3f3f;
LL sum[N],phi[N];
int main()
{
    phi[1]=0;
    for(int i=2;i<=N-5;++i){
      if(phi[i])continue;
      for(int j=i;j<=N-5;j+=i){
        if(!phi[j])phi[j]=j;
        phi[j]=phi[j]/i*(i-1);
      }
    }
    for(int i=1;i<=N-5;++i)sum[i]=sum[i-1]+phi[i];
    int n;
    while(~scanf("%d",&n),n){
      LL ans=0;
      for(int i=1;i<=n;++i){
        ans+=(LL)i*sum[n/i];
      }
      printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2016-04-13 18:30  shuguangzw  阅读(138)  评论(0)    收藏  举报