洛谷P1516 青蛙的约会【扩欧】

题目描述 https://www.luogu.org/problemnew/show/P1516
根据题目写出方程,判断无解的条件,再求出最小的正整数解。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
ll x,y,n,m,l,d,x1,y1,ans;
void ggcd(ll a,ll b,ll &d,ll &x,ll &y)
{
	if(b==0) d=a,x=1,y=0;
	else 
	{
		ggcd(b,a%b,d,x,y);
		ll t=x;
		x=y,y=t-(a/b)*y;
	}
}
int main()
{
   scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);
   if(m>n) swap(m,n),swap(x,y);
   ggcd(n-m,l,d,x1,y1);
   if((x-y)%d!=0||n==m){ cout<<"Impossible"; return 0;}   
   x1=(x-y)/d*x1;
   d=l/d;
   x1=(x1%d+d)%d;
   printf("%lld",x1);	 	
	
 return 0;	
}

猜你喜欢

转载自blog.csdn.net/qq_42920122/article/details/88930564