POJ1061 青蛙的约会

栏目: 编程工具 · 发布时间: 5年前

内容简介:设两只青蛙在$t$时刻相遇,那么第一只青蛙的位置是$(mt+x)\ mod\ L$,第二只青蛙的位置是$(nt+y)\ mod\ L$,根据题目要求则要求方程$mt+x\equiv nt+y(mod\ L)$的解$t$按照同余方程转化为裴蜀方程的证明步骤,我们这里也可以得到

POJ1061 青蛙的约会 题目大意是说,给一根长度为$L$的数轴,有两只青蛙分别从数轴的$x$和$y$位置开始向左跳,第一只青蛙的速度是$m$,第二只青蛙的速度是$n$,问这两只青蛙能否在相同时刻相同地点相遇

设两只青蛙在$t$时刻相遇,那么第一只青蛙的位置是$(mt+x)\ mod\ L$,第二只青蛙的位置是$(nt+y)\ mod\ L$,根据题目要求则要求方程$mt+x\equiv nt+y(mod\ L)$的解$t$

按照同余方程转化为裴蜀方程的证明步骤,我们这里也可以得到

$$

mt+x=Lk_1+p\\

nt+y=Lk_2+p

$$

两式相减得$(m-n)t+(x-y)=L(k_1-k_2)$,移项$(m-n)t+Lk=y-x$

$x,y,m,n,L$都是已知的,直接带入线性方程求解即可得到$t$,如果求出的$t$小于0,还需要变换一下求出第一个大于0的解

import java.util.Scanner;

public class Main {
    static long x, y;
    static long exgcd(long a, long b) {
        if (b == 0) {
            x = 1;
            y = 0;
            return a;
        }
        long res = exgcd(b, a % b);
        long x1 = x;
        x = y;
        y = x1 - (a / b) * y;
        return res;
    }

    static long linearEquation(long a, long b, long m) throws Exception {
        long d = exgcd(a, b);
        if (m % d != 0)
            throw new Exception("Impossible");
        long n = m / d;
        x *= n;
        y *= n;
        return d;
    }
    
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int m = cin.nextInt();
        int n = cin.nextInt();
        int l = cin.nextInt();
        try {
            long d = linearEquation(m-n, l, b-a);
            l /= d;
            l = l < 0 ? -l : l;
            x = (x % l + l) % l;
            System.out.println(x);
        } catch (Exception e) {
            System.out.println("Impossible");
        }
    }
}

以上所述就是小编给大家介绍的《POJ1061 青蛙的约会》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Iterative Methods for Sparse Linear Systems, Second Edition

Iterative Methods for Sparse Linear Systems, Second Edition

Yousef Saad / Society for Industrial and Applied Mathematics / 2003-04-30 / USD 102.00

Tremendous progress has been made in the scientific and engineering disciplines regarding the use of iterative methods for linear systems. The size and complexity of linear and nonlinear systems arisi......一起来看看 《Iterative Methods for Sparse Linear Systems, Second Edition》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试