[模拟] Jzoj P3520 原根

Description
 
Input
有且只有一个正整数m。
Output
以递增序依次输出模m的所有原根,每行输出一个原根。

如果不存在模m的原根,输出-1。
 
Sample Input
7
Sample Output
3
5
 
Data Constraint
50%的数据,m≤ 200。

100%的数据,m ≤ 10000。
 
Hint
样例解释:

 

题解

  • 看懂题目就很容易了,然后数据1000,显然直接暴力做就好了

代码

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 int n,cnt,x,o,k,a[10010];
 5 int gcd(int a, int b) { return a==0?b:gcd(b%a,a); } 
 6 int main()
 7 {
 8     freopen("math.in","r",stdin),freopen("math.out","w",stdout);
 9     scanf("%d",&n);
10     if (n==1) { printf("1"); return 0; }
11     for (int i=1;i<=n;i++) if (gcd(n,i)==1) cnt++,a[cnt]=i;
12     for (int i=1;i<=cnt;i++)
13     {
14         x=o=1;
15         for (int j=1;j<=cnt-1;j++)
16         {
17             (x*=a[i])%=n;
18             if (x==1) { o=0; break; }
19         }
20         if (x*a[i]%n==1&&o) { printf("%d\n",a[i]); k=1; }
21     }
22     if (!k) printf("-1");
23 }

 

转载于:https://www.cnblogs.com/Comfortable/p/9826384.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值