POJ 1061 青蛙的约会 (扩展欧几里得)

题意:青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。

题解:扩展欧几里得
(x+mt)%L = (y+nt)%L,即x-y = (n-m)* t+L*k。(t、K未知)
另gcd(n-m, L) = d,可得d = (n-m) * [t*d/(x-y)] + L*[K*d/(x-y)],接着用扩展欧几里得解即可。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
int x, y, m, n, l;
//扩展欧几里得
//若a和b为正整数,则存在整数x,y使得gcd(a,b)=ax+by;
void exgcd(ll a, ll b, ll& d, ll& x, ll& y) {  //d为a、b的最大公约数
    if (!b) {
        d = a;
        x = 1;
        y = 0;
    }
    else {
        exgcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int main() {
	scanf("%d%d%d%d%d", &x, &y, &m, &n, &l);
    ll d, k, t;
    exgcd(n - m, l, d, t, k);
    if ((x - y) % d != 0) puts("Impossible");
    else {
        t = t * (x - y) / d;
        int mod = l / d;
        printf("%lld\n", ((t % mod) + mod) % mod);
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值