Power of power modulo with non square-free numbers

180 Views Asked by At

Reference https://www.hackerrank.com/challenges/devu-police/problem

I have five numbers a, b, c, d, N and I need to find $(a^b)^{c^d}$ mod N. N is non square-free number so euler totient theorem or CRT won't work here.

I have tried this way but according to test-cases my program gets timed out. I find $a^b$ mod N then I run power modulo with algorithm https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/ here exponent divided by 2 and I do with c so each computation needs O(d) which should not be rather logarithm solution expected.

Could you please help me to solve?