首先,要知道欧拉函数是什么!!!
欧拉函数是小于n的数中与n互质(最大公约数为1)的数的数目;
然后,你需要想想
若n是质数p的k次幂,
,因为除了p的倍数外,其他数都跟n互质。
可得
若
则
代码:
int phi(int n) { int i,rea=n; for(i=2;i*i<=n;i++) { if(n%i==0) { rea=rea-rea/i; while(n%i==0) n/=i; } } if(n>1) rea=rea-rea/n; return rea; }
void get_eular(int n) { pnum = 0; memset(prime, true, sizeof(prime)); for(int i = 2; i <= n; i++) { if(prime[i]) { p[pnum ++] = i; phi[i] = i - 1; } for(int j = 0; j < pnum && i * p[j] <= n; j++) { prime[i * p[j]] = false; if(i % p[j] == 0) { phi[i * p[j]] = phi[i] * p[j]; break; } phi[i * p[j]] = phi[i] *phi[p[j]]; } } }
番外:欧拉函数有许多比较重要的相关的数论题;
比如指数循环节。。。。