内容简介:设两只青蛙在$t$时刻相遇,那么第一只青蛙的位置是$(mt+x)\ mod\ L$,第二只青蛙的位置是$(nt+y)\ mod\ L$,根据题目要求则要求方程$mt+x\equiv nt+y(mod\ L)$的解$t$按照同余方程转化为裴蜀方程的证明步骤,我们这里也可以得到
题目大意是说,给一根长度为$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 青蛙的约会》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 当约会软件能看到你的脸红:算法能否拯救失败约会?
- 人工生命 2.0.3 更新,两条腿的青蛙和吃青蛙的蛇
- 青蛙与缓存:简化实用版动态规划
- 相亲指南:怎么带妹子来一场硬核黑客的约会
- 人工生命 v2.0.0 发布,给青蛙添个眼睛
- 连约会都能替你完成:十年后数字替身可决定你人生?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Hibernate
James Elliott / O'Reilly Media, Inc. / 2004-05-10 / USD 24.95
Do you enjoy writing software, except for the database code? Hibernate:A Developer's Notebook is for you. Database experts may enjoy fiddling with SQL, but you don't have to--the rest of the appl......一起来看看 《Hibernate》 这本书的介绍吧!