text: Find ( a ^ b ) % m where ' b ' is very large | Function to find power ; Update x if it is more than or equal to p ; If y is odd , multiply x with the result ; y must be even now y = y >> 1 y = y / 2 ; Driver Code ; String input as b is very large ; Reduce the number B to a small number using Fermat Little
code: def power ( x , y , p ) : NEW_LINE INDENT x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT a = 3 NEW_LINE b = "100000000000000000000000000" NEW_LINE remainderB = 0 NEW_LINE MOD = 1000000007 NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT remainderB = ( ( remainderB * 10 + ord ( b [ i ] ) - 48 ) % ( MOD - 1 ) ) NEW_LINE DEDENT print ( power ( a , remainderB , MOD ) ) NEW_LINE