POJ 1061 青蛙的约会( 拓欧经典题 )


**链接:****传送门 **

思路:简单拓展欧几里德,分析后可以得到方程 x + m * t = y + n * t + L * s( s控制圈数,t代表跳t次会碰面 ),经过化简可以得到 ( n - m ) * t + L * s = ( x - y ),无解输出个"Impossible",有解就求出最小整数解即可,最小整数解求法为 x = x * ( c/d ), x = ( x % ( b/d ) + ( b/d ) ) % ( b/d )( 针对于 ax + by = c 这个形式的方程而言 )


/*************************************************************************
    > File Name: poj1061.cpp
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年05月21日 星期日 20时37分28秒
 ************************************************************************/

#include<cstdio>
using namespace std;

#define ll long long

ll exgcd(ll a,ll b,ll& x,ll& y){
	if( b == 0 ){
		x = 1; y = 0; return a;
	}
	ll d = exgcd( b , a%b , x , y);
	ll tmp = x;
	x = y;	y = tmp - a/b*y;
	return d;
}
int main(){
	ll x , y , m , n , L , t , s;
	while(~scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)){
		ll d = exgcd( n-m , L , t , s );
		ll c = (x-y);
		if( c%d != 0 )	printf("Impossible
");
		else{
			t = t*(c/d);
			t = ( t%(L/d) + (L/d) ) % (L/d);
			printf("%lld
",t);
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/6885998.html