POJ1061---青蛙的约会(同余方程,拓展gcd)

标准的同余方程题
设跳x次,青蛙A起点a,每次跳m;青蛙B起点b,每次跳n。则有下列关系:
(a+mx) mod L = (b+nx) mod L;
即为:(m-n)x=(b-a)(mod L) 注意,m-n和L必须都为正。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll x, y;
ll e_gcd(ll a, ll b, ll &x, ll&y)
{
    if (b==0)
    {
        x = 1; y = 0;
        return a;
    }
    ll r = e_gcd(b, a%b, x, y);
    ll t = y;
    y = x - (a / b)*y;
    x = t;
    return r;
}


bool tongyu(ll a, ll b, ll n)
{
    ll d = e_gcd(a, n, x, y);
    if (b%d) return false;
    x = x*(b / d) % n;          //最小解 
    x = (x + n / d) % (n / d);  //最小正数解 
    return true;
}

int main()
{
    ll a, b, m, n, L;
    cin >> a >> b >> m >> n>>L;
    if (m < n)
    {
        swap(m, n);
        swap(a, b);

    }
    if (tongyu(m-n,b-a,L))
        cout << x << endl;
    else cout << "Impossible" << endl;
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值