您的位置:首页 > 其它

poj1061 青蛙的约会(扩展欧几里得)

2016-04-05 21:26 369 查看
题目链接:http://poj.org/problem?id=1061

题目大意:中文题,不解释

方法:设t为A青蛙和B青蛙的跳的次数,k为绕地球绕的圈数

则得出公式:(x+m*t)- (y+n*t)= k*L

化简得:(x-y)+(m-n)*t = k*L

    化简:(m-n)*t - k*L = y-x(由扩展欧几里得定理可知:存在x0,y0使得a*x0+b*y0=gcd(a,b))

最终t = (x0+k(L/d))

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define LL long long

using namespace std;

LL exgcd(LL a,LL b,LL &x,LL &y)
{
LL r,t;
if(b==0)
{
x=1, y=0;
return a;
}
r=exgcd(b, a%b, x, y);
t=x;
x=y;
y=t-a/b*y;
return r;
}
int main()
{
LL n, L, m, x, y, t;
LL xx, yy;
cin>>x>>y>>m>>n>>L;
LL d = exgcd(n-m, L, xx, yy);
if((x-y) % d != 0)
cout<<"Impossible"<<endl;
else
cout<<((xx*(x-y)/d)%(L/d)+(L/d))%(L/d)<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: