标签:map png img image cloud height sig == void
欧拉函数是少于或等于n的数中与n互质的数的数目
φ(1)=1(定义)
类似与莫比乌斯函数,基于欧拉函数的积性
φ(xy)=φ(x)φ(y)
由唯一分解定理展开显然,得证
精髓在于对于积性的应用:
if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;} phi[i*p[j]]=phi[i]*(p[j]-1);
一个练手题Hdu1286
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <vector> 7 #include <cmath> 8 #include <queue> 9 #include <map> 10 #include <set> 11 using namespace std; 12 #define file(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout) 13 inline void read(int &ans){ 14 ans=0;char x=getchar();int f=0; 15 while(x<‘0‘||x>‘9‘){if(x==‘-‘)f=1;x=getchar();} 16 while(x>=‘0‘&&x<=‘9‘)ans=ans*10+x-‘0‘,x=getchar(); 17 if(f)ans=-ans; 18 } 19 20 const int maxn=50000+10; 21 int phi[maxn],p[maxn],flag[maxn],cnt; 22 void euler(int n){ 23 phi[1]=1; 24 for(int i=2;i<=n;i++){ 25 if(!flag[i])p[++cnt]=i,phi[i]=i-1; 26 for(int j=1;j<=cnt && i*p[j]<=n;j++){ 27 flag[i*p[j]]=1; 28 if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;} 29 phi[i*p[j]]=phi[i]*(p[j]-1); 30 } 31 } 32 } 33 34 int main(){ 35 euler(32768); 36 int CN,N; 37 for(read(CN);CN--;)read(N),printf("%d\n",phi[N]); 38 return 0; 39 }
Hdu1787线性筛O(n),MLE,怎么办?在线算
1 int euler(int n){ 2 int ans=n; 3 for(int i=2;i*i<=n;i++){ 4 if(n%i==0)n/=i,ans-=ans/i; 5 while(n%i==0)n/=i; 6 } 7 if(n>1)ans-=ans/n; 8 return ans; 9 }
AC code:
Hdu2824,sigma(a,b) phi(x)
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cstdio> 6 #include <vector> 7 #include <cmath> 8 #include <queue> 9 #include <map> 10 #include <set> 11 using namespace std; 12 13 typedef long long ll; 14 const int maxn=(int)3e6+10; 15 ll phi[maxn];int p[maxn],cnt;bool flag[maxn]; 16 void euler(int n){ 17 phi[1]=1; 18 for(int i=2;i<=n;i++){ 19 if(!flag[i])p[++cnt]=i,phi[i]=i-1; 20 for(int j=1;j<=cnt && i*p[j]<=n;j++){ 21 flag[i*p[j]]=1; 22 if(i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;} 23 phi[i*p[j]]=phi[i]*(p[j]-1); 24 } 25 } 26 for(int i=1;i<=n;i++)phi[i]+=phi[i-1]; 27 } 28 29 int main(){ 30 euler((int)3e6); 31 for(int a,b;scanf("%d%d",&a,&b)==2;)printf("%lld\n",phi[b]-phi[a-1]); 32 return 0; 33 }
标签:map png img image cloud height sig == void
原文地址:http://www.cnblogs.com/JasonCow/p/6536081.html