青蛙的约会
扩展欧几里得
简单的经典题目
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #define Mod 1000000007 7 #define SMod 10007 8 #define lll __int64 9 #define ll long long 10 using namespace std; 11 #define N 1000007 12 13 ll exgcd(ll a,ll b,ll &x,ll &y) 14 { 15 if(!b) 16 { 17 x = (ll)1,y = (ll)0; 18 return a; 19 } 20 ll r = exgcd(b,a%b,x,y); 21 ll t = x; 22 x = y; 23 y = t - a/b*y; 24 return r; 25 } 26 27 ll gcd(ll a,ll b) 28 { 29 if(!b) 30 return a; 31 return gcd(b,a%b); 32 } 33 34 int main() 35 { 36 ll x,y,m,n,L; 37 ll kx,ky; 38 while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)!=EOF) 39 { 40 ll nm = n-m; 41 ll delta = x-y; 42 if(nm < 0) 43 { 44 nm = m-n; 45 delta = y-x; 46 } 47 ll d = exgcd(nm,L,kx,ky); 48 if(delta%d) 49 { 50 puts("Impossible"); 51 continue; 52 } 53 ll t = L/d; 54 //printf("%lld\n",t); 55 printf("%lld\n",res); 56 } 57 return 0; 58 }