标签:
扩展欧几里得算法,可以在计算gcd(a,b)的同时,计算出
ax+by=gcd(a,b)中a、b的值
#include<stdio.h> #include<iostream> using namespace std; int ext_gcd(int a, int b, int *x, int *y) { if(b==0) { *x = 1,*y = 0; return a; } else { int r = ext_gcd(b, a%b, x, y); int t = *x; *x = *y; *y = t - a/b * *y; return r; } } int main() { int a,b; int x,y; while(1) { cin>>a>>b; cout<<ext_gcd(a,b,&x,&y)<<endl; cout<<x<<" "<<y<<endl; } }
标签:
原文地址:http://www.cnblogs.com/superxuezhazha/p/5760395.html