In this 615D - Multipliers after analyzing some cases, I saw that,
If a number n = p1^a * p2^b * p3^c.
Then for the answer, the contribution of each prime is something like -
Let that , calculating the contribution of p1 --->
x = (a * (a+1) )/2;
y = (b+1)*(c+1) [ That is , how many divisors are there without the prime p1]
z = x*y
so the contribution of p1 is p1^z
To calculate y , I had to use two loops. And got TLE 86303876 . How to optimize this portion?
Right now your method is O(n^2*log(n)) at worst. Your method can work just fine if you precompute the total number of divisors % p-1 = q. Say phi() is euler's totient function. Let's say you want the contribution of prime p1, then the contribution z = x*y = x*q/(a+1) = x*q*(a+1)**(phi(p-1)-1) % (p-1). The reason this is true is because the modular inverse of a number b mod p-1 is (b**(phi(p-1)-1)) % p-1. Then the problem can be solved in O(n*log(n)) time. You can calculate euler's totient function in O((p-1)^0.5) time.