POJ 1061

/*
 * poj_1061.c
 * accepted
 * refer to: http://www.cnblogs.com/comeon4mydream/archive/2011/07/18/2109060.html
 */
#include 
typedef long long int int64;
static int64 extGcd (int64 a, int64 b, int64 * p, int64 * q);
static void poj_1061 ();

int64 ret,tmp;
int64 x, y, m, n, L;
int64 p, q, r, n_gcd;

int main (int argc, char * argv[]){
	poj_1061 ();
	return 0;

}

static void poj_1061 (){
	scanf ("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &L); /* 注意long long int 的输入需要两个l作用d */

	n_gcd = extGcd(n-m, L, &p, &q);
	r = L/n_gcd;
	if ((x-y)%n_gcd){
		printf ("Impossible\n");
	}
	else{
		printf ("%lld\n",((x - y)/n_gcd * p % r + r ) % r); /* 输出也是需要ll修饰d */
	}
}

static int64 extGcd (int64 a, int64 b, int64 * p, int64 * q){
	if (!b){
		*p = 1; *q = 0; return a;
	}
	ret = extGcd (b, a%b, p, q);
	tmp = *p - a/b*(*q); *p = *q; *q = tmp;
	return ret;
}


你可能感兴趣的:(POJ,POJ)