SPOJ PROOT Primitive Root (数学找规律)

电玩女神 2022-06-04 03:57 281阅读 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

  1. Input:
  2. 5 2
  3. 3
  4. 4
  5. 7 2
  6. 3
  7. 4
  8. 0 0
  9. Output:
  10. YES
  11. NO
  12. YES
  13. NO

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.

题解:

给一个n,m,m组数据,每组数据的k,表示一个数字,表示以k为底,下标从1到n-1,数值mod n如果不重复,就输出yes,否则no

思路:

打表出所有n-1的因子,假设当前因子之一有个x,看k的x次方mod n是否为1,为1就是no,遍历完还没找到1就是yes

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. #include<stdio.h>
  16. using namespace std;
  17. #define ll long long
  18. ll quick(ll x,ll y,ll m)
  19. {
  20. int ans=1;
  21. while(y!=0)
  22. {
  23. if(y%2==1)
  24. {
  25. ans=(ans*x)%m;
  26. }
  27. x=(x*x)%m;
  28. y/=2;
  29. }
  30. return ans;
  31. }
  32. int ans;
  33. int p[1005];
  34. int main()
  35. {
  36. ll i,j,n,m,d;
  37. while(scanf("%lld%lld",&n,&m)!=EOF)
  38. {
  39. if(n==0&&m==0)
  40. break;
  41. ans=0;
  42. ll t=n-1;
  43. ll temp=sqrt(t);
  44. for(i=2;i<=temp;i++)
  45. {
  46. if(t%i==0)
  47. {
  48. ll s=t/i;
  49. if(s==i)
  50. {
  51. p[ans]=i;
  52. ans++;
  53. }
  54. else
  55. {
  56. p[ans]=i;
  57. p[ans+1]=s;
  58. ans+=2;
  59. }
  60. }
  61. }
  62. while(m--)
  63. {
  64. int tag=1;
  65. scanf("%lld",&d);
  66. for(i=0;i<ans;i++)
  67. {
  68. if(t%p[i]==0)
  69. {
  70. if(quick(d,p[i],n)==1)
  71. {
  72. tag=0;
  73. goto loop;
  74. }
  75. }
  76. }
  77. loop:;
  78. if(tag)
  79. printf("YES\n");
  80. else
  81. printf("NO\n");
  82. }
  83. }
  84. return 0;
  85. }

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读

    相关 292. Nim 游戏【规律

    你和你的朋友,两个人一起玩 Nim 游戏: 桌子上有一堆石头。 你们轮流进行自己的回合,你作为先手。 每一回合,轮到的人拿掉 1 - 3 块石头。 拿掉最后一块石

    相关 蓝桥杯—规律

    2014年第五届第1题 标题:啤酒和饮料 啤酒每罐2.3元,饮料每罐1.9元。小明买了若干啤酒和饮料,一共花了82.3元。 我们还知道他买的啤酒比饮料