| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 119749 | Accepted: 25250 |
Description
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
题目分析:
设时间为t,则两个青蛙的位置分别为(x+mt)mod L、(y+nt) mod L,相遇即是(x+mt)%L=(y+nt)%L,即(m-n)*t+k*L=y-x。
OK,现在已经符合ax+by=c的方程了,设a=m-n,b=L,c=y-x,然后套用模板求出特解t的值,注意t>0,所以要用通解公式得出最小正整数。最后注意用long long~(如果不明白看博客扩展欧几里得2详解)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll ex_gcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x = 1;
y = 0;
return a;
}
int r = ex_gcd(b,a%b,y,x);
y -= x*(a/b);
return r;
}//扩展欧几里得模板
int main(){
ll x,y,m,n,l;
ll a,b,c,gcd;
scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);
a = m-n;
b = l;
c = y-x;//由转化的式子用abc代替相应值
if(a < 0){
a = -a;
c = -c;
}//根据(x+mt)%L =(y+nt)%L,m-n<0说明m<n,若等式成立y<x,所以y-x<0,也要变号
gcd = ex_gcd(a,b,x,y);
if(c%gcd!=0){
printf("Impossible\n");
return 0;
}
x *= (c/gcd);
b = b/gcd; //求最小整数解模板
if(b<0)b=-b;
if(x>=0)
x %= b;
else
x = x%b + b;
printf("%lld\n",x);
return 0;
}
1262



被折叠的 条评论
为什么被折叠?



