fork download
  1. #include <stdio.h>
  2.  
  3. int getInverse(int a, int b)
  4. {
  5. int x1 = 1, y1 = 0, z1 = a, x2 = 0, y2 = 1, z2 = b, q, t;
  6. while (z2 > 1) {
  7. q = z1 / z2;
  8. x1 -= q * x2, y1 -= q * y2, z1 -= q * z2;
  9. #define SWAP(a, b) (t = a, a = b, b = t)
  10. SWAP(x1, x2), SWAP(y1, y2), SWAP(z1, z2);
  11. }
  12. return x2 < 0 ? x2 + b : x2;
  13. }
  14.  
  15. int main(void)
  16. {
  17. int r, q;
  18. while (printf(">> "), scanf("%d%d", &r, &q) == 2)
  19. printf(" inverse(%d) = %d (mod %d)\n",
  20. r, getInverse(r, q), q);
  21. return 0;
  22. }
  23.  
Success #stdin #stdout 0s 2164KB
stdin
5 11
stdout
>>  inverse(5) = 9 (mod 11)
>>