HDU1395+快速幂
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h> 2 int fast_pow( int a,int b,int mod ){ 3 int res = 1; 4 while( b>=1 ){ 5 if( b%2==1 ){ 6 res = res*a%mod; 7 } 8 a = a*a%mod; 9 b/=2; 10 } 11 return res; 12 } 13 int main(){ 14 int n; 15 while( ~scanf("%d",&n) ){ 16 if( n==1||n%2==0 ){ 17 printf("2^? mod %d = 1\n",n); 18 continue; 19 } 20 int ans = 2; 21 while( 1 ){ 22 if( fast_pow( 2,ans,n )==1 ) 23 break; 24 ans++; 25 } 26 printf("2^%d mod %d = 1\n",ans,n); 27 } 28 return 0; 29 }
keep moving...