1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 typedef long long LL; 6 7 //给予二整数 a 与 b, 必存在有整数 x 与 y 使得ax + by = gcd(a,b) 8 LL extgcd(LL a, LL b, LL &x, LL &y){ 9 LL d = a; 10 if (b != 0){ 11 d = extgcd(b, a%b, y, x); 12 y -= (a / b)*x; 13 } 14 else{ 15 x = 1; 16 y = 0; 17 } 18 return d; 19 } 20 21 LL inv(LL a, LL m) 22 { 23 LL x, y; 24 extgcd(a, m, x, y); 25 return (m + x%m) % m; 26 } 27 28 int main(){ 29 ios::sync_with_stdio(false); 30 LL n, m; 31 while (cin >> m >> n) 32 { 33 cout << inv(m, n) << endl; 34 } 35 return 0; 36 }
原文地址:https://www.cnblogs.com/jaydenouyang/p/8721235.html
时间: 2024-10-31 16:19:25