text: Find ( a ^ b ) % m where ' a ' is very large | C ++ program to find ( a ^ b ) mod m for a large ' a ' ; utility function to calculate a % m ; ( s [ i ] - '0' ) gives the digit value and form the number ; Returns find ( a ^ b ) % m ; Find a % m ; now multiply ans by b - 1 times and take mod with m ; Driver program to run the case
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int aModM ( string s , unsigned int mod ) { unsigned int number = 0 ; for ( unsigned int i = 0 ; i < s . length ( ) ; i ++ ) { number = ( number * 10 + ( s [ i ] - '0' ) ) ; number %= mod ; } return number ; } unsigned int ApowBmodM ( string & a , unsigned int b , unsigned int m ) { unsigned int ans = aModM ( a , m ) ; unsigned int mul = ans ; for ( unsigned int i = 1 ; i < b ; i ++ ) ans = ( ans * mul ) % m ; return ans ; } int main ( ) { string a = "987584345091051645734583954832576" ; unsigned int b = 3 , m = 11 ; cout << ApowBmodM ( a , b , m ) ; return 0 ; }