POJ-1061 青蛙的约会(扩展欧几里德算法)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;
typedef long long LL;

void gcd(LL a, LL b, LL &x, LL &y, LL &d){

    if(!b){
        x = 1;
        y = 0;
        d = a;
    }
    else{
        LL s1, s2;
        gcd(b, a%b, s1, s2, d);
        x = s2;
        y = s1 - a / b * s2;
    }

}
int main(){

// freopen("in.txt", "r", stdin);
    LL x, y, m, n, l;

    while(cin >> x >> y >> m >> n >> l){

        LL k1, k2, d;
        gcd(m-n, -l, k1, k2, d);
        if((y-x) % d != 0){
            cout << "Impossible" << endl;
            continue;
        }
        else{

            k1 *= (y - x) / d;
            l /= d;
            if(l < 0)
             l = -l;
            k1 %= l;
            if(k1 < 0)
             k1 += l;
        }
        cout << k1 << endl;
    }

    return 0;
}

你可能感兴趣的:(POJ-1061 青蛙的约会(扩展欧几里德算法))