数论原根 及其求法
来源:互联网 发布:淘宝账号密码破解 编辑:程序博客网 时间:2024/06/05 03:24
定义2:若G为n的原根,则当gcd(i,φ(n))==1,Gi也为n的原根
可以通过先求出n的最小原根,来枚举得到其他的合法原根。
#include <algorithm>#include <cstring>#include <cstdio>#define ll long longusing namespace std;const int maxn = 1e6 + 10;bool vis[maxn] = {0};int pri[maxn / 10] = {0};int phi[maxn] = {0};int n,k;int temp[maxn] = {0};int cnt = 0;ll res[maxn] = {0};void init () { for (int i = 2;i < maxn; ++ i) { if (!vis[i]) { pri[cnt ++] = i; for (int j = i + i;j < maxn;j += i) { vis[j] = 1; } } } phi[1] = 1; memset (vis,0,sizeof (vis)); for (int i = 2;i < maxn; ++ i) { if (!vis[i]) { for (int j = i;j < maxn; j += i) { vis[j] = 1; if (phi[j] == 0) phi[j] = j; phi[j] = phi[j] / i * (i - 1); } } }}void div (int n) { k = 0; for (int i = 0;pri[i] * pri[i] <= n; ++ i) { if (n % pri[i] == 0) { temp[k ++] = pri[i]; while (n % pri[i] == 0) n /= pri[i]; } } if (n > 1) temp[k ++] = n;}ll lpow (ll a,ll b,ll m) { ll ans = 1; a %= m; while (b) { if (b & 1) { ans = ans * a % m; b --; } b >>= 1; a = a * a % m; } return ans % m;}bool check (int n) { if (n == 2 || n == 4) return true; if (n % 2 == 0) n /= 2; if (binary_search (pri + 1,pri + cnt,n)) return true; for (int i = 1;i < cnt && pri[i] * pri[i] <= n; ++ i) { if (n % pri[i] == 0) { while (n % pri[i] == 0) { n /= pri[i]; } if (n == 1)return true; break; } } return false;}int main () { init (); while (scanf ("%d",&n) != EOF) { if (n == 2) { printf ("1\n"); continue ; } if (check(n) == 0) { printf ("-1\n"); continue; } int tot = 0; int p = phi[n]; div (p); for (int q = 2;q < n; ++ q) { if (__gcd (q,n) != 1) continue; int flag = 1; for (int i = 0;i < k; ++ i) { int t = p / temp[i]; if (lpow(q, t, n) % n == 1) { flag = 0; break; } } if(flag) { res[tot ++] = q; break; } } ll cc = res[0]; tot = 0; for (int i = 1;i < p; ++ i) { if (__gcd (i,p) == 1) { res[tot ++] = cc; } cc = cc * res[0] % n; } sort (res,res + tot); if (tot) { printf ("%lld",res[0]); for (int i = 1;i < tot; ++ i) { printf (" %lld",res[i]); } } else { printf ("-1"); } printf ("\n"); } return 0;}
阅读全文
0 0
- 数论原根 及其求法
- 数论之原根
- poj 1284 Primitive Roots 【原根】【数论】
- 51nod--1135 原根 (数论)
- 51nod 1135 原根 (数论)
- 【51NOD1135】原根(数论)
- 51nod 1135 原根 (数论)
- 欧拉函数及其求法
- 多元函数极值及其求法
- 欧拉函数及其求法
- 数论之因子个数的求法
- 数论之因子个数的求法
- 数论之因子个数的求法
- 组合数简单求法(数论)
- 数论之因子个数的求法
- DH(Diffie-Hellman)算法本元根(原根)求法
- [阶 原根 指标]BZOJ 2219 数论之神
- [POJ 1284] Primitive Roots (数论,原根)
- Hibernate学习之映射文件
- # NOIP2014 飞翔的小鸟 【DP】
- Python 2.x中常见字符编码和解码方面的错误及其解决办法
- Redis集群配置-配置过程中出现的问题及解决方案
- 结构体
- 数论原根 及其求法
- gulp.watch
- python 练习
- oracle 杀死进程
- 联系人索引字母条
- 论文笔记:Training Region-based Object Detectors with Online Hard Example Mining
- [搜索]ElasticSearch Java Api(一)
- WebView错误(待完成)
- 01背包(回溯法)