A Problem
可以抽象地看做数轴上有两个点,坐标为a,c,每个点一次可以向右移动b,d,问两点最快在哪个坐标能重叠,若不能则输出-1(a,b,c,d <= 100)
Solution
暴力,扩展欧几里得
Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int a,b,c,d,e,f;
cin >> a >> b >> c >> d;
for (int i=1;i<=100000;++i){
if (b < d) b += a;
else if (b > d) d += c;
else{
cout << b << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}