ZOJ 1489 HDU1395 2^x mod n = 1 数学

2^x mod n = 1

Time Limit: 2 Seconds      Memory Limit:65536 KB

Give a number n, find the minimum x 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.


Sample Input

2
5


Sample Output

2^? mod 2 = 1
2^4 mod 5 = 1


题目大意:

给你正整数n,求最小的x使得2^x mod n = 1。


1:n=1无解,%后为0。

2:n为偶数无解,(偶%偶)==偶!=1。

3:n为奇数一定有解,对于乘法逆元:在a mod n的操作下,a存在乘法逆元当且仅当a与n互质。(a为偶数,n为奇数,互质)

http://blog.csdn.net/adjky/article/details/70341771 ,大师的代码先阁起,看得懂了再来看。


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int main()
{
	while(cin>>n)
	{
		int k=1,t=2;
		if(n==1||n%2==0) printf("2^? mod %d = 1
",n);	
		else{ 
		  while(t%n!=1){
		    t*=2;
	        k++;
	        t%=n;
		  }
		  printf("2^%d mod %d = 1
",k,n);
	    } 
	}
	return 0;
} 

原文地址:https://www.cnblogs.com/hua-dong/p/7603963.html