51Nod 1256 乘法逆元(扩展欧几里得)
复制代码
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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步