标签:code alt 并且 点击 要求 一个 style none ==
1 2 3 4 5
4
解题思路:
因为两只青蛙有可能在绕弯大于一圈的维度才相遇到,这个问题实际上就是取余的问题
x + k * m = L * t1+ 余数
y + k * n = L * t2 + 余数
方程两边相减:(m - n)*k + Lt = y - x;
a=m-n,b=L,c=y-x;
ax+by=c;
题目求x最小 a(x-D)+b(y-a*D/b)=c;
尽可能D大 aD%a=0,aD%b=0--> aD=klcm(a,b)--->D=k*b/gdc(a,b);
通解 :x=x0+b/gdc(a,b)*t;
y=y0-a/*gdc(a,b)*t;
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <map> 5 using namespace std; 6 7 typedef long long ll; 8 ll x,y,m,n,L; 9 ll X,Y; 10 11 ll gdc(ll a,ll b){ 12 return b==0?a:gdc(b,a%b); 13 } 14 15 void exgdc(ll a,ll b,ll &X,ll &Y){ 16 if(b==0){ 17 X=1; 18 Y=0; 19 return; 20 } 21 exgdc(b,a%b,X,Y); 22 int temp=X; 23 X=Y; 24 Y=temp-a/b*Y; 25 } 26 27 28 int main(){ 29 ios::sync_with_stdio(false); 30 cin>>x>>y>>m>>n>>L; 31 ll a=m-n,b=L,c=y-x; 32 ll GDC=gdc(a,b); //最大公约数 33 exgdc(a,b,X,Y); 34 if(c%GDC!=0){ 35 cout << "Impossible" << endl; 36 } 37 else{ 38 ll res=X*(c/GDC); //一个特解; 39 ll ans=b/GDC; 40 if(ans<0) ans=-ans; 41 res%=ans; 42 if(res<0) res+=ans; 43 cout << res << endl; 44 } 45 return 0; 46 }
标签:code alt 并且 点击 要求 一个 style none ==
原文地址:https://www.cnblogs.com/qq-1585047819/p/11332080.html