fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. map<int, int>vis;
  4. int main(){
  5.  
  6. int a, b;
  7. int d, c;
  8. cin >> a >> b >> c >> d;
  9.  
  10. map<int, int>vis;
  11. for (int i = 0; i <= 150; i++){
  12. vis[b + a*i] = 1;
  13. }
  14. for (int i = 0; i <= 150; i++){
  15. if (vis[d + c*i])
  16. return cout << d + c*i << endl, 0;
  17. }
  18. puts("-1");
  19. return 0;
  20. }
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
Success #stdin #stdout 0s 16064KB
stdin
5 20
9 20
stdout
20