Advertisement
Guest User

G

a guest
Oct 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. #define li long long
  9.  
  10. li gcd(li a, li b) {
  11. return b ? gcd(b, a % b) : a;
  12. }
  13.  
  14.  
  15. int gcdex(li a, li b, li& x, li& y) {
  16. if (a == 0) {
  17. x = 0; y = 1;
  18. return b;
  19. }
  20. li x1, y1;
  21. li d = gcdex(b % a, a, x1, y1);
  22. x = y1 - (b / a) * x1;
  23. y = x1;
  24. return d;
  25. }
  26.  
  27. int main() {
  28. long long n, q;
  29. cin >> n >> q;
  30.  
  31. if (gcd(n, q) != 1)
  32. cout << -1;
  33. else {
  34. li x, y;
  35. gcdex(q, n, x, y);
  36. x = (x % n + n) % n;
  37. cout << x;
  38. }
  39. return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement