用欧拉phi函数值算出每个数与他互素的整数的个数。
#include<bits/stdc++.h> using namespace std; int solve(int n,int* phi){ for(int i=2;i<=n;i++) phi[i] = 0; phi[1] = 1; int max_phi=0; for(int i=2;i<=n;i++) if(!phi[i]) for(int j=i;j<=n;j+=i) { if(!phi[j]) phi[j]=j; phi[j] = phi[j] / i * (i-1); } for(int i=2;i<=n;i++) max_phi+=phi[i]; return max_phi; } int phi[50005],n; int main() { while(scanf("%d",&n)!=EOF&&n) { printf("%d\n",2*solve(n,phi)+1); } return 0; }
原文:http://blog.csdn.net/weizhuwyzc000/article/details/44593039