青蛙的约会

题目分析:
  设时间为t,则两个青蛙的位置分别为(x+mt)mod L、(y+nt) mod L,相遇即是(x+mt)%L=(y+nt)%L,即(m-n)*t+k*L=y-x。
OK,现在已经符合ax+by=c的方程了,设a=m-n,b=L,c=y-x,然后套用模板求出特解t的值,注意t>0,所以要用通解公式得出最小正整数(为啥刚开始我就没想到这一点呢)。最后注意用long long~ 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
ll extended_gcd(ll a,ll b,ll &x,ll &y)
{
    ll r,t;
    if(!b)
    {
        x=1;
        y=0;
        return a;
    }
    r=extended_gcd(b,a%b,x,y);
    t=x;
    x=y;
    y=t-a/b*y;
    return r;
}
int main()
{
    ll x,y,m,n,l,p,c,a,b;
    cin>>x>>y>>m>>n>>l;
    a=n-m;
    c=x-y;
    b=l;
    if(a<0)
    {
        a=-a;
        c=-c;
    }
    p=extended_gcd(a,b,x,y);
    if(c%p!=0)
        printf("Impossible
");
    else
    {
        x=x*c/p;
        ll t=b/p;
        if(x>=0)
            x=x%t;
        else
            x=x%t+t;
        printf("%lld
",x);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zcy19990813/p/9702699.html