Given a GP sum (1-((n-1)/n)^r) = P/Q , how to calculate this P/Q fraction when r is large and output (P*Q^(-1))%1000000007 where Q^(-1) is modular inverse of Q modulo 1000000007
I can calculate (n-1)^r and n^r using modular exponentiation and then print P*Q^(-1) by using modular inverse formula using fermat's little theorem, but this is not correct because i think (n^r) modular inverse is not same as Q^(-1) and if i calculate Q without using modular exponentiation it overflows even long long in C++. So please guide me what i am doing wrong?
ll modInverse(ll a, ll m)
{
ll ans = power(a, m-2, m); //for finding modular inverse
return ans;
}
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0) // ll is long long
{ //p=1000000007;
if (y & 1) //for calculating n^r and (n-1)^r
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
calculating P*Q^(-1) % 1000000007 is giving unexpected answer for large values because of overflow and if overflow is restricted using mod 1000000007 giving wrong values. I use fermat's little theorem to calculate the modular inverse and fast power method to evaluate n^r.
for