您的位置:首页 > 其它

bzoj 1477 exgcd

2017-08-17 16:54 169 查看
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
ll x,y,m,n,l,a,b,c,d;
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0) {x=1;y=0;return ;}
exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-y*(a/b);
}
ll gcd(ll a,ll b) {return b==0?a:gcd(b,a%b);}
int main()
{
scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);
a=n-m;b=l;c=x-y;
d=gcd(a,b);
if(c%d!=0) printf("Impossible\n");
else {
c/=d;a/=d;b/=d;
exgcd(a,b,x,y);
x=((x*c)%b+b)%b;
if(!x) x+=b;
printf("%lld\n",x);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: