题目链接
Input
输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。
Output
输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"
x代表青蛙A的初始位置,m代表青蛙A每跳一步的距离;
y代表青蛙B的初始位置,n代表青蛙B每跳一步的距离;
L表示一圈的长度
假设跳T次,两只青蛙相遇,可以写出 (x+mT)-(y+nT)= PL(pL代表两只青蛙开始时距离差)
移项得(n-m)T+LP=x-y,将(n-m)看做a,L看做b,求T的最小解,用到的方法是扩展欧几里得
参考博客:https://blog.csdn.net/Dafang_Xu/article/details/50186653?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522160964714416780310291343%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25255Fall.%252522%25257D&request_id=160964714416780310291343&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-22-50186653.pc_search_result_cache&utm_term=%20%E6%89%A9%E5%B1%95%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%97
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define ll long long
using namespace std;
const int maxn = 1e5+10;
const int MAXN = 2e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;
ll x,y,n,m,l;
ll gcd(int a,int b)
{
ll t,d;
if(b==0)
{
x=1;
y=0;
return a;
}
d=gcd(b,a%b);
t=x;
x=y;
y=t-(a/b)*y;
return d;
}
int main()
{
cin>>x>>y>>m>>n>>l;
ll c=x-y;
ll a = n-m,b=l;
ll g = gcd(a,b);
if(c%g!=0)
cout<<"Impossible"<<endl;
else
{
x = x*(c/g);//x,y是ax+by的特解
// y = y*(c/g);
// x = x0+b*t
// y = y0-a*t(x0,y0代表求出的x,y)
ll k = x/(b/g);
x = x-(b/g)*k;
if(x<0)
x+=b/g;
printf("%lld\n",x);
}
return 0;
}