洛谷 P1516

来源:互联网 发布:虚拟机ubuntu nat上网 编辑:程序博客网 时间:2024/06/05 20:03

【题目分析】
拓展欧几里得


【代码】

#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>#define L long longusing namespace std;L x,y,m,n,l,d,p,q;inline void exgcd(L a,L b,L&d,L &x,L &y)//拓展的欧几里得 ,顺便计算最大公约数 {    if (b==0) {x=1;y=0;d=a;return;}    exgcd(b,a%b,d,y,x);    y-=x*(a/b);}int main(){    scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);//猥琐的读入     exgcd(m-n,l,d,p,q);    if ((y-x)%d)    {        printf("Impossible\n");        return 0;    }    L tmp=abs(l/d);    printf("%lld\n",((p*(y-x)/d)%tmp+tmp)%tmp);}
0 0
原创粉丝点击