I am trying to calculate $A^A$ % mod, where mod is $10^9 + 7$. I can calculate ($A$ % mod) for any value of mod but don't have direct access to A. How would I solve this?
Asked
Active
Viewed 53 times
0
-
fermat's little theorem – Saketh Malyala May 05 '19 at 19:21
-
Could you please elaborate? – Omar S May 05 '19 at 19:59
1 Answers
1
Let $p=10^9+7$ and $a=A \bmod p$. This answer also assumes $\gcd(A,p)=1$.
Since $p\in\mathbb{P}$, $\phi(p)=p-1$ and therefore $a^{p-1}=1 \bmod p$.
Now let $b=A\bmod(p-1)$
Therefore $A^A\bmod p=a^b\bmod p$.
There are more efficient algorithms than above for modular exponentiation such as those discussed at the following link.
Steven Clark
- 7,363