hdu13952^x mod n = 1<数论>

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395

思路: 由题意得N=2^x(x>=1)为偶数, 所以当 n 也为偶数时  N%n必为偶数, 故不存在;

当n为奇数时,利用同余定理求;

View Code
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <string>

 4 #include <cstring>

 5 #include <cmath>

 6 using namespace std;

 7 

 8 int main( )

 9 {

10     int N;

11     while( scanf( "%d", &N )!= EOF ){

12         if( N==1 || !(N&1) )

13             printf( "2^? mod %d = 1\n", N );

14         else{

15             int t=2, ans=1;

16             while( t != 1 ){

17                 t <<= 1;

18                 t%=N;

19                 ans++;

20             } 

21             printf( "2^%d mod %d = 1\n",ans, N );

22             

23         }

24     }

25     return 0;

26 }

 

你可能感兴趣的:(HDU)