拓展欧几里得临时文档5

#include <stdio.h>
#define LL long
LL gcd (LL a, LL b)  
{  
    return b ? gcd (b, a%b) : a;  
}  
void Egcd(LL a, LL b, LL &x, LL &y) {  
    if (b == 0) {  
        x = 1; y = 0; return;  
    }  
    LL tp;  
    Egcd (b, a%b, x, y);  
    tp = x;  
    x = y;  
    y = tp - a/b*y;  
}     
int main() {
	LL a,b, c,d;
	while (scanf("%d%d%d", &a, &b,&c) == 3) {
		LL x, y;
		d = gcd (a, b); 
		if (c % d != 0){ printf("IMposible
");continue;}
		 LL ee=c/d;	 
		a /= d, b /= d, c /= d;  
		 Egcd (a, b, x, y);  
		// x *= c;   //尽量让值给y
	//	  x = (x % b + b) % b;                         //x的特解 
	//	  y = (c - a*x) / b;                           //有了x,求对应的y  

		 y*=c;      //尽量让值给x
		 y=(y%a+a)%a;
		 x=(c-b*y)/a;
		 a*=d;b*=d;c*=d;
		printf("%d * %d + %d * %d = %d
", a, x, b, y,c);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

today lazy . tomorrow die .
原文地址:https://www.cnblogs.com/france/p/4808676.html