【POJ3090】Visible Lattice Points

题目大意:求 $$sumlimits_{i=2}^nvarphi(i)$$

题解:利用与埃筛类似的操作,可在 (O(nlogn)) 时间求出结果。

代码如下

#include <cstdio>
using namespace std;
const int maxn=3010;

int kase,n,phi[maxn];

int main(){
	int T;scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=2;i<=n;i++)phi[i]=i;
		for(int i=2;i<=n;i++)if(i==phi[i])
			for(int j=i;j<=n;j+=i)
				phi[j]=phi[j]/i*(i-1);
		long long ans=3;
		for(int i=2;i<=n;i++)ans+=(phi[i]<<1);
		printf("%d %d %lld
",++kase,n,ans);
	}
	return 0;
}
原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10549349.html