Primitive Root 原根
来源:互联网 发布:清华直博生待遇 知乎 编辑:程序博客网 时间:2024/06/06 14:16
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <queue>#include <vector>#include <stack>#include <algorithm>using namespace std;int a[10000];int mod,cnt;void pr(int p){ a[0]=1;cnt=0; for(int i=2;i<=(int)sqrt(p);i++) { if(p%i==0) { a[++cnt]=i; a[++cnt]=p/i; } }}ll powmod(ll x,ll n){ if(n==0) return 1%mod; ll temp=powmod(x,n>>1); temp=temp*temp%mod; if(n&1) temp=temp*x%mod; return temp;}int main(){ int n; while(scanf("%d%d",&mod,&n)&&mod||n) { int c; for(int i=1;i<=n;i++) { bool tag=false; memset(a,0,sizeof(a)); scanf("%d",&c); pr(mod-1); for(int j=0;j<=cnt;j++) { if(powmod(c,a[j])==1) { tag=true; break; } } if(powmod(c,mod-1)!=1) tag=false; if(tag) printf("NO\n"); else printf("YES\n"); } } return 0;}
In the field of Cryptography, prime numbers play an important role. We are interested in a scheme called "Diffie-Hellman" key exchange which allows two communicating parties to exchange a secret key. This method requires a prime number p and r which is a primitive root of p to be publicly known. For a prime number p, r is a primitive root if and only if it's exponents r, r2, r3, ... , rp-1 are distinct (mod p).
Cryptography Experts Group (CEG) is trying to develop such a system. They want to have a list of prime numbers and their primitive roots. You are going to write a program to help them. Given a prime number p and another integer r < p , you need to tell whether r is a primitive root of p.
Input
There will be multiple test cases. Each test case starts with two integers p ( p < 2 31 ) and n (1 ≤ n ≤ 100 ) separated by a space on a single line. p is the prime number we want to use and n is the number of candidates we need to check. Then n lines follow each containing a single integer to check. An empty line follows each test case and the end of test cases is indicated by p=0 and n=0 and it should not be processed. The number of test cases is atmost 60.
Output
For each test case print "YES" (quotes for clarity) if r is a primitive root of p and "NO" (again quotes for clarity) otherwise.
Example
Input:5 2347 2340 0Output:YESNOYESNO
Explanation
In the first test case 31, 32 , 33 and 34 are respectively 3, 4, 2 and 1 (mod 5). So, 3 is a primitive root of 5.
41, 42 , 43 and 44 are respectively 4, 1, 4 and 1 respectively. So, 4 is not a primitive root of 5.
- Primitive Root 原根
- 原根(Primitive Root)
- SPOJ PROOT Primitive Root (关于原根的一点结论)
- poj1284 Primitive Roots 【原根】
- POJ1284 Primitive Roots【原根】
- Primitive root
- POJ 1284 Primitive Roots(素数原根)
- poj1284 Primitive Roots 原根个数
- Primitive Roots +欧拉函数+原根
- poj 1284 Primitive Roots 【原根】【数论】
- POJ 1284 Primitive Roots 原根
- POJ 1284 Primitive Roots (原根)
- POJ 1284 Primitive Roots 原根个数
- POJ 1284 Primitive Roots 原根
- POJ1284 Primitive Roots(原根定理)
- POJ 1284 Primitive Roots (原根,欧拉函数)
- POJ 1284 Primitive Roots 原根(欧拉函数)
- POJ 1284 Primitive Roots (欧拉函数&原根定理)
- Set Matrix Zeroes
- HashMap的一些原理(同时分析源码,英文)
- Ice-3.5.1在CentOS 6.5系统中的编译配置教程
- 安装icephp 记
- 线程范围内共享数据
- Primitive Root 原根
- Linux下安装ICE(自测)
- 17.12.4日报
- hxb 2017 部分题解
- Nginx的安装目录详解
- php变量定义和输出
- 在命令行下,如何使用JAD反编译jar文件
- 回溯进阶---迭代深搜IDA*---埃及分数
- 用c语言创建一个窗口