欧拉函数的计算

来源:互联网 发布:犀牛软件授权码 编辑:程序博客网 时间:2024/05/22 00:17

NOIp要来了,最后Orz一把LRJ.
欧拉函数
欧拉函数,别的计算方法不多证明.直接上代码

#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>using namespace std ;int euler_phi ( int x ) {    int i, j, k, ans = x, lim = (int)sqrt(x+0.5) ;    for ( i = 2 ; i <= lim ; i ++ )         if ( x % i == 0 ) {            ans = ans / i * ( i-1 ) ;            while ( x%i==0 ) x /= i ;        }    if ( x > 1 ) ans = ans / x * (x-1) ;     //如果剩下的x也是原来x的因数    return ans ;}int main() {    int i, j, k, n, m ;    while ( scanf ( "%d", &n ) != EOF )         printf ( "Euler_phi(%d) = %d\n", n, euler_phi(n) ) ;     return 0 ; }
0 0
原创粉丝点击