标签:快速幂 return gray 等于 code eof math set ble
输入1个质数P(3 <= P <= 10^9)
输出P最小的原根。
3
2
解:使用快速幂的时候小心int爆了。
1 #include <stdio.h> 2 #include <math.h> 3 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 }
标签:快速幂 return gray 等于 code eof math set ble
原文地址:https://www.cnblogs.com/Ekalos-blog/p/9734661.html