Problem Description
Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
Input
One positive integer on each line, the value of n.
Output
If the minimum x exists, print a line with 2^x mod n = 1.
Print 2^? mod n = 1 otherwise.
You should replace x and n with specific numbers.
Print 2^? mod n = 1 otherwise.
You should replace x and n with specific numbers.
Sample Input
2 5
Sample Output
2^? mod 2 = 1 2^4 mod 5 = 1
这道题数据简单,暴力求解可以出来,但真要用思维做,需要用到欧拉函数,在此先不用了
但是注意,当x为1或者偶数时,是绝对不可能实现的,首先pass
其余的进行暴力求解法即可
- #include<stdio.h>
- int main(){
- int x,n,t;
- while(scanf("%d",&n)!=EOF){
- if(n==1 || n%2==0)
- {
-
- printf("2^? mod %d = 1\n",n);
- }
- else
- {
- x=1;
- t=2;
-
- while(t%n!=1){
- x++;
- t=t*2%n; //反之数值过大
- }
- printf("2^%d mod %d = 1\n",x,n);
- }
- }
- return 0;
- }