solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
a,b=map(int,input().split()) c,d=map(int,input().split()) while b!=d and max(b,d)<pow(10,4)+1: if b<d:b+=a else:d+=c if b==d:print(b) else:print(-1)
7
PYTHON3
import sys a, b = map(int, input().split()) c, d = map(int, input().split()) times1 = [b + a*i for i in range(100)] for time2 in [d + c*i for i in range(100)]: if time2 in times1: print(time2) sys.exit() print(-1)
7
PYTHON3
a,b=map(int,input().split()) c,d=map(int,input().split()) f=0 for i in range(0,101): for j in range(0,101): if(b+i*a==d+j*c): print(b+i*a) f=1 break if(f==1): break if(f==0): print(-1)
7
PYTHON3
import sys a, b = map(int, input().split()) c, d = map(int, input().split()) cur1, cur2 = b, d x = set() y = set() for i in range(100000): x.add(cur1) y.add(cur2) cur1 += a cur2 += c for i in range(100000): if i in x and i in y: print(i) sys.exit(0) print(-1)
7
PYTHON3
# -*- coding: utf-8 -*- st = input() i = 0 n = '' m = '' while st[i] != " ": n = n + st[i] i += 1 i += 1 while i < len(st): m = m + st[i] i += 1 a = int(n) b = int(m) st = input() i = 0 n = '' m = '' while st[i] != " ": n = n + st[i] i += 1 i += 1 while i < len(st): m = m + st[i] i += 1 c = int(n) d = int(m) k = 0 for i in range(1000000): b1 = b + a*i if ((b1 - d)% c == 0) and ((b1 - d) >= 0) : print(b1) k = 1 break if k != 1: print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) """ def nok(a, b): r = a while r % b != 0: r += a return r def get_answer(): if b == d: return 0 if c == a: return -1 if (b - d) * (c - a) < 0: return -1 k = nok(abs(b - d), abs(c - a)) return b + k * a """ def get_answer(): for i in range(101 * 2): for j in range(101 * 2): if b + a*i == d + c*j: return b + a*i return -1 print(get_answer())
7
PYTHON3
x,y=map(int,input().split()) p,q=map(int,input().split()) for i in range(1,101): for j in range(1,101): if (y+(i-1)*x)==(q+(j-1)*p): print(y+(i-1)*x) exit() print(-1)
7
PYTHON3
x,y=map(int,input().split()) z,t=map(int,input().split()) tmp=0 def timluot(a,b,c,d): i=0 tm=0 while i<=200: j=0 tm=b+a*i while True: tmp=d+c*j if tmp==tm: return tmp elif tmp>tm: break j+=1 i+=1 return 0 tmp=timluot(x,y,z,t) if tmp!=0: print(tmp) else: print(-1)
7
PYTHON3
a,b = list(map(int, input().split())) c,d = list(map(int, input().split())) check=-1 #print("A") for i in range(101): for j in range(101): if b+i*a==d+j*c: check=1 print(b+i*a) break if check==1: break if check==-1: print("-1")
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) ans = -1 for i in range(int(1e6)): if b < d: b += a elif d < b: d += c else: ans = d break print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long ar[500005]; long long br[500005]; int main() { long long a, b, c, d, i, j, k, m, n, t, sum, f; cin >> a >> b; cin >> c >> d; for (i = 0; i < 101; i++) ar[i] = b + i * a; for (i = 0; i < 101; i++) br[i] = d + i * c; for (i = 0; i < 101; i++) for (j = 0; j < 101; j++) if (ar[i] == br[j]) { cout << ar[i] << endl; return 0; } cout << -1 << endl; }
7
CPP
a, b = list(map(int, input().split())) c, d = list(map(int, input().split())) t1 = b t2 = d solution_found = False for i in range(10000): if (t1 == t2): print(t1) solution_found = True break elif (t1 > t2): t2 += c else: t1 += a if (not solution_found): print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) same_time = 0 coeff = 0 count = 0 if (a % 2 == 0 and b % 2 != 0) and c % 2 == 0 and d % 2 == 0: print("-1") elif (a % 2 == 0 and b % 2 == 0) and c % 2 == 0 and d % 2 != 0: print("-1") elif b == d: print(b) else: if b > d: g = a h = b i = c j = d else: g = c h = d i = a j = b # Think of each scream as just an addition to the last??? # ab a+b, b+a+a, b+a+a+a.... new_gh = h new_ij = j while True: # test until one one is larger than the other, then flip if new_gh / new_ij == 1: print(new_gh) break if new_gh > new_ij: new_ij += i if new_gh < new_ij: new_gh += g count += 1 if count == 10000: print(-1) break
7
PYTHON3
flag=1 a,b=map(int,input().split()) c,d=map(int,input().split()) for i in range(1,1000): if flag==1: for j in range(1,1000): if b+a*i-a-d-c*j+c==0: print(b+(i-1)*a) flag=0 break else: break if flag==1: print("-1")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int i = 0, n, a, b = 0, c = 0, e, m, d, t = 0, x, sum = 0, y, f = 0, o, z = 0, h = 0, l = 0, k = 0, r, x1, x2, y1, y2; cin >> a >> b; cin >> c >> d; for (i = 0; i < 100000; i++) { if (b == d) { cout << b; z = 1; break; } else if (b < d) { b = b + a; } else { d = d + c; } } if (z == 0) { cout << "-1"; } }
7
CPP
b, a = map(int, input().split()) d, c = map(int, input().split()) if a < c: a, b, c, d = c, d, a, b R = [a+i*b for i in range(d) if (a+i*b - c) % d == 0] if R: print(min(R)) else: print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) checker = False for i in range(100): for k in range(100): if b+i*a == d+k*c: print(b+i*a) checker = True if checker: break if checker: break if not checker: print("-1")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e[10100], f[10100], i, j; cin >> a >> b; cin >> c >> d; for (i = 0; i < 100; i++) { e[i] = b + i * a; for (j = 0; j < 100; j++) { f[j] = d + j * c; if (e[i] == f[j]) { cout << e[i] << endl; return 0; } } } cout << -1; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 1000000001; using ll = long long; using ull = unsigned long long; struct ToggleSeparator {}; template <ostream *Out> struct DumpFirst {}; template <ostream *Out> struct Dumper { static void *&last() { static void *p; return p; } static const char *&separator() { static const char *s = " "; return s; } static const char *&separatorNewLine() { static const char *s = "\n"; return s; } static const char *&unseparator() { static const char *s = "="; return s; } Dumper() { last() = this; } ~Dumper() { if (this == last()) { *Out << separatorNewLine(); last() = 0; } } Dumper operator,(ToggleSeparator) { std::swap(separator(), unseparator()); return Dumper(); } }; template <ostream *Out, class T> Dumper<Out> operator,(Dumper<Out>, const T &e) { *Out << Dumper<Out>::separator() << e; return Dumper<Out>(); } template <ostream *Out, class T> Dumper<Out> operator,(DumpFirst<Out>, const T &e) { *Out << e; return Dumper<Out>(); } template <class T, class Y> ostream &operator<<(ostream &o, const pair<T, Y> &p) { return o << p.first << ' ' << p.second; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { bool isBegin = true; for (auto &e : v) { if (!isBegin) { o << ' '; } isBegin = false; o << e; } return o; } template <class T> ostream &operator<<(ostream &o, const set<T> &v) { bool isBegin = true; for (auto &e : v) { if (!isBegin) { o << ' '; } isBegin = false; o << e; } return o; } template <class T> ostream &operator,(ostream &o, const T &t) { return o << t; } struct StdinReader { StdinReader() { ios::sync_with_stdio(0); } } stdinReader; template <class T> StdinReader &operator,(StdinReader &sr, T &t) { cin >> t; return sr; } template <class T> StdinReader &operator,(StdinReader &sr, vector<T> &v) { for (auto &x : v) sr, x; return sr; } template <class T, class U> StdinReader &operator,(StdinReader &sr, pair<T, U> &p) { return sr, p.first, p.second; } template <class T, class U> bool remin(T &a, const U &b) { return b < a ? a = b, true : false; } template <class T, class U> bool remax(T &a, const U &b) { return b > a ? a = b, true : false; } template <class T> int size(const T &t) { return (int)t.size(); } int main() { int a, b, c, d; stdinReader, a, b, c, d; int ans = -1; vector<int> v; v.reserve(10000); for (int i = 0; i < (10000); ++i) { v.push_back(b); b += a; } for (int i = 0; i < (10000); ++i) { if (binary_search((v).begin(), (v).end(), d)) { ans = d; break; } d += c; } DumpFirst<&std::cout>(), ans; }
7
CPP
a,b=map(int,input().split()) c,d=map(int,input().split()) ans=-1 for i in range(1,101): x=b+(i-1)*a for j in range(1,101): y=d+(j-1)*c if(y==x): ans=y print(ans) quit(0) if(y>x): break print(ans)
7
PYTHON3
b,a = map(int,input().split()) d,c = map(int,input().split()) ans = -1 ar = [a+b*i for i in range(100)] br = [c+d*i for i in range(100)] i = 0 j = 0 while i<100 and j<100: if ar[i]==br[j]: ans = ar[i] break elif ar[i]<br[j]: i+=1 else: j+=1 print(ans)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) ans = -1 dic = {} for i in range(200): dic[b + a*i] = 1 for i in range(200): if d + i*c in dic: print(d + i*c) break else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 9; const int INF = 1e9 + 5; const int mod = 1e9; int main() { int a, b, c, d; cin >> a >> b >> c >> d; set<int> kek; for (int i = 0; i < 1e5; i++) { kek.insert(b + a * i); } for (int i = 0; i < 1e5; i++) if (kek.find(d + c * i) != kek.end()) { cout << d + c * i; return 0; } cout << -1; return 0; }
7
CPP
a,b=input().split(" ") c,d=input().split(" ") a=int(a) b=int(b) c=int(c) d=int(d) s=b s1=d j=0 if(not (a&(a-1)==a-1) and not (c&(c-1)==c-1)): if(b&(b-1)==b-1 and not (d&(d-1)==d-1)): print("-1") j=1 while(j==0 and s<100000): if(s==s1): print(s) j=1 elif(s1>s): s=s+a else: s1=s1+c if(j==0): print("-1")
7
PYTHON3
# -*- coding: utf-8 -*- """ Created on Sun Dec 16 12:35:38 2018 @author: asus """ a,b=(int(x) for x in input().split()) c,d=(int(x) for x in input().split()) while b!=d and b<10**4: if b<d: b+=a else: d+=c if b!=d: print(-1) else: print(b)
7
PYTHON3
def compute_gcd(a, b): if a == 0: return b return compute_gcd(b % a, a) def main(): a, b = map(int, input().split()) c, d = map(int, input().split()) gcd = compute_gcd(a, c) if (b - d) % gcd != 0: print(-1) steps = 1000 size = max(b, d) + max(a, c) * steps used = [False] * size x = b for _ in range(steps): used[x] = True x += a x = d for _ in range(steps): if used[x]: print(x) break x += c if __name__ == '__main__': main()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int i, a, b, c, d; bool ok = false; cin >> a >> b >> c >> d; for (i = 0; i <= 100; ++i) { int dif = b - d + i * (a - c); if (dif % c == 0 && i * a + b >= d) { ok = true; break; } } if (ok == true) cout << i * a + b; else cout << -1; return 0; }
7
CPP
ab = list(map(int, input().split())) a = ab[0] b = ab[1] cd = list(map(int, input().split())) c = cd[0] d = cd[1] list1 = [] list2 = [] counter1= 0 counter2 = 0 for i in range(max(a + b,c + d)): list1.append(counter1 * a + b) list2.append((counter2 * c + d)) counter1 += 1 counter2 += 1 for i in range(len(list1)): if int(list1[i]) in list2: print(list1[i]) break if i == len(list1) - 1: print(-1) break
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, i, j, flag = 0; scanf("%d %d %d %d", &a, &b, &c, &d); for (i = 0; i <= 100; i++) for (j = 0; j <= 100; j++) { if (b + a * i == d + c * j) { printf("%d", b + a * i); return 0; } } if (flag == 0) printf("-1"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; double n; cin >> a >> b; cin >> c >> d; if (b == d) { cout << b << endl; return 0; } for (int i = 0; i <= 100000; i++) { n = 1.00 * ((d - b) + c * i) / a; if (ceil(n) == floor(n) && n >= 0) { cout << (int)(b + n * a) << endl; return 0; } } cout << -1 << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main(void) { ios::sync_with_stdio(false); int a, b, c, d; cin >> a >> b >> c >> d; if (b <= d) { unsigned long long k = 0, x = d; d -= b; d %= a; vector<bool> arr(a, false); while (!arr[d] && d != 0) { k++; arr[d] = true; d += c; d %= a; } if (arr[d]) cout << -1 << "\n"; else cout << c * k + x << "\n"; } else { unsigned long long k = 0, x = b; b -= d; b %= c; vector<bool> arr(c, false); while (!arr[b] && b != 0) { k++; arr[b] = true; b += a; b %= c; } if (arr[b]) cout << -1 << "\n"; else cout << a * k + x << "\n"; } return 0; }
7
CPP
import math def bezout(m,n,t): x=0 while((t-m*x)%n!=0): x+=1 return (x,int(t-m*x)/n) a,b=map(int,input().split()) c,d=map(int,input().split()) if (d-b)%(math.gcd(a,c)) !=0: print(-1) else: (t1,t2)=bezout(a,c,d-b) while(t2>0): t1+=int(c/math.gcd(a,c)) t2-=int(a/math.gcd(a,c)) print(b+a*t1)
7
PYTHON3
gcd = lambda a, b: gcd(b, a % b) if b else a a, b = map(int, input().split()) c, d = map(int, input().split()) if (d - b) % gcd(a, c): print(-1) else: while True: if b == d: print(b) break if b < d: b += a else: d += c
7
PYTHON3
import collections a,b=map(int,input().split()) c,d=map(int,input().split()) v=collections.defaultdict(int) i=0 while True: if b-d>0: if ((b-d)+a*i)%c==0: print(b+a*i) break if v[((b-d)+a*i)%c] not in v: v[((b-d)+a*i)%c]+=1 else: print(-1) break else: if ((d-b)+c*i)%a==0: print(d+i*c) break if v[((d-b)+c*i)%a] not in v: v[((d-b)+c*i)%a]+=1 else: print(-1) break i+=1
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) e = [] g = [] l = 0 for i in range(101): h = b+i*a e.append(h) k = d+i*c g.append(k) #print(e) #print(g) for i in range(len(e)): if e[i] in g: l = 1 s = e[i] break if(l == 1): print(e[i]) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a, b, c, d, mn = -111111; int main() { ios_base::sync_with_stdio(0); cin >> a >> b >> c >> d; for (long long i = 0; i <= 4000; i++) { for (long long j = 0; j <= 4000; j++) { if (b + i * a == d + j * c) { if (mn == -111111) { mn = d + j * c; } else mn = min(mn, d + j * c); } } } if (-111111 == mn) cout << -1; else cout << mn; return 0; }
7
CPP
import sys import math import itertools import functools import collections import operator import fileinput import copy ORDA = 97 # a def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return [int(i) for i in input().split()] def lcm(a, b): return abs(a * b) // math.gcd(a, b) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=2): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): new_number = 0 while number > 0: new_number += number % base number //= base return new_number def cdiv(n, k): return n // k + (n % k != 0) def ispal(s): # Palindrome for i in range(len(s) // 2 + 1): if s[i] != s[-i - 1]: return False return True # a = [1,2,3,4,5] -----> print(*a) ----> list print krne ka new way def main(): a,b = mi() c,d = mi() g = math.gcd(a,c) if (d-b)%g != 0: print(-1) exit() while b != d: if b < d: b += a else: d += c print(b) main()
7
PYTHON3
import sys input = sys.stdin.readline a, b = map(int, input().split()) c, d = map(int, input().split()) found = False for i in range(int(1e6)): j = (a*i + b - d)/c if j % 1 == 0 and j >= 0: print(a*i + b) found = True break if not found: print(-1)
7
PYTHON3
a, b = [int(v) for v in input().split(' ')] c, d = [int(v) for v in input().split(' ')] # Il ragionamento qui è che dobbiamo imporre # b + ai = d + cj # che è un'eq. diofantea che ha soluzione sse gcd(c, a) | (d - b) -- [Hardy & Wright, An introduction to the theory of numbers, thm.25] # siccome inoltre a, b, c e d sono abbastanza piccoli una volta appurato questo andiamo di brute force. # attenzione alle condizioni da imporre su i e j che devono essere interi >= 0. # Non mi aspetto nessuna soluzione corretta da parte dei ragazzi # perchè è un pochino "avanzata", ma secondo me è uno di quei problemi su cui vale la pena perdere mezz'oretta. def gcd(x, y): if x == 0: return y return gcd(y%x, x) if (d-b)%gcd(c, a) != 0: print(-1) else: finished = False i = 0 while not finished: j = (b+a*i-d)//c if b + a*i == d + c*j and j >= 0: print(b + a*i) finished = True i += 1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long a, b, c, d; cin >> a; cin >> b; cin >> c; cin >> d; long long z = abs(d - b); long long e = gcd(a, c); if (z % e != 0) cout << -1; else { long long ans = 0, i = 0; while (1) { long long p = d - b + c * i; if (p % a == 0 && p >= 0) { ans = p / a; break; }; i++; }; long long xv = b + a * ans; cout << xv; }; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (abs(b - d) % gcd(a, c)) { cout << -1; return 0; } int x = b; while (1) { if ((x - d) >= 0 && (x - d) % c == 0) { cout << x; return 0; } x += a; } }
7
CPP
a,b=map(int,input().split()) c,d=map(int,input().split()) l1=[] l2=[] for i in range(0,1000): l1.append(b+i*a) for j in range(0,1000): if (d+c*j) in l1: print(d+c*j) break else: print(-1)
7
PYTHON3
g=lambda x,y:g(y,x%y)if y else x a,b=map(int,input().split()) c,d=map(int,input().split()) e=g(a,c) if (b-d)% e:print(-1) else: while b!=d: if b<d:b+=a else:d+=c print(b)
7
PYTHON3
def gcd(a, b): if b == 0: return a return gcd(b, a % b) a, b = map(int, input().split()) c, d = map(int, input().split()) morty = set() cr = b for i in range(1000): morty.add(cr) cr += a cr = d for i in range(1000): if cr in morty: print(cr) exit(0) cr += c print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int a1 = 0; int a2 = 0; int R, M; int cnt = 0, flag = 1; while (1) { if (cnt > 1e7) { flag = 0; break; } R = b + a1 * a; M = d + a2 * c; if (R > M) a2++; if (R < M) a1++; if (R == M) break; cnt++; } if (flag) cout << R << endl; else cout << "-1"; return 0; }
7
CPP
arr = input().split(' ') a = int(arr[0]) b = int(arr[1]) arr = input().split(' ') c = int(arr[0]) d = int(arr[1]) k = set(range(b,10**4,a)) l = set(range(d,10**4,c)) j = k & l print(min(j) if j else -1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) for x in range(101): y = (b + a*x - d) * 1.0/c if y % 1 == 0 and y >= 0: print (b+a*x) exit() print ("-1")
7
PYTHON3
import sys a, b = [int(x) for x in input().split()] c, d = [int(x) for x in input().split()] if a < c: b, a, d, c = d, c, b, a foundoverlap = False x = b while x < 100000: if x < d: x = x + a continue if (x - d) % c == 0: print(x) foundoverlap = True break x = x + a if not foundoverlap: print(-1)
7
PYTHON3
a,b=map(int,input().split()) r,l=map(int,input().split()) for i in range(0,101) : for j in range(101) : if a*i+b==l+r*j : print(a*i+b) exit() print(-1)
7
PYTHON3
######### ## ## ## #### ##### ## # ## # ## # # # # # # # # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # ##### # # # # ### # # # # # # # # ##### # # # # # # # # # # # # # # # # # # ######### # # # # ##### # ##### # ## # ## # # """ PPPPPPP RRRRRRR OOOO VV VV EEEEEEEEEE PPPPPPPP RRRRRRRR OOOOOO VV VV EE PPPPPPPPP RRRRRRRRR OOOOOOOO VV VV EE PPPPPPPP RRRRRRRR OOOOOOOO VV VV EEEEEE PPPPPPP RRRRRRR OOOOOOOO VV VV EEEEEEE PP RRRR OOOOOOOO VV VV EEEEEE PP RR RR OOOOOOOO VV VV EE PP RR RR OOOOOO VV VV EE PP RR RR OOOO VVVV EEEEEEEEEE """ """ Perfection is achieved not when there is nothing more to add, but rather when there is nothing more to take away. """ import sys input = sys.stdin.readline # from bisect import bisect_left as lower_bound; # from bisect import bisect_right as upper_bound; # from math import ceil, factorial; def ceil(x): if x != int(x): x = int(x) + 1 return x def factorial(x, m): val = 1 while x>0: val = (val * x) % m x -= 1 return val def fact(x): val = 1 while x > 0: val *= x x -= 1 return val # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; ## gcd function def gcd(a,b): if b == 0: return a; return gcd(b, a % b); ## nCr function efficient using Binomial Cofficient def nCr(n, k): if k > n: return 0 if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return int(res) ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0, hi = None): if hi == None: hi = len(a); while lo < hi: mid = (lo+hi)//2; if a[mid] < x: lo = mid+1; else: hi = mid; return lo; ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0 and n > 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0 and n > 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b; # find function with path compression included (recursive) # def find(x, link): # if link[x] == x: # return x # link[x] = find(link[x], link); # return link[x]; # find function with path compression (ITERATIVE) def find(x, link): p = x; while( p != link[p]): p = link[p]; while( x != p): nex = link[x]; link[x] = p; x = nex; return p; # the union function which makes union(x,y) # of two nodes x and y def union(x, y, link, size): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] prime[0], prime[1] = False, False p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e5 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# spf = [0 for i in range(MAXN)] # spf_sieve(); def factoriazation(x): res = [] for i in range(2, int(x ** 0.5) + 1): while x % i == 0: res.append(i) x //= i if x != 1: res.append(x) return res ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())); def float_array(): return list(map(float, input().strip().split())); ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################### ---------------- TEMPLATE ENDS HERE ---------------- ################### from itertools import permutations import math from bisect import bisect_left def check(n, m, k): print("inside", (n + (k - 1) * m), k * (k + 1) // 2, (n + (k - 1) * m) // k * k) if (n + (k - 1) * m) // k * k <= k * (k + 1) // 2: return True return False def solve(): a, b = map(int, input().split()) c, d = map(int, input().split()) for i in range(0, 101): for j in range(0, 101): if b + i * a == d + j * c: print(b + i * a) return print(-1) if __name__ == '__main__': for _ in range(1): solve() # fin_time = datetime.now() # print("Execution time (for loop): ", (fin_time-init_time))
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) res = -1 for i in range(10000): if (b+a*i-d) % c == 0 and b+a*i-d >= 0: res = b+a*i break print(res)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000009; map<long long int, int> m; int main() { int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 0; i < 100000; i++) { m[b + i * a] = 1; } for (int i = 0; i < 100000; i++) { if (m[d + i * c] == 1) { cout << d + i * c; return 0; } } cout << "-1"; return 0; }
7
CPP
#!/usr/bin/env python3 from fractions import gcd def ri(): return map(int, input().split()) a, b = ri() c, d = ri() if c > a: a, c = c, a b, d = d, b for i in range(10**5): if b-d+i*a >=0 and (b-d+i*a)%c == 0: print(b+i*a) exit() else: print(-1)
7
PYTHON3
from fractions import gcd a, b = [int(x) for x in input().split()] c, d = [int(x) for x in input().split()] g = gcd(a, c) if abs(b - d) % g != 0: print(-1) else: s1 = set([b]) s2 = set([d]) while True: if b in s2: print(b) break if d in s1: print(d) break b += a d += c s1.add(b) s2.add(d)
7
PYTHON3
(a, b) = (int(i) for i in input().split()) (c, d) = (int(i) for i in input().split()) br = -1 for k in range(1000000): n = ((d-b+c*k)/a) if n>=0 and not n%1: br = k break if br!=-1: print(d+c*br) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int ans = -1; map<int, int> m; int a, b, c, d; cin >> a >> b >> c >> d; m[b]++; m[b + a]++; for (int i = 2; i <= 100; ++i) { m[(i * a) + b]++; } if (m[d] == 1) ans = d; else if (m[d + c] == 1) ans = d + c; else { for (int i = 2; i <= 100; ++i) { int x = d + (i * c); if (m[x] == 1) { ans = x; break; } } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; for (int i = 1; i <= t; ++i) { solve(); } return 0; }
7
CPP
a,b=map(int,input().split()) c,d=map(int,input().split()) x=y=0 chk=set([]) for i in range(0,700000,1): x=b+a*i y=d+c*i if x==y or (x in chk and y in chk): print(min(x,y)) exit() if x in chk: print(x) exit() if y in chk: print(y) exit() chk.add(x) chk.add(y) print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f, cs = 10000; cin >> a >> b >> c >> d; e = b; f = d; for (cs; cs > 0; cs--) { if (e < f) e += a; else if (e > f) f += c; else if (e == f) break; } if (cs == 0) { cout << "-1\n"; return 0; } cout << e << "\n"; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5; int a, b, c, d; int main() { scanf("%d%d%d%d", &a, &b, &c, &d); for (int i = max(b, d); i < 100000; i++) { if ((i - b) % a == 0 && (i - d) % c == 0) { printf("%d", i); return 0; } } printf("-1"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, k, l, m, t, a, b, c, d; string str; cin >> a >> b; cin >> c >> d; long long count1 = 0; bool flag = 0; while (count1 <= 20000) { while (b < d) { b += a; count1++; } while (d < b) { d += c; count1++; } if (d == b) { flag = 1; break; } } if (flag) cout << b << '\n'; else cout << -1 << '\n'; return 0; }
7
CPP
a, b = map(int, input().split()) c, d = map(int, input().split()) # b + ai = d + cj # -> i = (d - b + cj) / a cnt = 0 founded = False j = 0 while cnt <= a: i = (d - b + c*j) // a if i >= 0: cnt += 1 if (d - b + c*j) % a == 0: founded = True break j += 1 if founded: print(b + a*i) else: print(-1)
7
PYTHON3
a,b = map(int, input().split()) c, d = map(int, input().split()) #ax - cy = d- b def GCD(a,b): while b != 0: r = a % b a = b b = r return a if (d - b) % GCD(a,c) != 0: print(-1) else: y = 0 while True: x = (d - b + c*y) // a if x >= 0 and (d - b + c*y) % a == 0: print(a*x + b) break y += 1
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) while (b!=d and b<10**5): if b<d: b+=a else: d+=c if (b==d): print(b) else: print(-1)
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) def rick(a,b,r): return b+a*r def morty(c,d,r): return d+c*r def gcd(a,b): if b==0: return a return gcd(b,a%b) pos = d-b if (pos % gcd(a,c) !=0): print(-1) else: i = 0 j = 0 while True: if rick(a,b,i) < morty(c,d,j): i+=1 elif rick(a,b,i) > morty(c,d,j): j+=1 else: print(rick(a,b,i)) break
7
PYTHON3
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") const long long int MAXN = 2e5 + 5; const long long int mod = 1000000007; const long long int N = 200005; void solve() { int a, b, c, d; cin >> a >> b >> c >> d; map<int, int> mp; int dis1 = b, dis2 = d; while (dis1 <= 1e6) { mp[dis1] += 1; dis1 += a; } while (dis2 <= 1e6) { mp[dis2] += 1; dis2 += c; } for (auto [a, b] : mp) { if (b == 2) { cout << a << "\n"; return; } } cout << -1 << "\n"; return; } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, d1; int b, d2, i; cin >> d1 >> a >> d2 >> b; int n, m, KK; KK = max(max(a, b), max(d1, d2)); n = 0; while (true) { if (n > KK) { cout << -1; return 0; } m = 0; while ((b + m * d2) < (a + n * d1)) m++; if ((b + m * d2) == (a + n * d1)) { cout << b + m * d2; return 0; } n++; } }
7
CPP
'''input 69 48 39 9 ''' a, b = map(int, input().split()) c, d = map(int, input().split()) # if (b % 2 != d % 2 and a % 2 == c % 2): # print(-1) # else: for x in range(100): for y in range(100): if b + x*a == d + y*c: print(b + x*a) quit() print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) ans = 1e18 for i in range(1000): for j in range(1000): if b + a*i == d + c*j: ans = min(ans, b + a*i) if ans == 1e18: ans = -1 print(ans)
7
PYTHON3
import sys a,b = map(int,input().split()) c,d = map(int,input().split()) li = [] li1 = [] for i in range(100): for j in range(100): if (b+a*i == d+c*j): print(b+a*i) sys.exit() print("-1")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int a, b, c, d; int main() { cin >> a >> b >> c >> d; while (b != d and b < 1000000) { if (b < d) b = b + a; else d = d + c; } if (b < 100000) cout << b; else cout << "-1"; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a = 0, b = 0, c = 0, d = 0, res = INT_MAX; cin >> a >> b; cin >> c >> d; for (int i = 0; i <= 100; ++i) for (int j = 0; j <= 100; ++j) { if (b + i * a == d + j * c) res = min(res, (b + i * a)); } cout << (res == INT_MAX ? -1 : res); }
7
CPP
from math import gcd def main(): a, b = map(int, input().split()) c, d = map(int, input().split()) n = a * c // gcd(a, c) + b + d l = [*[0] * n, 2] for i in range(b, n, a): l[i] = 1 for i in range(d, n, c): l[i] += 1 i = l.index(2) print(i if i < n else -1) if __name__ == '__main__': main()
7
PYTHON3
#include <bits/stdc++.h> int v[1000001]; using namespace std; int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); while (b <= 1000000) { v[b] = 1; b += a; } while (d <= 1000000) { if (v[d] == 1) { printf("%d", d); return 0; } d += c; } printf("-1"); return 0; }
7
CPP
a,b = map(int,input().split()) c,d = map(int,input().split()) f = [] s = [] ans = -1 for i in range(10000): ff = b+(i*a) f.append(ff) for i in range(10000): ss = d+(i*c) if(ss in f): ans = ss break s.append(ss) print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 900010; int a, b, c, d; int main() { cin >> a >> b; cin >> c >> d; if (b == d) { return 0 * printf("%d\n", b); } else { for (int i = 0; i <= 1000; i++) for (int j = 0; j <= 1000; j++) { if (b + i * a == d + j * c) { return 0 * printf("%d\n", b + i * a); } } } return 0 * printf("-1\n"); }
7
CPP
#include <bits/stdc++.h> int main(void) { int a, b, c, d, i; scanf("%d%d%d%d", &a, &b, &c, &d); for (i = 1; i <= 10000000; i++) { if (i - b < 0 || i - d < 0) continue; if ((i - b) % a == 0 && (i - d) % c == 0) { printf("%d\n", i); return 0; } } printf("-1\n"); return 0; }
7
CPP
a,b=[int(i)for i in input().split()] c,d=[int(i)for i in input().split()] ans=-1 for i in range(int(1e6)): if b<d:b+=a elif d<b:d+=c else:ans=d;break print(ans)
7
PYTHON3
##n = int(input()) ##a = list(map(int, input().split())) ##print(' '.join(map(str, res))) [a, b] = list(map(int, input().split())) [c, d] = list(map(int, input().split())) def gcd(a, b): if b == 0: return a return gcd(b, a%b); if abs(d-b)%gcd(a, c) != 0: print('-1') exit(0) res = 1000000 for k in range(1000): for l in range(1000): if k*a+b == l*c+d: res = min(res, k*a+b) print(res)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e; scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); if (d > b) { int t = a; a = c; c = t; t = b; b = d; d = t; } int i; for (i = 0; i < 1000; i++) { int t = (a * i + b - d) % c; if (t == 0) { printf("%d\n", a * i + b); break; } } if (i == 1000) printf("-1\n"); }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); for (int i = 0; i < 1e6 + 100; i++) { int t = b + i * a; if (t - d >= 0 && (t - d) % c == 0) { printf("%d\n", t); return 0; } } printf("-1\n"); return 0; }
7
CPP
def e_gcd(a,b): s = pt = 0 ps = t = 1 r = b pr = a while r: q = pr // r pr, r = r, (pr - q*r) ps, s = s, (ps - q*s) pt, t = t, (pt - q*t) return pr, (ps, pt) a, b = [int(x) for x in input().split()] c, d = [int(x) for x in input().split()] g, x = e_gcd(a,c) num = d - b if num % g: #if the difference cannot be bridged by multiples gcd print(-1) #no solutions exist else: u, v = x #Take the Bezout id xp = num*u//g #xp*a + yp*c == num, only interested in particular solution hs = c//g #x incrmnt for homogeneous solution if not u: xp += num // g while xp > 0: xp -= hs while xp < 0: xp += hs ans = xp*a + b #intersection if ans < d: o,p = e_gcd(a,c) while ans < d: ans += (a*c)//o print(ans)
7
PYTHON3
def find_scream(): a, b = map(int, input().split()) c, d = map(int, input().split()) for i in range(0, 1000): current_scream = b + a * i if (current_scream - d ) % c == 0 and current_scream - d >= 0: print(current_scream) return print(-1) return find_scream()
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) ans = -1 for i in range(100000): if b == d: ans = b break elif b < d: b += a else: d += c print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 1; i <= 100; i++) { for (int j = 1; j <= 100; j++) { if (((i - 1) * a) + b == ((j - 1) * c) + d) { cout << ((i - 1) * a) + b; return 0; } } } cout << -1; }
7
CPP
## necessary imports import sys input = sys.stdin.readline from math import log2, log, ceil # swap_array function def swaparr(arr, a,b): temp = arr[a]; arr[a] = arr[b]; arr[b] = temp ## gcd function def gcd(a,b): if a == 0: return b return gcd(b%a, a) ## nCr function efficient using Binomial Cofficient def nCr(n, k): if(k > n - k): k = n - k res = 1 for i in range(k): res = res * (n - i) res = res / (i + 1) return res ## upper bound function code -- such that e in a[:i] e < x; def upper_bound(a, x, lo=0): hi = len(a) while lo < hi: mid = (lo+hi)//2 if a[mid] < x: lo = mid+1 else: hi = mid return lo ## prime factorization def primefs(n): ## if n == 1 ## calculating primes primes = {} while(n%2 == 0): primes[2] = primes.get(2, 0) + 1 n = n//2 for i in range(3, int(n**0.5)+2, 2): while(n%i == 0): primes[i] = primes.get(i, 0) + 1 n = n//i if n > 2: primes[n] = primes.get(n, 0) + 1 ## prime factoriazation of n is stored in dictionary ## primes and can be accesed. O(sqrt n) return primes ## MODULAR EXPONENTIATION FUNCTION def power(x, y, p): res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 x = (x * x) % p return res ## DISJOINT SET UNINON FUNCTIONS def swap(a,b): temp = a a = b b = temp return a,b # find function def find(x, link): while(x != link[x]): x = link[x] return x # the union function which makes union(x,y) # of two nodes x and y def union(x, y, size, link): x = find(x, link) y = find(y, link) if size[x] < size[y]: x,y = swap(x,y) if x != y: size[x] += size[y] link[y] = x ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES def sieve(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime #### PRIME FACTORIZATION IN O(log n) using Sieve #### MAXN = int(1e6 + 5) def spf_sieve(): spf[1] = 1; for i in range(2, MAXN): spf[i] = i; for i in range(4, MAXN, 2): spf[i] = 2; for i in range(3, ceil(MAXN ** 0.5), 2): if spf[i] == i: for j in range(i*i, MAXN, i): if spf[j] == j: spf[j] = i; ## function for storing smallest prime factors (spf) in the array ################## un-comment below 2 lines when using factorization ################# # spf = [0 for i in range(MAXN)] # spf_sieve() def factoriazation(x): ret = {}; while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1; x = x//spf[x] return ret ## this function is useful for multiple queries only, o/w use ## primefs function above. complexity O(log n) ## taking integer array input def int_array(): return list(map(int, input().strip().split())) ## taking string array input def str_array(): return input().strip().split(); #defining a couple constants MOD = int(1e9)+7; CMOD = 998244353; INF = float('inf'); NINF = -float('inf'); ################# ---------------- TEMPLATE ENDS HERE ---------------- ################# a,b = int_array(); c,d = int_array(); this_set = set(); for i in range(101): for j in range(101): if b + a*i == d + c*j: this_set.add(b+a*i); if len(this_set) == 0: print(-1) else: print(min(this_set));
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int a, b, c, d, i, j; int NOD(int x, int y) { while (x > 0 && y > 0) { if (x > y) { x %= y; } else { y %= x; } } return x + y; } int main() { scanf("%d%d%d%d", &a, &b, &c, &d); if ((b - d) % NOD(a, c) != 0) { printf("-1"); return 0; } for (i = 0; i < 1000; i++) { for (j = 0; j < 1000; j++) if (b + i * a == d + j * c) { j = b + i * a; printf("%d", j); return 0; } } return 0; }
7
CPP
#include <bits/stdc++.h> std::pair<std::pair<int, int>, int> gcd(int a, int b) { if (b == 0) { return std::make_pair(std::make_pair(1, 0), a); } else { int c = a % b; int mult = (a - c) / b; std::pair<std::pair<int, int>, int> res = gcd(b, c); return std::make_pair( std::make_pair(res.first.second, res.first.first - res.first.second * mult), res.second); } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int a, b, c, d; std::cin >> b >> a >> d >> c; if (d > b) { int temp = d; d = b; b = temp; temp = a; a = c; c = temp; } std::pair<std::pair<int, int>, int> res = gcd(b, d); int div = res.second; int bm = res.first.first; int dm = res.first.second; if ((c - a) % div) { std::cout << "-1\n"; } else { int x = bm * (c - a) / div; int y = -dm * (c - a) / div; while ((x > 0) && (y > 0)) { x -= d / div; y -= b / div; } while ((x < 0) || (y < 0)) { x += d / div; y += b / div; } std::cout << a + b * x << '\n'; } }
7
CPP
a, b = map(int, input().split()) c, d = map(int, input().split()) arr = [b + a * i for i in range(1000)] for i in range(1000): if d + c * i in arr: print(d + c * i) exit() print(-1)
7
PYTHON3
def BS(f,n,len): L = 0 R = len-1 while(L<=R): mid = (L+R)//2 if(f[mid] == n): return True elif(f[mid]<n): L =(mid+1) else: R =(mid-1) return False a,b = map(int,input().split()) c,d = map(int,input().split()) f = [] s = [] ans = -1 for i in range(10000): ff = b+(i*a) f.append(ff) size = len(f) for i in range(10000): ss = d+(i*c) if(BS(f,ss,size)): ans = ss break s.append(ss) print(ans)
7
PYTHON3
inputBuffer = input().split() a = int(inputBuffer[0]) b = int(inputBuffer[1]) inputBuffer = input().split() c = int(inputBuffer[0]) d = int(inputBuffer[1]) ans = 1E9 for i in range(1000): for j in range(1000): if (a*i+b == c*j+d): ans = min(ans,a*i+b) if (ans == 1E9): ans = -1 print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); vector<bool> can(1e7 + 1); for (int i = b; i <= 1e7; i += a) { can[i] = true; } for (int i = d; i <= 1e7; i += c) { if (can[i]) { printf("%d\n", i); return 0; } } printf("-1\n"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int rick, morty; int a, b, c, d, x, y; int main() { cin >> a >> b >> c >> d; for (x = 0, y = 0; x <= 100 && y <= 100;) { rick = b + x * a; morty = d + y * c; if (rick == morty) { cout << rick; return 0; } if (rick > morty) y++; else x++; } cout << -1; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; unsigned long long cream_time(unsigned long long in1, unsigned long long int2, unsigned long long time); int main() { unsigned long long a = 0, b = 0, c = 0, d = 0; unsigned long long catched_time = 0, cream_count = 0; unsigned long long Rick_time = 0; bool Is_catched = false; cin >> a >> b; cin >> c >> d; for (unsigned long long i = cream_count; i < c + cream_count; i++) { Rick_time = cream_time(a, b, i); if (Rick_time < d) { cream_count++; } else { if ((Rick_time - d) % c == 0) { Is_catched = true; catched_time = Rick_time; break; } } } if (true == Is_catched) { cout << catched_time << endl; } else { cout << "-1" << endl; } return 0; } unsigned long long cream_time(unsigned long long in1, unsigned long long in2, unsigned long long time) { unsigned long long result = 0; result = in2 + time * in1; return result; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long i, a, b, c, d, mn, mx; cin >> a >> b >> c >> d; mn = min(b, d); b -= mn; d -= mn; if (d > b) { for (i = 0; i < 1000; i++) { if (d % a == 0) { cout << d + mn; return 0; } d += c; } } else if (d < b) { for (i = 0; i < 1000; i++) { if (b % c == 0) { cout << b + mn; return 0; } b += a; } } else if (d == b) { cout << d + mn; return 0; } cout << "-1"; return 0; }
7
CPP
a,b=map(int,input().split()) c,d=map(int,input().split()) s={b-d} while b!=d: if b<d: b+=a else: d+=c bd=b-d if bd in s: break s|={bd} if d==b: print(b) else: print(-1)
7
PYTHON3
a, b = [int(v) for v in input().split(' ')] c, d = [int(v) for v in input().split(' ')] # Il ragionamento qui è che dobbiamo imporre # b + ai = d + cj # che è un'eq. diofantea che ha soluzione sse gcd(c, a) | (d - b) -- [Hardy & Wright, An introduction to the theory of numbers, thm.25] # siccome inoltre a, b, c e d sono abbastanza piccoli una volta appurato questo andiamo di brute force. # Non mi aspetto nessuna soluzione corretta da parte dei ragazzi # perchè è un pochino "avanzata", ma secondo me è uno di quei problemi su cui vale la pena perdere mezz'oretta. def gcd(x, y): if x == 0: return y return gcd(y%x, x) if (d-b)%gcd(c, a) != 0: print(-1) else: finished = False i = 0 while not finished: j = (b+a*i-d)//c if b + a*i == d + c*j and j >= 0: print(b + a*i) finished = True i += 1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int dp[199]; int main() { long long x, y, x1, y1; int untd = -1; cin >> x >> y; cin >> x1 >> y1; for (int k = 0; k <= 100; k++) for (int j = 0; j <= 100; j++) if (y + x * k == y1 + x1 * j) { cout << y + x * k; return 0; } cout << untd; }
7
CPP
a, b = map(int,input().split()) c, d = map(int,input().split()) cnt = 0 found = False j = 0 while cnt <= a: i = (d-b + c *j)//a if i >= 0: cnt += 1 if (d-b+c*j)%a == 0: found = True # print (i,j) break j += 1 if found: print (b+a*i) else: print (-1)
7
PYTHON3
R = lambda:map(int , input().split()) a , b = R() c , d = R() n = 100000 f = [0] * n i = b while(i < 10000 ) : f[i] |= 1 i += a i = d while(i < 10000 ) : f[i] |= 2 i += c found = False i = 0 while(i < 10000) : if f[i] == 3 : print(i) found = True break i += 1 if(not found): print(-1)
7
PYTHON3
a, b = map(int,input().split()) #b + a, b + 2a c, d = map(int,input().split()) #c + d, c + 2d e = [b] f = [d] g = 1 h = 0 for i in range(0, 5000): x = b + g * a e.append(x) y = d + g * c f.append(y) g += 1 u = set(e) i = set(f) y = u&i if y: print(min(y)) else: print("-1")
7
PYTHON3