POJ-1284-Primitive Roots 解题报告

来源:互联网 发布:面板数据和混合数据 编辑:程序博客网 时间:2024/06/06 10:06

       欧拉函数水题。题意:给出原根的定义,求模p的原根的个数。


       我的解题思路:根据原根的性质,模p的原根个数为phi(phi(p)),直接求两次欧拉函数就好。


       我的解题代码:

#include <cstdio>#include <cstdlib>#include <cstring>#include <cctype>#include <cmath>#include <climits>#include <algorithm>using namespace std;int Phi(int x);int main(){    int m;    while (~scanf("%d", &m))    {        printf("%d\n", Phi(Phi(m)));    }    return 0;}int Phi(int x){    int ans = x;    int cnt = (int)sqrt(x + 0.5) + 1;    for (int i=2; i<cnt; ++i)    {        if (x % i == 0)        {            ans -= ans / i;            while (x % i == 0) x /= i;        }    }    if (x > 1) ans -= ans / x;    return ans;}


0 0