[POJ1601]青蛙的约会(exgcd)

来源:互联网 发布:无实体软件出口 编辑:程序博客网 时间:2024/05/17 13:42

题目:

我是超链接

题解:

好久以前写过的题目,但以前写的太丑了,并且我对exgcd的理解更深了一步,我决定重新写这篇blog!
其实就是让求
(mn)xyx(modL)

代码:

#include <cstdio>#include <iostream>#define LL long longusing namespace std;LL gcd(LL a,LL b){if (!b) return a;else return gcd(b,a%b);}void exgcd(LL a,LL b,LL &x,LL &y){if (!b) x=1,y=0;else exgcd(b,a%b,y,x),y-=(a/b)*x;}int main(){    LL xx,yy,m,n,L,a,b,c;    scanf("%lld%lld%lld%lld%lld",&xx,&yy,&m,&n,&L);    if (m-n<0) {swap(xx,yy); swap(m,n);}    LL d=gcd(m-n,L);    if ((yy-xx)%d!=0) {printf("Impossible"); return 0;}    c=(yy-xx)/d;    a=(m-n)/d;b=L/d;    LL x,y;    exgcd(a,b,x,y);    printf("%lld",(x*c+b)%b);}

普及向

安利一波优秀的学姐

原创粉丝点击