text: Find ( a ^ b ) % m where ' b ' is very large | C # program to find ( a ^ b ) % m for b very large . ; Function to find power ; Initialize result ; 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: using System ; class GFG { static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } public static void Main ( ) { long a = 3 ; string b = "100000000000000000000000000" ; long remainderB = 0 ; long MOD = 1000000007 ; for ( int i = 0 ; i < b . Length ; i ++ ) remainderB = ( remainderB * 10 + b [ i ] - '0' ) % ( MOD - 1 ) ; Console . WriteLine ( power ( a , remainderB , MOD ) ) ; } }