pku1061青蛙的约会 解题报告

两只青蛙跳了t 步
A的坐标 x+mt
B的坐标 y+nt
相遇的充要条件:
     x+mt-y-nt= pL ( p是整数) 即
     (n-m)*t+Lp=x-y    (L>0)
问题转化为:
求满足 (m-n)*t+Lp=(y-x)   的最小 t (t>0)
即求 一次同余方程
(m-n)*t = (y-x) (mod L) 的最小正整数解

 

#include<cstdio>

long long mod(long long a,long long b)
{
return (a % b + b) % b;
}

struct triple
{
long long d,x,y;
};

long long Euclid(long long a,long long b)
{
if(b == 0)
   return a;
else
   return Euclid(b,mod(a,b));
}

triple Extended_Euclid(long long a,long long b)
{
triple result;
if(b == 0)
{
   result.d = a;
   result.x = 1;
   result.y = 0;
}
else
{
   triple ee = Extended_Euclid(b,mod(a,b));
   result.d = ee.d;
   result.x = ee.y;
   result.y = ee.x - (a/b)*ee.y;
}
return result;
}

long long MLES(long long a,long long b,long long n)
{
triple ee = Extended_Euclid(a,n);
if(mod(b,ee.d) == 0)
   return mod((ee.x * (b / ee.d)),n/ee.d);
else
   return -1;
}

int main()
{
     long long x, y, m, n, l;
     while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l) != EOF){
long long mles;
mles = MLES(m-n,y-x,l);
if(mles < 0)
         printf("Impossible/n");
     else
         printf("%I64d/n",mles);
}
     return 0;
}

你可能感兴趣的:(struct)