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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值