poj1061 青蛙的约会

拓展欧几里得,求出符合条件的最小整数解

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#define LL long long
using namespace std;

LL exgcd(LL a,LL b,LL &x,LL &y)
{
    if(b == 0)
    {
        x= 1;
        y = 0;
        return a;
    }

    else
    {
        long long r = exgcd(b,a%b,y,x);
        y -= a/b*x;
        return r;
    }
}

int main()
{
    LL x,y,n,m,l,t,p;
    scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);
    LL a = n - m;
    LL b = l;
    LL d = x - y;
    if(a < 0)
    {
        a = -a;
        d = -d;
    }
    LL c = exgcd(a,b,t,p);
    if(d%c!=0)
    {
        printf("Impossible
");
        return 0;
    }

    else
    {
        t = t*d/c;
        p = p*d/c;
        LL mod = b/c;
        if(t >= 0)
            t = t%mod;
        else
            t = (t%mod + mod)%mod;
        printf("%lld
",t);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/alan-W/p/6511791.html