标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 105587 | Accepted: 20789 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
#include <iostream> using namespace std; typedef __int64 LL;//int前双‘_‘ LL extgcd(LL a,LL b,LL &x,LL &y) { LL d=a; if(b!=0) { d=extgcd(b,a%b,y,x); y-=(a/b*x); } else { x=1;y=0; } return d; } LL gcd(LL a,LL b) { if(b==0) return a; else return gcd(b,a%b); } LL s1,s2,v1,v2,m; int main() { while(cin>>s1>>s2>>v1>>v2>>m) { //两者相遇的条件 s1+v1*t=s2+v2*t-k*m => (v1-v2)*t+m*k=s2-s1 //得线性同余方程 ax+by=c (a:v1-v2,x:t,b:m,k:y,c:s1-s1) LL a=v1-v2; if(a<0) a+=m; LL b=m; LL c=s2-s1; if(c<0) c+=m; LL div=gcd(a,b); if(c%div!=0) //同余方程ax+by=c.有解的充要条件是 c|gcd(a,b). { cout<<"Impossible"<<endl; continue; } a/=div;//将各个系数均缩小div倍 b/=div;//ax+by=c => a‘x+b‘y=c‘ c/=div; LL x=0,y=0; extgcd(a,b,x,y);//求解线性同余方程 ax+by=1 x=(x*c)%b;//扩展欧几里得求的是ax+by=1中的x,结果需要将x扩大c倍 while(x<0) x+=b; cout<<x<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5701139.html