P1516 青蛙的约会

 

(n-m)T+LP=X-Y

用exgcd去求解这个方程,然后找到最小正整数解即可

 

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll x,y,m,n,l;
void exgcd(ll a,ll b, ll &d, ll &x,ll &y)
{
	if(!b)
	{
		x=1; y=0; d=a;
		return;
	}
	exgcd(b,a%b,d,x,y);
	ll t=x; x=y; y=t-a/b*y;
}
int main()
{
	freopen("frog.in","r",stdin);
	freopen("frog.out","w",stdout);
	ll a,b,d;
	while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)==5 && x!=EOF)
	{
		if(n<m) swap(n,m),swap(x,y);
		exgcd(n-m,l,d,a,b);
		if((x-y)%d!=0 || m==n)
			printf("Impossible\n");
		else printf("%lld\n",(a*(x-y)/d%(l/d)+(l/d))%(l/d));
	}
	
	return 0;
}

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页