1

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

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
cooldude
  • 63
  • 10
  • The [extended Euclidean algorithm](https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm) could be used to find modular inverses. Perhaps that approach is more immune to overflow in intermediate results (although I would think that as long as your numbers are well below the square root of what a long long can hold, overflow shouldn't be a problem in using Fermat's little theorem if it is properly implemented). – John Coleman Feb 03 '19 at 13:23
  • actually i am trying to find (10000^10000)^(-1)%1000000007 so that's why either i am getting wrong result or number overflow @JohnColeman – cooldude Feb 03 '19 at 13:47
  • Why not find (10000^10000)%1000000007 and then find its inverse? With a good modular exponentiation algorithm, overflow isn't an issue. – John Coleman Feb 03 '19 at 13:49
  • @JohnColeman will it produce the same result as first inverse then modulo?? i am currently doing the same but i am not getting correct result it's a task of a probability maximizing game – cooldude Feb 03 '19 at 13:51
  • 1
    In any group (and nonzero numbers mod 1000000007 form a group under modular multiplication) `(a^k)^-1 = (a^-1)^k` for all integers `k`. – John Coleman Feb 03 '19 at 13:57
  • @JohnColeman thank you i will check my algorithm again, one more query if we take gcd (f,(a^k)^(-1)) = gcd (f , (a^(-1))^k) ?? is this statement true? – cooldude Feb 03 '19 at 14:03
  • @cooldude Not quite sure what you are asking. If `b%p = c%p` then it isn't true that for any `a` we have `gcd(a,b)%p = gcd(a,c)%p`. For example, 6%7 = 20%7 but gcd(5,6) = 1 which isn't congruent mod 7 to gcd(5,20) = 5 – John Coleman Feb 03 '19 at 14:27
  • Alright @JohnColeman thanks for teaching some modular mathematics. – cooldude Feb 03 '19 at 14:30
  • What is your type `ll`? Is it big enough to contain e.g. `p*p`? I suggest reading http://sscce.org/ on how to include exactly the right amount of code in a question like this. – MvG Feb 03 '19 at 20:37
  • it's long long and yes it can handle p*p – cooldude Feb 04 '19 at 08:07

1 Answers1

-1

We have to find a value x such that (Qx)%MOD = 1. This means (Q%MODx%MOD)%MOD = 1. Which means for denominator, you can find Q%MOD ie (n^r)%MOD using modular exponentiation. Then replace Q%MOD with Q'. This means (Q'*x)MOD=1. So find modular inverse for Q'. (MOD=1000000007)