There two important theories that would help you to solve this problem :-
- Modular Exponentiation
- Fermat's little theorem
Modular Exponentiation :- This simple recursive formula to make you understand :-
(a^n)%p = ((a^(n-1))%p*a)%p
The above formula can help you prevent the overflow which occurs if a^n is large. Moreover fast exponention can be done using O(logn)
Fermat's little theorem :- If p is prime in your case 11 is prime then (a^(p-1))%p = 1
for any a that is co-prime to p hence (a^n)%p = (a^(n%(p-1)))%p
your example :-
9^2012 % 11 = ?
11 is prime and 9 is co-prime to 11 then using fermat's theorem
9^2012 % 11 = (9^(2012%10)) % 11
2012%10 = 2
9^2012 % 11 = 9^2 % 11 = 4