51nodoj 1135 (原根)

来源:互联网 发布:人工智能有关英语作文 编辑:程序博客网 时间:2024/06/04 20:08

1135 原根
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注
设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根。(其中φ(m)表示m的欧拉函数)
给出1个质数P,找出P最小的原根。
Input
输入1个质数P(3 <= P <= 10^9)
Output
输出P最小的原根。
Input示例
3
Output示例
2

推荐大神博客好了, 自己不会写,也不算太懂这里写链接内容

#include <cstdio>#include <cstring>#define M 10010#define LL long longint prime[M], cnt;void getprime(int n){    cnt = 1;    for(int i=2; i<=n; i++)    {        if(n % i == 0)        {            prime[cnt++] = i;            while(n % i == 0)            {                n /= i;            }        }    }    if(n > 1)    {        prime[cnt++] = n;    }}LL quickpow(LL n, LL m, LL mod){    n %= mod;    LL res = 1;    while(m)    {        if(m & 1)        {            res = res * n % mod;        }        n = n * n % mod;        m >>= 1;    }    return res;}int main(){    LL n;    bool ok;    while(scanf("%lld", &n) != EOF)    {        getprime(n-1);//求出 n-1 的所有的质因数        for(LL i=2; i<=n; i++)        {            ok = 1;            for(LL j=1; j<cnt; j++)            {                if(quickpow(i, (n-1)/prime[j], n) == 1)                //快速幂,判断是否有不符合的                {                    ok = 0;                    break;                }            }            if(ok)//有成立的就直接输出            {                printf("%lld\n", i);                break;            }        }    }    return 0;}
0 0
原创粉丝点击