poj-1061-扩展欧几里得算法-青蛙的约会

题目链接:

http://poj.org/problem?id=1061

题目意思:

两只青蛙,开始的位置分别为x和y,在长度为L的循环直线上,分别以速度m和n从开始位置跳,跳一次两只青蛙的时间相同。

问跳多少次两只青蛙能到达同一点,如果不能输出Impossible.

解题思路:

有(x+ma)%L=(y+na)%L

得(m-n)a=(y-x)%L

实际上就是求出最小的正整数a使得上式满足,很裸的扩展欧几里得算法的应用。

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF (1<<30)
#define PI acos(-1.0)
#define ll __int64
using namespace std;

/*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/

struct Frog
{
   ll p,s;
};

void swap(struct Frog &a,struct Frog &b)
{
   struct Frog t;

   t=a;
   a=b;
   b=t;

   return ;
}

ll egcd(ll a,ll b,ll & x,ll & y)
{
   if(b==0)
   {
      x=1,y=0;
      return a;
   }
   ll d=egcd(b,a%b,x,y);
   ll t=x;

   x=y,y=t-a/b*y;

   return d;
}

bool lineq(ll a,ll b,ll c,ll & x,ll &y,ll & t)
{
   t=egcd(a,b,x,y);
  // printf(":%I64d\n",t);
   if(c%t)
      return false;
   x=x*c/t;
   y=y*c/t;

   return true;
}

int main()
{
   Frog a,b;
   ll l;

   while(scanf("%I64d%I64d",&a.p,&b.p)!=EOF)
   {
      scanf("%I64d%I64d%I64d",&a.s,&b.s,&l);
      if(a.s<b.s) //交换下,这样避免求对大公约数的时候出问题
         swap(a,b);
      ll c=b.p-a.p;
      ll x,y,d;

      bool ans=lineq(a.s-b.s,l,c,x,y,d);

      if(ans==false)
         printf("Impossible\n");
      else
      {
         if(x<0) //找到最小的正整数x
         {
            while(x<0)
               x+=l/d;
         }
        else
         {
            while(x>=0)
               x-=l/d;
            x+=l/d;
         }
         printf("%I64d\n",x);
      }


   }

   return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值