POJ-1284-Primitive Roots 解题报告

       欧拉函数水题。题意:给出原根的定义,求模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;
}


猜你喜欢

转载自blog.csdn.net/JZQT_T/article/details/44590727
今日推荐