The principle is a ^ B % n = d; >>>>>> (a % n) × ........ * (a % n) % n = d
Then when n = 1 or n % 2 = 0, d must be 0, so there is no solution at this time;
When n is another value, it must be 1 ~ The remainder of n-1 exists. Therefore, use the method for solving a ^ B % n = d.
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<bitset> #include<iomanip> using namespace std; int main() { int a , b , n ; while( ~scanf( "%d" ,&n ) ) { if( n == 1 || n % 2 == 0 ) { printf( "2^? mod %d = 1\n" , n ); } else { b = 1 ; int temp = 2 ; while( temp != 1 ) { b++ ; temp = ( temp * 2 ) % n ; } printf( "2^%d mod %d = 1\n" , b , n ) ; } } return 0 ; } #include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<bitset>#include<iomanip>using namespace std;int main(){int a , b , n ;while( ~scanf( "%d" ,&n ) ){if( n == 1 || n % 2 == 0 ){printf( "2^? mod %d = 1\n" , n );}else{b = 1 ;int temp = 2 ; while( temp != 1 ){b++ ;temp = ( temp * 2 ) % n ;}printf( "2^%d mod %d = 1\n" , b , n ) ;}}return 0 ;}