POJ 1061 青蛙的约会 (欧几里德+同余方程)

本文深入探讨了扩展欧几里得算法的原理及应用,通过实例代码讲解了如何求解最大公约数和贝祖等式的解。适用于数学算法、计算机科学领域的学习者。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<iostream>
using namespace std;
void Ex_gcd(long long a, long long b, long long &x, long long &y)
{
    if(b == 0)//µÝ¹é³ö¿Ú
    {
        x = 1;
        y = 0;
        return;
    }
    long long x1, y1;
    Ex_gcd(b, a%b, x1, y1);
    x = y1;
    y = x1-(a/b)*y1;
}
long long gcd(long long a,long long b)
{
    return b?gcd(b,a%b):a;
}
void jiaohuan(long long &a,long long &b)
{
    long long c=a;
    a=b;
    b=c;
}
int main()
{

  long long x,y,m,n,L,a,b,c,g;
  scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L);
  if(m<n)
  {
      jiaohuan(x,y);
      jiaohuan(m,n);
  }
  a=m-n;
  b=L;
  c=y-x;///是y-x  不是x-y
  if(a==0)
  {
      printf("Impossible\n");
      return 0;
  }
  g=gcd(a,b);
 // printf("%lld\n",g);
   if(c%g!=0)
   {
       printf("Impossible\n");
   }
   else
   {
      c=c/g;
      x=y=0;
       Ex_gcd(a,b,x,y);
       b=b/g;
        x=x*c;
        x=(x%b+b)%b;
        printf("%lld\n",x);
   }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值