代码示例:求出ax + by = c的所有解
#include<cstdio>
int exgcd(int a,int b,int& x,int& y){
if(b == 0){
x = 1,y = 0;
return a;
}
int d = exgcd(b,a%b,x,y);
int t = x;
x = y,y = t - a/b*y;
return d;
}
int main(){
int a,b,c,x,y;
scanf("%d%d%d",&a,&b,&c);
int d = exgcd(a,b,x,y);
int x0 = c/d*x; //特解
int y0 = c/d*y; //特解
printf("%d %d
",x0,y0);
for(int k = 1;k < 100;k++){
printf("%d %d
",x0+b/d*k,y0-a/d*k);
}
return 0;
}