poj1061青蛙的约会(扩展欧几里德)

http://poj.org/problem?id=1061

这里解释的很详细。。http://blog.csdn.net/SwordHoly/article/details/4423543

View Code
 1 #include <iostream>

 2 #include<cstdio>

 3 #include<string.h>

 4 #include<cmath>

 5 #define LL long long

 6 using namespace std;

 7 LL k,t,d;

 8 LL exgcd(LL a,LL b)

 9 {

10     if(b==0)

11     {

12         k = 1;

13         t = 0;

14         d = a;

15         return a;

16     }

17     LL egcd = exgcd(b,a%b);

18     LL temp = k;

19     k = t;

20     t = temp-(a/b)*t;

21     return egcd;

22 }

23 int main()

24 {

25     LL L,a,b,x,y,m,n;

26     cin>>x>>y>>m>>n>>L;

27     a = m-n;

28     b = y-x;

29     if(a<0)

30     {

31         a = -a;

32         b = -b;

33     }

34     exgcd(a,L);

35     if(b%d!=0)

36     printf("Impossible\n");

37     else

38     {

39          k = k*b/d;

40          L = L/d;

41          if(k>=0)

42          k = k%L;

43          else

44          k = k%L+L;

45          cout<<k<<endl;

46     }

47     return 0;

48 }

 

你可能感兴趣的:(poj)