筛法求欧拉函数(poj2478
求1~n的欧拉函数的值
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) using namespace std; long long n; long long a[N]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } int main(){ a[1]=1; For(i,2,1e6) if(!a[i]) for(long long j=i;j<=1e6;j+=i){ if(!a[j]) a[j]=j; a[j]=a[j]/i*(i-1); } For(i,1,1e6) a[i]+=a[i-1]; while(cin>>n&&n){ o(a[n]-1);p('\n'); } return 0; }