标签:tar exgcd col end names class .com problem else
1 int gcd(int a,int b) 2 { 3 return b?gcd(b,a%b):a;//最后返回的a为最大公约数 4 }
扩展欧几里得求逆元:51nod1256
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 using namespace std; 5 int d,x,y; 6 //ax+by=1(x为a的逆元,同理y为b的逆元) 7 void Exgcd(int a,int b,int &x,int &y) 8 { 9 if(!b) {d=a; x=1; y=0;} 10 else 11 { 12 Exgcd(b,a%b,x,y); 13 int tmp=x; 14 x=y; 15 y=tmp-(a/b)*y; 16 } 17 } 18 int main() 19 { 20 int a,b; 21 cin>>a>>b; 22 23 Exgcd(a,b,x,y); 24 if(d==1) 25 { 26 int ans=(x%b+b)%b; 27 cout<<ans<<endl; 28 } 29 else cout<<"-1"<<endl; 30 31 return 0; 32 }
完。
标签:tar exgcd col end names class .com problem else
原文地址:https://www.cnblogs.com/redblackk/p/9735324.html