POJ-1061 青蛙的约会

题意:有两只青蛙,一只在坐标x,另一直在坐标y,青蛙x一次跳跃可以前进m单位距离,青蛙y一次跳跃可以前进n单位的距离,两青蛙都在同一纬度,该纬度长度为L。两只青蛙同方向同时跳啊跳,问你最少跳多少次,它们才可以相遇,如果不能相遇,输出impossble。

思路:初识扩展欧几里得~

题目链接:http://poj.org/problem?id=1061

 

View Code
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 #define LL long long
10 
11 LL x,y,m,n,L;
12 
13 LL exgcd(LL a,LL b){
14     LL t,d;
15     if(b==0){
16         x=1;
17         y=0;
18         return a;
19     }
20     else{
21         d=exgcd(b,a%b);
22         t=x;
23         x=y;
24         y=t-(a/b)*y;
25         return d;
26     }
27 }
28 
29 int main(){
30     
31     freopen("data.in","r",stdin);
32     freopen("data.out","w",stdout);
33     
34     while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)!=EOF){
35         LL a=n-m;
36         LL b=L;
37         LL c=x-y;
38         LL d=exgcd(a,b);
39         if(c%d!=0) {puts("Impossible"); continue;}
40         x=x*(c/d);
41         y=y*(c/d);    //x1=x+b/d*t    y1=y-a/d*t;
42         LL k=x*d/b;
43         k=x-k*b/d;
44         if(k<0) k+=b/d;
45         printf("%lld\n",k);
46     }
47     return 0;
48 }

转载于:https://www.cnblogs.com/Hug-Sea/articles/2645229.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值