0

Question

I have uploaded my problem as screenshot.

Maggi Iggam
  • 682
  • 1
  • 8
  • 20
  • 1
    MathJax is not operational on this site. Could you re-do your expressions to be more understandable here? You could use graphics--if you do not have enough reputation to put them directly into your question, link to them and others can edit them in. – Rory Daulton Jul 14 '18 at 20:24
  • Your theorem is wrong. Consider (7^9)%5. The answer is 2. However (7^(9%5))%5 = (7^4)%5 = 1. You might want to take this to https://math.stackexchange.com to ask if there is a way of simplifying the maths you want to do. I'm guessing you at least know that (a^b)%m = ((a%m)^b)%m ? – Chris Jul 14 '18 at 20:33
  • Check out [Lucas's theorem](https://en.wikipedia.org/wiki/Lucas%27s_theorem) – user58697 Jul 14 '18 at 20:33
  • @Chris yes i know it. I guess its computational, and that maths can't help me reduce it further. – Maggi Iggam Jul 14 '18 at 20:40
  • @user58697 hey i know Lucas Theorem. but problem here is exponent can be computed only with modulo. – Maggi Iggam Jul 14 '18 at 20:41
  • 1
    How large is M in general? If its not too big you might be able to find k such that `a^k = 1 mod M` and then you could use that `a^b = a^c mod M` where c = b mod k (I am pretty sure that is right). – Chris Jul 14 '18 at 20:47
  • @Chris M=10^9+7. It looks like something i need. Can you give more insight to how you got there? And ofcourse, we can use k=M-1 using Fermat's little theorem. – Maggi Iggam Jul 14 '18 at 21:09

2 Answers2

3

Fermat's little theorem says

x^p mod p = x mod p     or     x^(p-1) mod p = 1  (if p does not divide x)

We can use this to reduce the number of operations for computing x^m (m arbitrary) when p doesn't divide x:

  1. Divide m by (p-1) and get: s = (m mod p-1); no need to compute the quotient
  2. Note that m = (p-1)q + s and x^(p-1) = 1 mod p. So,
    • x^m = x^((p-1)q)x^s = (1^q)(x^s) mod p = x^(m mod p-1)

The problem that remains, however, is how to compute

choose(n,r) = n(n-1)...(n-r+1)/(r(r-1)...1) mod p-1

Addendum

Further thinking on the problem above leads us to consider the following. We have three integers:

c = choose(n,r)
m = n(n-1)...(n-r+1)
f = factorial(r)
q = p-1

which satisfy

c = m/f,                                 eq 1

the question we have to answer is whether it is valid to compute c mod q as

(c mod q) = (m mod q)/(f mod q)          eq 2

right? Because this would allow us to reduce the calculation of choose(n,r) to two series of modular multiplications plus a single division (an easy and efficient algorithm).

Now, eq 1 can be rewritten as

 c*f = m

which enables us to apply mod q to both sides:

 (c mod q)(f mod q) = (m mod q)          eq 3

because mod is known to commute with products (and sums), which is the very property we will use to compute the series of modular multiplications mentioned above.

And since the tree quantities involved in eq 3 are integers, we can divide both sides by (f mod q) arriving at eq 2. My answer is now complete.

Addendum 2

I said My answer is now complete. Not quite. The problem that still remains happens when f mod q = 0, in this case we cannot divide eq 3 as we did above. This case requires an special treatment and will result in a more complex algorithm.

One idea worth trying is to factor q, i.e., p-1 as a product of primes and consider these primes with their exponents one by one. Take one of them, say t^e. We know that t^e must divide the factorial f. Therefore, it must divide the right hand side of eq 3 too. So, we have to peek enough factors among n, n-1, n-2, ..., n-r+1 that are divisible by t until we exhaust e divisions by t. Then we need to replace those factors with their quotient by the corresponding power of t. After repeating this procedure for all the primes inside q=p-1, we will get f/q on the left, and the new list of factors on the right. This will be our new version of eq 3. Of course the new f (=f/q) may be again divisible by q. Because of this we have to repeat the same procedure until f mod q is no longer 0. At that point we will be able to divide and get the value of c mod q.

Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51
1

https://en.m.wikipedia.org/wiki/Fermat%27s_little_theorem implies that (x^y) mod p = x^(y mod (p-1)) mod p when p does not divide x.

David Eisenstat
  • 64,237
  • 7
  • 60
  • 120