Poj--1061(拓展欧几里得)

2014-12-12 12:14:58

思路:嘛,一道exgcd水题,列下方程:k*(m - n) + p*L = y - x,以k为a,L为b,(m-n)为x,p为y,y-x为c,建立:ax+by=c,若gcd(m-n,L)|c则有解,否则无解。最后算出解要转化为最小正整数(形式:x0 + t * (b / gcd(m-n,L)))

 1 /*************************************************************************
 2     > File Name: 1061.cpp
 3     > Author: Natureal
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Fri 12 Dec 2014 11:56:40 AM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 
27 ll X,Y,M,N,L;
28 
29 ll Ex_gcd(ll a,ll b,ll &x,ll &y){
30     if(b == 0){
31         x = 1;
32         y = 0;
33         return a;
34     }
35     ll r = Ex_gcd(b,a % b,x,y);
36     ll t = x;
37     x = y;
38     y = t - a / b * y;
39     return r;
40 }
41 
42 int main(){
43     while(scanf("%I64d%I64d%I64d%I64d%I64d",&X,&Y,&M,&N,&L) != EOF){
44         ll a = M - N,b = L,c = Y - X,d,x,y;
45         d = Ex_gcd(a,b,x,y);
46         if(c % d)
47             printf("Impossible\n");
48         else{
49             x *= c / d;
50             ll k = L / d;
51             if(k < 0) k = -k;
52             x = (x % k + k) % k;
53             printf("%I64d\n",x);
54         }
55     }
56     return 0;
57 }

 

posted @ 2014-12-12 12:18  Naturain  阅读(119)  评论(0编辑  收藏  举报