标签:des style color os io strong div sp html
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 91134 | Accepted: 16678 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
扩展欧几里德的应用。
分析:设青蛙A经过s步之后追上青蛙B。则满足以下式子:
(x+s*m)-(y+s*n)=k*L(k=0,1,2...)变形得:
(n-m)*s+k*L=x-y;令a=n-m ;b=L;c=x-y;得
a*s+b*k=c;(一元二次方程)原方程①
只要上式存在整数解,则两只青蛙可以相遇,否则不能;如果有解,我们需要求出min_s;
令r=gcd(a,b)由扩展gcd可以求出ax+by=r;的解,若令a/=r,b/=r;则可以求出a‘x+b‘y=1 的一组解(x0,y0),两边同时乘以c, 得a‘cx+b‘cy=c;(即为原方程①)那么x0*c就是原方程的一个解,令x1=x0*c,那么(x1%b+b)%b即为原方程的一组x的非负最小解。(加b是防止x1是负值)
#include <iostream> #include <cstring> #include <cstdio> #include <cctype> #include <cstdlib> #include <algorithm> #include <set> #include <vector> #include <string> #include <cmath> #include <map> #include <queue> using namespace std; #define LL long long LL gcd(LL a,LL b) { if(b==0)return a; return gcd(b,a%b); } void exgcd(LL a,LL b,LL &x,LL &y) { if(b==0) { x=1; y=0; return ; } exgcd(b,a%b,y,x); y-=a/b*x; } int main() { LL x,y,m,n,L,s,k; cin>>x>>y>>m>>n>>L; LL a=n-m; LL b=L; LL c=x-y; LL r=gcd(a,b); if(c%r) { puts("Impossible"); return 0; } a/=r;b/=r;c/=r; exgcd(a,b,s,k); if(b<0)b=-b; s*=c; cout<<(s%b+b)%b<<endl; return 0; }
标签:des style color os io strong div sp html
原文地址:http://blog.csdn.net/qq_16255321/article/details/39054775