HDU 2824 The Euler function

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2824

 解题思路:

打表筛选出一段范围内的欧蓝函数

实现代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=3000001;

int phi[N];

void init(){
    for(int i=1;i<N;i++)
        phi[i]=i;

    for(int i=2;i<N;i++){
        if(i==phi[i])     //此时i是素数
            for(int j=i;j<N;j+=i)
                phi[j]=(phi[j]/i)*(i-1);  //每一个j都有素因子i
    }
}

int main(){
    int a,b;
    init();
    while(cin>>a>>b){
        long long res=0;
        for(int i=a;i<=b;i++)
            res+=phi[i];
        cout<<res<<endl;
    }
}

 

posted on 2017-04-07 20:38  mkfoy  阅读(119)  评论(0编辑  收藏  举报

导航