Numbers and bits · 14 / 15 Exercise Hard

Modular inverse

Objective

The modular inverse of a modulo m is the number x between 0 and m-1 such that a * x % m == 1. It exists only if a and m are coprime, and it is computed with the extended Euclidean algorithm.

Instructions

  • The first line has the number n of cases. Each case is a line with a and m.
  • Show the inverse of a modulo m, or -1 if it does not exist.
Input
3
3 7
2 4
10 17
Output
5
-1
12

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise