模板—数学—Exgcd
模板—数学—Exgcd
Code:
#include <cstdio> #include <algorithm> using namespace std; int ex_gcd(int a,int b,int &x,int &y) { if(!b) {x=1,y=0;return a;} int gcd=ex_gcd(b,a%b,x,y),tmp=x; x=y,y=tmp-a/b*y; return gcd; } int a,b,c,gcd,x1,y1; int main() { scanf("%d%d%d",&a,&b,&c),gcd=ex_gcd(a,b,x1,y1); if(c%gcd) {printf("-1\n");return 0;} x1*=c/gcd,y1*=c/gcd; printf("x=%d+k*%d\n",x1,b/gcd); printf("y=%d-k*%d\n",y1,a/gcd); }