原根相关知识:
http://blog.youkuaiyun.com/zhang20072844/article/details/11541133
——AndyZhang
(感谢原Po写得很详细)
题意:求一个质数 P 的最小原根 (P <= 1e9)
方法见上面链接
AC代码如下:
#include <bits/stdc++.h>
int n, p[100010], tot;
typedef long long LL;
LL poww(LL a, LL b) {
LL ret = 1;
while (b) {
if (b & 1) ret = ret * a % n;
a = a * a % n;
b >>= 1;
}
return ret;
}
bool check(int x) {
int nn = n - 1;
for (int i = 0; i < tot; ++i) {
if (poww(x, nn / p[i]) == 1) return false;
}
return true;
}
void work() {
tot = 0; int nn = n - 1;
for (int i = 2; i * i <= nn; ++i) {
if (nn % i == 0) {
p[tot++] = i;
while (nn % i == 0) nn /= i;
}
}
if (nn > 1) p[tot++] = nn;
for (int i = 1; i < n; ++i) {
if (check(i)) { printf("%d\n", i); return; }
}
}
int main() {
while (scanf("%d", &n) != EOF) work();
return 0;
}