题目大意:数轴上两只青蛙分别从X,Y出发,一个一次跳m米,一个一次跳n米,L米后回到原点,求几次碰面
题解:
X+am≡Y+an(modL)
,求解a
整理一下
a(m−n)≡Y−X(modL)
变成不定方程
a(m−n)+kL=Y−X
然后扩展欧几里得求最小正整数解
m
<
<script id="MathJax-Element-310" type="math/tex"><</script>n时交换(m,n)&(X,Y),防止出现负数
我的收获:板子题……
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
ll X,Y,n,m,L;
void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b) d=a,x=1,y=0;
else exgcd(b,a%b,d,y,x),y-=x*(a/b);
}
void init()
{
cin>>X>>Y>>m>>n>>L;
if(m<n) swap(m,n),swap(X,Y);//处理负数情况
ll d,x,y;
exgcd(m-n,L,d,x,y);
if((Y-X)%d) {puts("Impossible");return ;}
x=x*((Y-X)/d)%(L/d);
while(x<0) x+=L/d;
cout<<x<<endl;
}
int main()
{
init();
return 0;
}