Contents
  1. 1. 题目:

题目:

输入n,2的x次方%n=1,求x,如果无解输出-1,x属于正整数。

  • 分析:如果n为偶数则无解
  • 2^x % n = 1等价于2^x = 1 (mod n);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <cstdio>

int main()
{

int n, x, ans;
while(scanf("%d", &n) != EOF){
if(n % 2 == 0){
printf("-1\n");
continue;
}
ans = 1;
for(x = 1; ; x++){
ans = (ans * 2) % n;
if(ans == 1){
printf("%d\n", x);
break;
}
}
}
return 0;
}
Contents
  1. 1. 题目: