Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
题目大意:
2只青蛙分别站在x和y处,每次分别能跳m和n米,维度线总长L,求跳了几次后会碰面,若永远不能碰面,则输出-1.参考代码:
#include<map> #include<stack> #include<queue> #include<cmath> #include<vector> #include<cctype> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const double eps=1e-10; const int INF=0x3f3f3f3f; const int MAXN=1100; typedef long long LL; LL m,n,x,y,L; LL gcd(LL a,LL b) { return b?gcd(b,a%b):a; } void exgcd(LL a,LL b,LL c,LL& ansx,LL& ansy) { if(b==0) { c=a; ansx=1; ansy=0; } else { exgcd(b,a%b,c,ansy,ansx); ansy-=ansx*(a/b); } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L)!=EOF) { LL a=n-m,b=L,c=x-y; LL g=gcd(a,b); if(c%g||m==n) { printf("Impossible\n"); continue; } LL ansx,ansy; a/=g; b/=g; c/=g; exgcd(a,b,c,ansx,ansy);//a*ansx+b*ansy=c ansx*=c; ansx%=b; while(ansx<0)//ansx可能为负数 ansx+=L; printf("%lld\n",ansx); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/noooooorth/article/details/47735609