Modular Inverse


Problem

Given a formular which result is normally very large, can't be stored in any primitive data type. Calculate the result in mod . The formular often involve division. For example, let's calculate this

Naive approach

Naive approach 1 (Heavy Implementation)

  1. Calculate , using big number class to store result
  2. Calculate , also using a custom implemented BigNumber class
    1. You can optimize this by calculate in one loop. This way you decimate alot of computation
  3. Divide 2 numbers to get the result
  4. Mod the result by

This requires non-trivial effort to implement BigNumber class. Python folks are in luck but most programming language doesn't support big number by default. Assume that you are an average programmer like me. You often fail to come up with a correct implementation of the BigNumber class.

Naive approach 2 (Wrong Answer)

  1. Calculate
  2. Calculate
    1. Same as before. You can optimize this by calculate in one loop. This way you decimate alot of computation
  3. Divide 2 numbers to get the result

This seems natural but unfortunately gives WA, because the algorithm is wrong mathematically.

Solution

The problem with naive approach #2 is that, in step 3 where we normally divide 2 numbers, we get incorrect answer. Because division doesn’t work in modular arithmetic, only multiplication. But luckily there is a formular to address this issue, it converts division to multiplication by leveraging Modular multiplicative inverse.

Formally, if is a prime number, we have

Everytime you were to do division, you multiply it with the power of the divisor instead and it's guaranteed to be correct mathematically 😎

There is a new problem araise, calculation of could be expensive (for example ). This could easily be solved by using a fast power calculation algorithm

#define INF (1e9+7)
long long fastPow(int x, int p) {
	if(p == 0) return 1;
	if(p == 1) return x;
	auto k = fastPow(x,p/2);
	return k*k*fastPow(x,p%2)%INF;
}