POJ 2478

使用递推求欧拉函数,因为FN就是欧拉函数的累加和。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>

using namespace std;

const int Max=1000010;
int phi[Max];

int main(){
	for(int i=1;i<Max;i++) phi[i]=i;
	for(int i=2;i<Max;i+=2) phi[i]/=2;
	for(int i=3;i<Max;i+=2){
		if(phi[i]==i){
			for(int j=i;j<Max;j+=i)
			phi[j]=phi[j]/i*(i-1);
		}
	}
	int n;
	while(scanf("%d",&n),n){
		long long ans=0;
		for(int i=2;i<=n;i++)
		ans+=(long long)phi[i];
		printf("%lld
",ans);
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/jie-dcai/p/3964085.html