标签:拓展的欧几里得
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
由给定的关系有公式『 (x-y)+T*(m-n)=p*l 』可得出a,b,c的值然后进行exgcd和取模运算,
由于X,Y在局部全局都有结果WA了好几次。。。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
LL x,y;
LL exgcd(LL a,LL b)
{
if(!b)
{
y=0;
x=1;
return a;
}
LL d=exgcd(b,a%b);
LL g=x;
x=y;
y=g-y*(a/b);
return d;
}
LL mod(LL a,LL b)
{
if(a>=0)
return a%b;
else
return a%b+b;
}
int main()
{
LL n,m,k,i,j,l,a,b,d,x1,y1,c;
while(~scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l))
{
a=n-m;
b=l;
c=x-y;
d=exgcd(a,b);
if(c%d)
{
printf("Impossible\n");
continue;
}
b/=d;//为了使b变小,加速运行
x=mod(x*c/d,b);//a*x=(恒等)bmod(n)
printf("%lld\n",x);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:拓展的欧几里得
原文地址:http://blog.csdn.net/grit_icpc/article/details/47998531