HDU1286 欧拉函数

来源:互联网 发布:如何学游戏编程 编辑:程序博客网 时间:2024/05/18 15:24

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286

通项:φ(x) = x * ( 1 - 1 / p1 ) *( 1 - 1 / p2 ) * ( 1 - 1 / p3 ) ...... ( 1 - 1 / pn ),其中p为x的质因子

code:

#include<cstdio>#include<cmath>using namespace std;int phi(int n){    int res=n;    for(int i=2;i<=sqrt(n+0.5);i++)    {        if(n%i==0)        {            res=res/i*(i-1);            while(n%i==0)                n/=i;        }    }    if(n>1)    res=res/n*(n-1);    return res;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        scanf("%d",&n);        printf("%d\n",phi(n));    }    return 0;}

0 0