您的位置:首页 > 其它

poj 1061 青蛙的约会

2014-02-24 23:39 253 查看
扩展欧几里德算法的练习题,直接调用exgcd函数就可以了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;

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