bzoj1477 青蛙的约会

题目链接

exgcd

已知a,b求一组正整数解x,y使得ax+by=gcd(a,b);

我为什要做这样的水题?因为我很弱

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<string>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<map>
 9 #include<set>
10 using namespace std;
11 typedef long long LL;
12 LL x,y,n,m,l;
13 void exgcd(LL a,LL b,LL &x,LL &y)
14 {
15     if(!b){x=1,y=0;return ;}
16     exgcd(b,a%b,x,y);
17     LL t=x;x=y,y=t-a/b*y;
18 }
19 LL gcd(LL a,LL b)
20 {
21     LL c;
22     while(a%b)c=a%b,a=b,b=c;
23     return b;
24 }
25 int main()
26 {
27     scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l);
28     LL s,k;
29     LL a=n-m,b=l,c=x-y;
30     LL yue=gcd(a,b);
31     if(c%yue)
32     {
33         printf("Impossible\n");
34         return 0;
35     }
36     a/=yue,b/=yue,c/=yue;
37     exgcd(a,b,s,k);
38     s=((c*s)%b+b)%b;
39     if(!s)s+=b;
40     printf("%lld\n",s);
41     return 0;
42 }

 

posted @ 2016-03-06 21:01  HugeGun  阅读(133)  评论(0编辑  收藏  举报