标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 94857 | Accepted: 17597 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
Source
/** 题意:两只青蛙 A的初始位置是x,每次跳n;B的初始位置是y,每次跳m,总长度为l; 做法:设两只青蛙跳了t后相遇,A的为位置为 x + t * n ;B 的位置是 y + t*m 并且 x + t * n - y + t*m = k * l; 可得(n - m) * t + k * l = y - x 可以得到 扩展欧几里得 a * x + b* y = GCD(a,b); 求出x0,y0; 其中a = n-m;b = l , x = x0 * (y-x ) / GCD(a,b) ,y = y0 * ( y - x)/GCD(a,b) 在扩展欧几里的中a/gcd(a,b) 为整数,b/gcd(a,b) 也是整数,所以在判断如果(y-x) % gcd(a,b) 不是整数,则无解; **/ #include <iostream> #include <cmath> #include <string.h> #include <stdio.h> using namespace std; void swap(long long &x, long long &y) { long long t; t = x; x = y; y = t; } int gcd(long long a,long long b) { if(b == 0) return a; return gcd(b,a%b); } void extend_gcd(long long a,long long b,long long d,long long &x,long long &y) { if(a == 0 && b == 0) return; if(b == 0) { d = a; x = 1; y = 0; return; } extend_gcd(b,a%b,d,y,x); y -= a/b *x; } long long solve(long long a,long long b,long long n) { long long tmp,tt,x1,y1; tmp = gcd(a,b); if(n%tmp) return -1; extend_gcd(a,b,tmp,x1,y1); tt = (n*x1 /tmp) %(b/tmp); if(tt <0) tt += (b/tmp); return tt; } int main() { long long x,y,n,m,l; while(~scanf("%lld %lld %lld %lld %lld",&x,&y,&n,&m,&l)) { long long a,b,c; if(n < m) { swap(n,m); swap(x,y); } a = n-m; c = y - x; if(c < l) c += l; long long ans = solve(a,l,c); if(ans == -1) printf("Impossible\n"); else printf("%lld\n", ans); } }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4474713.html