(数论)51NOD 1135 原根

设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
解:使用快速幂的时候小心int爆了。
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #define CLR(x) memset(x,0,sizeof x)
 5 
 6 int num[30];
 7 
 8 int deco(int temp)//分解质因数
 9 {
10     int max = sqrt((double)temp);
11     int j = 0;
12     for (int i = 2; i <= max && temp != 1;i++)
13     {
14         if(temp%i == 0)
15         {
16             num[j++] = i; 
17             while (temp%i == 0) temp /= i;            
18         }
19     }
20     if (temp != 1) num[j] = temp;
21 }
22 
23 int jud(int a,int b)
24 {
25     int p = b + 1;
26     long long a1 = a;
27     for (int i = 0; num[i]; i++)
28     {
29         int c = b / num[i];
30         long long pd = 1;
31         while (c)
32         {
33             if (c & 1) pd = pd * a1 % p;
34             a1 = a1 * a1 % p;
35             c >>= 1;
36         }
37         if (pd == 1) return 1;
38     }
39     return 0;
40 }
41 
42 int main()
43 {
44     int n;
45     while (scanf_s("%d", &n) != EOF)
46     {
47         int temp = n - 1, i;
48         CLR(num);
49         deco(temp);
50         for (i = 2; jud(i, temp); i++);
51         printf("%d\n", i);
52     }
53 }

 

posted @ 2018-10-01 14:34  Ekalos  阅读(172)  评论(0编辑  收藏  举报