solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
# https://codeforces.com/problemset/problem/787/A a,b = map(int, input().split()) c,d = map(int, input().split()) i = 0 j = 0 def GCD(a,b): if b==0: return a return GCD(b,a%b) if (d-b) % GCD(a,c): print(-1) exit() while b+a*i!=d+c*j: if b + a*i > d + c*j: j += 1 else: i += 1 print(b+a*i)
7
PYTHON3
def de(x,y): if y%2 != 0 and x%2 == 0: k = "odd" elif y%2 == 0 and x%2 == 0: k = "even" else: k = "undef" return k def uc(m,n): for i in range(min(m,n) + 1, 0, -1): if m%i == 0 and n%i == 0: break return i a, b = map(int,input().split(" ")) c, d = map(int,input().split(" ")) f = 0 if de(a,b) != "undef" and de(c,d) != "undef" and de(a,b) != de(c,d) or abs(d - b)%uc(a,c) != 0: print(-1) else: while True: if a >= c: if ((b + f*a) - d)%c == 0 and b + f*a - d >= 0: print(b + f*a) break else: f += 1 else: if ((d + f*c) - b)%a == 0 and d + f*c - b >= 0: print(d + f*c) break else: f += 1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const double EPSILON = 1e-9; const double PI = acos(-1); const int MOD = 1e9 + 7; const int INF = 0x3c3c3c3c; const long long INFL = 0x3c3c3c3c3c3c3c3c; const int MAX_N = 102; int a, b, c, d; int visited[9999]; int main() { scanf("%d%d%d%d", &a, &b, &c, &d); for (int i = b; i < 9999; i += a) { visited[i] = 1; } for (int i = d; i < 9999; i += c) { if (visited[i]) { printf("%d", i); return 0; } } printf("-1"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-6; const double Pi = acos(-1); bool cmp(const pair<int, int> &fi, const pair<int, int> &se) { return fi.second < se.second; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } int dr[] = {1, 1, 0, -1, -1, -1, 0, 1}; int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; int main() { int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) { int B = b + (a * i); int D = d + (c * j); if (B == D) { cout << B << endl; return 0; } } } cout << -1 << endl; return 0; }
7
CPP
def gcd(a,b): if b==0: return a else: return gcd(b,a%b) def main(): a,b=input().split() a=int(a) b=int(b) c,d=input().split() c=int(c) d=int(d) if abs(d-b) % gcd(max(a,c),min(a,c)): print(-1) else: i=0 while True: ans=b+i*a if ans>=d and not ((ans -d) % c): print(ans) break i+=1 if __name__=='__main__': main()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long bigmod(long long b, long long p) { if (p == 0) return 1; long long my = bigmod(b, p / 2); my *= my; my %= 1000000007; if (p & 1) my *= b, my %= 1000000007; return my; } int setb(int n, int pos) { return n = n | (1 << pos); } int resb(int n, int pos) { return n = n & ~(1 << pos); } bool checkb(int n, int pos) { return (bool)(n & (1 << pos)); } int a, b, c, d; int main() { int i; scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); int ret = -1; int mn = max(b, d); for (i = mn; i <= 100000000; i++) { int ok = 0; int val1 = i - b; if (val1 % a == 0) ok++; int val2 = i - d; if (val2 % c == 0) ok++; if (ok == 2) { ret = i; break; } } printf("%d", ret); printf("\n"); return 0; }
7
CPP
R = lambda:map(int, input().split()) a, b = R() c, d = R() s = set(range(b, 100**2, a)) & set(range(d, 100**2, c)) print(min(s) if s else -1)
7
PYTHON3
a,b=list(map(int,input().split())) c,d=list(map(int,input().split())) h=0 for k in range(10000): t=b+k*a for i in range(10000): if t== d+i*c: print(d+i*c) h+=1 break if h>0: break if h==0: print('-1')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename P, unsigned long long int mod> class Mod { public: Mod() : v_() {} public: template <typename V> P& operator=(const V&& rv) { static_assert(std::is_integral<V>::value, "should be integral type"); v_ = rv; return v_; } template <typename V> P& operator+=(const V&& rv) { static_assert(std::is_integral<V>::value, "should be integral type"); v_ += rv; return v_; } private: P v_; }; const int sz = 1e5; int main() { ios::sync_with_stdio(false); int a, b, c, d; cin >> a >> b >> c >> d; set<int> num; for (int i = 0; i < sz; ++i) { num.insert(b + i * a); } for (int i = 0; i < sz; ++i) { if (num.find(d + i * c) != num.end()) { cout << d + i * c << endl; return 0; } } cout << -1 << endl; }
7
CPP
#include <bits/stdc++.h> using namespace std; int a, b, c, d; map<int, int> m; void nhap() { cin >> a >> b >> c >> d; } void solve() { m[b] = 1; for (int i = 1; i <= 100000; i++) b += a, m[b] = 1; for (int i = 1; i <= 100000; i++) if (m[d]) { cout << d; return; } else d += c; cout << -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); nhap(); solve(); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int a, b, c, d; cin >> a >> b >> c >> d; if (d < b) { swap(b, d); swap(a, c); } for (int m = 0; m <= 1000000; ++m) { if ((m * a + b - d) % c == 0) { int n = (m * a + b - d) / c; if (n >= 0) { cout << (m * a + b) << endl; return 0; } } } cout << -1 << endl; return 0; }
7
CPP
a,b = map(int, input().split()) c,d = map(int, input().split()) MAX = 10**5 for pos in range(d, MAX, c): if pos in range(b, MAX, a): print(pos) break else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d; cin >> a >> b >> c >> d; if (a % 2 == 0 && b % 2 != 0 && c % 2 == 0 && d % 2 == 0) { cout << "-1" << endl; } else if (a % 2 == 0 && b % 2 == 0 && c % 2 == 0 && d % 2 != 0) { cout << "-1" << endl; } else { while (b != d && b < 20000) { if (b > d) { d = d + c; } else if (b < d) { b = b + a; } } if (b > 10000) { cout << "-1" << endl; } else { cout << b << endl; } } return (0); }
7
CPP
from math import * a,b=map(int,input().split()) c,d=list(map(int,input().split())) x=b-d if x%gcd(a,c)!=0: print(-1) else: y=-1 for i in range(101): for j in range(101): if a*i-c*j==d-b: y=i break if y>=0: break if y<0: print(-1) else: print(a*i+b)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { unsigned long long int a, b, c, d; cin >> a >> b >> c >> d; unordered_map<unsigned long long int, unsigned long long int> mp, mp1; for (int i = 0; i <= 100000; i++) { ++mp[b + (i * a)]; ++mp1[d + (i * c)]; } for (int i = 0; i <= 100000; i++) { if (mp[i] && mp1[i]) { cout << i; return 0; } } cout << -1; } }
7
CPP
a,b = map(int,input().split()) c,d = map(int,input().split()) for i in range(101): for j in range(101): if b+i*a == d+c*j: print(d+c*j) exit() print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) x = [b] y = [d] for i in range(1000): if x[len(x) - 1] in y: print(x[len(x) - 1]) break elif y[len(y) - 1] in x: print(y[len(y) - 1]) break x.append(x[len(x) - 1] + a) y.append(y[len(y) - 1] + c) else: print(-1)
7
PYTHON3
a,b=map(int,input().split()) c,d=map(int,input().split()) for i in range(100101): x=b+a*i if x>=d and (x-d)%c==0: print(x) exit(0) print("-1")
7
PYTHON3
a = list(map(int, input().split())) b = list(map(int, input().split())) list1 = [] list2 = [] counter1 = 0 counter2 = 0 for i in range(100): list1.append(a[1] + i * a[0]) list2.append(b[1] + i * b[0]) while True: if list1[counter1] == list2[counter2]: print(list1[counter1]) break elif list1[counter1] < list2[counter2]: counter1 += 1 else: counter2 += 1 if counter1 == 100 or counter2 == 100: print(-1) break
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int INF = 2147483647; int inf = -2147483648; const double PI = acos(-1.0); const double pi = 2 * asin(1.0); int vis[1000009]; int vi[1000009]; int main() { ios::sync_with_stdio(false); int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 0; i <= 100; i++) { vis[b + a * i] = 1; } for (int i = 0; i <= 100; i++) { vi[d + c * i] = 1; } for (int i = 0; i < 1000009; i++) { if (vis[i] == 1 && vi[i] == 1) { cout << i << endl; return 0; } } cout << -1 << endl; return 0; }
7
CPP
a,b=[*map(int,input().split())] c,d=[*map(int,input().split())] e=[] count=0 for i in range(0,100): e.append(b+a*i) for j in range(0,100): ans=d+c*j if ans in e: count+=1 ans1=ans break if count==1: print(ans1) else: print(-1)
7
PYTHON3
# http://codeforces.com/problemset/problem/787/A a, b = map(int, input().split()) c, d = map(int, input().split()) n = 0 m = 0 while (n < 10000) and (m < 10000): abn = b + a*n cdm = d + c*m if (abn < cdm): n += 1 elif (abn > cdm): m += 1 else: break if (abn == cdm): print(abn) else: print("-1")
7
PYTHON3
import sys import math import bisect def solve(a, b, c, d): A = [] B = [] for i in range(200): A.append(b + a * i) for i in range(200): B.append(d + c * i) s = set(A) & set(B) if len(s) == 0: return -1 return min(s) def main(): a, b = map(int, input().split()) c, d = map(int, input().split()) ans = solve(a, b, c, d) print(ans) if __name__ == "__main__": main()
7
PYTHON3
b, a = map(int, input().split()) d, c = map(int, input().split()) ans = -1 for i in range(100100): if i >= a and i >= c and i % b == a % b and i % d == c % d and ans == -1: ans = i print(ans)
7
PYTHON3
a,b=[int(x) for x in input().split()] c,d=[int(x) for x in input().split()] for i in range(100): if (a*i+b-d)%c==0 and (a*i+b-d)//c>=0: print(a*i+b) break else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, mini = 1e12; cin >> a >> b >> c >> d; for (int i = 0; i <= 1000; i++) { for (int j = 0; j < 1001; j++) { if (i * a - j * c == d - b && mini > b + i * a) { mini = b + i * a; } } } if (mini == 1e12) cout << -1 << endl; else cout << mini; return 0; }
7
CPP
a,b = map(int,input().split()) c,d = map(int,input().split()) res = -1 for i in range (100): for j in range(100): if (b+(a*i)) == (d+(c*j)): res = (b+a*i) break else: continue break print(res)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long a, b, c, d; cin >> a >> b >> c >> d; unordered_set<long long> myset; while (1) { long long df = b - d; if (df == 0) { cout << b; return; } if (myset.find(df) != myset.end()) break; myset.insert(df); if (df < 0) { b = b + a; } else { d = d + c; } } cout << -1; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
7
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1e5 + 5; int main() { int a, b, c, d, ans; cin >> a >> b >> c >> d; if ((b > d && a > c) || (b < d && a < c)) ans = -1; for (int i = max(b, d); i < 1e5; i++) { if ((i - b) % a == 0 && (i - d) % c == 0) { ans = i; break; } } cout << ans << endl; return 0; }
7
CPP
import math a, b = list(map(int, input().split())) c, d = list(map(int, input().split())) k = math.gcd(a, c) if abs(b - d) % k == 0: for i in range(101): for j in range(101): if b + a*i == d + c*j: print(b+a*i) exit() else: print(-1)
7
PYTHON3
a,b=map(int,input().split()) c,d=map(int,input().split()) for nr in range(1,1001): for nm in range(1,1001): if b+(nr-1)* a == d+(nm-1)*c: print(b+(nr-1)* a) exit() print(-1)
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) for i in range(0,101): time = b + i*a for j in range(0,101): time2 = d + j*c if time == time2: break if time == time2: break if time == time2: print(time) else: print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) i = 0 j = 0 high = max(a, b, c, d) ans = -1 while i <= high and j <= high: if i*a + b == j*c + d: ans = i*a + b break elif (i*a + b) > (j*c + d): j += 1 else: i += 1 print(ans)
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) f = 0 i = 0 for i in range(101): for j in range(101): if(b+i*a == d+j*c): # print(d+j*c) f = 1 break if(f==1): break if(f): print(b+a*i) else: print(-1)
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) for i in range(1000): t = b+i*a if t< d: continue if(t-d)%c == 0: print(t) exit(0) print(-1)
7
PYTHON3
a, b = input().split() a = int(a) b = int(b) c, d = input().split() c = int(c) d = int(d) i = 0 j = 0 ric = [] morti = [] while i <= 100: ric.append(a * i + b) i += 1 while j <= 100: morti.append(c * j + d) j += 1 ric = set(ric) morti = set(morti) answ = list(ric.intersection(morti)) answ.sort() if len(answ) == 0: print(-1) else: print(answ[0])
7
PYTHON3
a, b = [int(i) for i in input().split()] c, d = [int(i) for i in input().split()] z = max(b,d) while True: if z % a == b % a and z % c == d % c: print(z) break elif z>100*100: print(-1) break z += 1
7
PYTHON3
def gcd(a, b): while b != 0: r = a % b a = b b = r return a a, b = map(int, input().split()) c, d = map(int, input().split()) if (d - b) % gcd(a, c) != 0: print(-1) exit() y = 0 while True: x = (d - b + c * y) // a if x >= 0 and (d - b + c * y) % a == 0: print(d + c * y) break y += 1
7
PYTHON3
a, b = input().split() c, d = input().split() a, b, c, d = int(a), int(b), int(c), int(d) if d > b: a, b, c, d = c, d, a, b d = d % c for i in range(c): res = b + a*i if res % c == d: print(res) break else: print("-1")
7
PYTHON3
a=list(map(int,input().strip().split(" "))) b=list(map(int,input().strip().split(" "))) a.append(a[1]) b.append(b[1]) for i in range(20000): if a[2]!=b[2]: if a[2]<b[2]: a[2]+=a[0] else: b[2]+=b[0] else: print(a[2]) break else: print("-1")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = 1e9 + 7, N = 1e3 + 10; void test() { ll a, b; cin >> a >> b; ll c, d; cin >> c >> d; ll mn = 1e18; for (ll i = 0; i < N; ++i) { for (ll j = 0; j < N; ++j) { if (b + a * i == d + c * j) { mn = min(mn, b + a * i); } } } mn = (mn == (ll)1e18 ? -1 : mn); cout << mn << '\n'; return; } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); ll tt = 1; for (ll t = 1; t <= tt; ++t) { test(); } return 0; }
7
CPP
def greatestCommonDivisor(a, b): if (a == 0 or b == 0): return a + b while (a != b): if (a > b): a -= b else: b -= a return a def firstScreamTime(a, b, c, d): bias = d - b gcd = greatestCommonDivisor(a, c) if (bias % gcd != 0): return -1 y = 0 while(True): x = (bias + c * y) / a if (x == x // 1 and x >= 0): return y * c + d y += 1 a, b = map(int, input().split()) c, d = map(int, input().split()) print(firstScreamTime(a, b, c, d))
7
PYTHON3
# http://codeforces.com/contest/787/problem/A def is_even(num): return num % 2 == 0 def is_odd(num): return num % 2 == 1 def is_valid_scream(a, b, c, d, rick_scream_count): numerator = d + rick_scream_count * c - b if numerator < 0: return False return numerator % a == 0 def find_overlapping_screams(a, b, c, d): rick_is_always_odd = is_odd(b) and is_even(a) morty_is_always_odd = is_odd(d) and is_even(c) rick_is_always_even = is_even(b) and is_even(a) morty_is_always_even = is_even(d) and is_even(c) if (rick_is_always_even and morty_is_always_odd) or (rick_is_always_odd and morty_is_always_even): return -1 rick_scream_count = 0 count = 0 max_enum = (100**3) while True: if is_valid_scream(a, b, c, d, rick_scream_count): break rick_scream_count += 1 count += 1 if count >= max_enum: return -1 return d + rick_scream_count * c a, b = list(map(int, input().strip().split(' '))) c, d = list(map(int, input().strip().split(' '))) overlapping_scream_time = find_overlapping_screams(a, b, c, d) print(overlapping_scream_time)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d; cin >> a >> b >> c >> d; map<long long int, long long int> mp; for (long long int i = 0; i < 101; i++) mp[b + i * a]++; for (long long int j = 0; j < 101; j++) { if (mp.find(d + j * c) != mp.end()) { cout << d + j * c; return 0; } } cout << -1; }
7
CPP
#include <bits/stdc++.h> using namespace std; int a, b, c, d; int occ[100010]; int main() { scanf("%d %d %d %d", &a, &b, &c, &d); while (b < d) b += a; while (d < b) d += c; for (int i = b, cnt = 1; cnt <= c; cnt++, i += a) { occ[i] = 1; } for (int i = d, cnt = 1; cnt <= a; cnt++, i += c) if (occ[i]) { printf("%d\n", i); return 0; } puts("-1"); return 0; }
7
CPP
# 787A def gcd(a, b): if b == 0: return a return gcd(b, a%b) from sys import stdin a, b = map(int, stdin.readline().split()) c, d = map(int, stdin.readline().split()) # gen term # b + (n-1)*a = d + (m-1)*c # b - d + c - a = mc - na # a = 39 # b = 52 # c = 88 # d = 78 if a == 0: if c!=0: if b<d: print(-1) elif (b-d)%c==0: print() else: print(-1) else: if b!=d: print(-1) else: print(b) if (b%2==1 and a%2==0 and d%2 == 0 and c%2 == 0) or (d%2==1 and c%2==0 and b%2 == 0 and a%2 == 0): print(-1) else: const = b - d + c - a # m*c + n*(-a) = const if const%gcd(a, c) == 0: for m in range(1, 10**7): if m*c>=const: if (m*c - const)/a == (m*c - const)//a: print(b + ((m*c - const)//a - 1)*a) break else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n, m, x, y, z; int a, b, c, d; int main() { cin >> a >> b >> c >> d; int t = max(b, d); for (register int i = t; i <= 1000000; ++i) { if ((i - b) % a == 0 && (i - d) % c == 0) { cout << i << endl; return 0; } } puts("-1"); return 0; }
7
CPP
a,b=(map(int,input().split())) c,d=(map(int,input().split())) for i in range(b,10**4,a): if i in range(d,10**4,c): exit(print(i)) print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using lli = long long; const lli OO = 1e18; string ys = "YES", no = "NO"; void fast() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } lli di[8] = {-1, 1, 0, 0, -1, 1, -1, 1}; lli dj[8] = {0, 0, -1, 1, 1, 1, -1, -1}; vector<vector<lli>> adj; lli frq[1000007 + 7]; bool isPrime[1000007]; vector<int> primes; void sieve() { memset(isPrime, true, sizeof isPrime); isPrime[0] = isPrime[1] = false; for (int i = 4; i < 1000007; i += 2) isPrime[i] = false; for (lli i = 3; i * i < 1000007; i++) if (isPrime[i]) for (lli j = i * i; j < 1000007; j += i + i) isPrime[j] = false; primes.push_back(2); for (int i = 3; i < 1000007; i += 2) if (isPrime[i]) primes.push_back(i); } lli gcd(lli a, lli b) { if (b == 0) return a; else return gcd(b, a % b); } lli lcm(lli n, lli m) { return n * m / gcd(n, m); } int main() { fast(); lli q; sieve(); q = 1; while (q--) { lli a, b, c, d, dd; cin >> a >> b >> c >> d; for (int i = 0; i <= 100; i++) { dd = d; for (int j = 0; j <= 100; j++) { if (dd == b) return cout << b << "\n", 0; dd += c; } b += a; } cout << -1 << "\n", 0; } }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j; int a, b, c, d; cin >> a >> b >> c >> d; for (i = 0; i < 10000; i++) { for (j = 0; j < 10000; j++) { if (b + a * i == d + c * j) { cout << b + a * i << endl; return 0; } } } cout << "-1" << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } int main() { int a, b, c, d; scanf("%d", &a); scanf("%d", &b); scanf("%d", &c); scanf("%d", &d); int k = gcd(a, c); if (abs(b - d) % k != 0) { cout << "-1"; return 0; } int y1 = min(b, d), y2 = max(b, d), flag = 0, flag1 = 0; if (y1 == b) flag1 = 1; if (y1 == y2) flag = 1; while (flag == 0) { if (flag1 == 1) y1 += a; else y1 += c; while (y2 < y1) { if (flag1 == 1) y2 += c; else y2 += a; } if (y1 == y2) flag = 1; } cout << y1; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int len = 1000; int arr[len] = {0}; int m = 0; bool is_appear(int x) { for (int i = 0; i < m - 1; ++i) { if (x == arr[i]) return true; } return false; } int main() { int a, b, c, d, h, ok = 1, j; cin >> a >> b >> c >> d; for (j = 0;; ++j) { if (a * j + b - d >= 0) break; } for (int i = j;; ++i) { h = (a * i + b - d) % c; if (h == 0) { cout << a * i + b << endl; break; } else { arr[m++] = h; if (is_appear(h)) { ok = 0; break; } } } if (ok == 0) cout << "-1" << endl; return 0; }
7
CPP
import sys a,b=map(int,input().split()) c,d=map(int,input().split()) for i in range(101) : sum1=b+(a*i) sum2=(sum1-d)%c if sum2==0 and sum1-d>=0 : print(sum1) sys.exit() print(-1)
7
PYTHON3
#include <bits/stdc++.h> using std::cin; using std::cout; using std::max; int a, b, c, d; int main() { cin >> a >> b >> c >> d; int n = max(a, max(b, max(c, d))); for (int i = 0; i <= n; i++) if ((a * i + b - d) >= 0 && (a * i + b - d) % c == 0) { cout << a * i + b; return 0; } cout << -1; return 0; }
7
CPP
def gcd(x,y): while(y): x,y = y, x%y return x a,b = map(int, input().split()) c,d = map(int, input().split()) y = d-b z = gcd(a,c) x= abs(y)%z if x==0: p,q = 0,0 while (p*a - q*c) != y: if (p*a - q*c) > y: q +=1 else: p+=1 print((p*a)+b) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int a, b, c, d, i, j, n, flag = 0; cin >> a >> b >> c >> d; n = max(max(a, b), max(c, d)); flag = 1; for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { if (b + i * a == d + j * c) { cout << b + i * a << endl; flag = 0; break; } } if (!flag) break; } if (flag) cout << -1 << endl; return 0; }
7
CPP
#include <bits/stdc++.h> int p[100000]; int main() { int a, b, c, d, i; scanf("%d %d %d %d", &b, &a, &d, &c); for (i = a; i < 100000; i += b) p[i] = 1; for (i = c; i < 100000; i += d) { if (p[i]) { printf("%d", i); return 0; } } printf("-1"); }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; while (t--) { int a, b, c, d; cin >> a >> b >> c >> d; while (b != d && b < 1e5) { b < d ? b += a : d += c; } if (b == d) { cout << b; } else { cout << "-1"; } cout << endl; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int a, b, c, d; int f(int x) { return a * x + b - d; } int fa(int x) { return a * x + b; } int main() { cin >> a >> b >> c >> d; for (int i = 0; i < 100000; ++i) { if (f(i) >= 0 && f(i) % c == 0) { cout << fa(i) << endl; return 0; } } cout << -1 << endl; return 0; }
7
CPP
a, b = map(int, input().strip().split()) c, d = map(int, input().strip().split()) flag = 0 for i in range(0, 100): for j in range(0, 100): if b + a * i == d + c * j: flag = 1 ans = i break elif b + a * i < d + c * j: break if bool(flag): break if bool(flag): print(ans * a + b) else: print(-1)
7
PYTHON3
a,b = [int(i) for i in input().split(' ')] c,d = [int(i) for i in input().split(' ')] d1 = d%c ans = -1 b-=a for i in range(c+120): b+=a if b%c == d1 and b>=d: ans = b break print(ans)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) mark = 0 for i in range (0, 1000): for j in range (0, 1000): if (b + i * a == d + j * c): print(b + i * a) mark += 1 break if (mark != 0): break if (mark == 0): print('-1')
7
PYTHON3
a, b = (int(x) for x in input().split()) c, d = (int(x) for x in input().split()) x = b mod = [] cont = True while cont: y = x % c if x < d: x += a continue if y in mod: cont = False print(-1) elif (y-d) % c == 0: cont = False print(x) else: mod.append(y) x += a
7
PYTHON3
a, b = map(int, 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; int const c = 1e6; int freq[c]; int main() { int a, b, c, d; cin >> a >> b; cin >> c >> d; for (int i = 0; i <= 100; ++i) { ++freq[(a * i) + b]; ++freq[d + (c * i)]; } for (int i = 0; i < 20000; ++i) if (freq[i] > 1) { cout << i; return 0; } cout << -1; }
7
CPP
import math a,b = map(int,input().split()) c,d = map(int,input().split()) i = -1 x = 0 flag = 0 while i<=100 : i += 1 x = max(math.ceil(i*(c/a) + ((d-b)/a)),0) if b+x*a == d + i*c : flag = 1 break if flag == 1: print(b+x*a) else: print(-1)
7
PYTHON3
####################### # python 46ms C++ 15ms# ####################### import sys from fractions import gcd #R=lambda:list(map(int,input.split())) #a,b=R() #c,d=R() #a,b=[int(i) for i in input().split()] #c,d=[int(i) for i in input().split()] a,b=input().split(); c,d=input().split(); a=int(a); b=int(b); c=int(c); d=int(d); if abs(b-d)%gcd(a,c) !=0 : print(-1); exit(0); else: while b!=d : if d>b: b=b+a; else: d=d+c; print(b); exit(0);
7
PYTHON3
#! /bin/python3 a, b = [int(x) for x in input().strip().split(' ')] c, d = [int(x) for x in input().strip().split(' ')] # bf R = set([]) M = set([]) for i in range(10000): R.add(b + a * i) M.add(d + c * i) t = R & M if len(t) > 0: print(min(t)) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long b, c, d, e; bool a[1000000]; int main() { ios_base::sync_with_stdio(false); cin >> b >> c >> d >> e; for (int i = 0; c + (i * b) < 1000000; i++) a[c + (b * i)] = true; for (int i = 0; e + (i * d) < 1000000; i++) if (a[e + (d * i)]) { cout << e + (d * i); return 0; } cout << -1; return 0; }
7
CPP
a,b = map(int,input().split()) c,d = map(int,input().split()) ptr1 = b; #ptr2 = d; arr1 = [0]*c; while( ptr1 < d ): ptr1 += a; flag=0; if ptr1%c == d%c: flag=1; while( max(arr1) != 2 and flag==0): ptr1 += a; if ptr1%c == d%c: flag=1; break; arr1[ ptr1%c ] +=1; if flag==0: print(-1); else: print(ptr1);
7
PYTHON3
k = [int(x) for x in input().strip().split()] a = k[0] b = k[1] m = [int(x) for x in input().strip().split()] c = m[0] d = m[1] out = -1 while d < 10**6 or b < 10**6: if b>d: d+=c elif d>b: b+=a else: out = b break print(out)
7
PYTHON3
# http://codeforces.com/problemset/problem/787/A a, b = [int(x) for x in input().split()] c, d = [int(x) for x in input().split()] vmax = max(a,b,c,d) for i in range(vmax+1): temp = a*i + b # print(temp) if ( ((temp-d)%c == float(0)) and (temp-d >= 0) ): print(temp) break else: print(-1)
7
PYTHON3
def gcd(a, b): while(b > 0): r = a % b a = b b = r return a a, b = map(int, input().split()) c, d = map(int, input().split()) if((d - b) % gcd(a, c) != 0): print(-1) else: i = 0 while(True): j = (b + i * a - d) // c if (j >= 0 and (b + i * a - d) % c == 0): print(b + i*a) break i += 1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; bool debug = true; bool debug1 = true; int f[1000000 + 1]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int a, b, c, d; cin >> a >> b >> c >> d; int idx = b; while (idx < 1000000 + 1) f[idx]++, idx += a; idx = d; while (idx < 1000000 + 1) f[idx]++, idx += c; for (int i = 1; i < 1000000 + 1; i++) { if (f[i] > 1) { cout << i << endl; return 0; } } cout << "-1" << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long solve(long long a, long long b, long long c, long long d) { if ((d - b) % gcd(a, c) != 0) return -1; for (long long y = 0; y <= 100; y++) { int x = (d - b + c * y) / a; if (x >= 0 and b + a * x == d + c * y) return b + a * x; } } int main() { int a, b, c, d; while (scanf("%d %d %d %d", &a, &b, &c, &d) == 4) { printf("%lld\n", solve(a, b, c, d)); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, k = 1111; set<int> s1, s2; cin >> a >> b >> c >> d; b -= a, d -= c; while (k--) { b += a; d += c; s1.insert(b); s2.insert(d); if (s1.count(d)) return cout << d, 0; if (s2.count(b)) return cout << b, 0; } return cout << -1, 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int a, b, c, d; cin >> a >> b >> c >> d; for (long long int i = 0; i < 100; i++) { for (long long int j = 0; j < 100; j++) { if (b + (i * a) == d + (j * c)) { cout << b + (i * a); return 0; } } } cout << "-1"; }
7
CPP
a, b = map(int, input().split(' ')) c, d = map(int, input().split(' ')) x = [b + i * a for i in range(100)] y = [d + i * c for i in range(100)] res = -1 for t in x: if t in y: res = t break print(res)
7
PYTHON3
import math a, b = map(int, input().split()) c, d = map(int, input().split()) g = math.gcd(a, c) if (d-b)%g != 0: print(-1) else: t0, t1=b, d while t0 != t1: if t0 < t1: t0 += a else: t1 += c print(t0)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) flag = 0 for i in range(1, 10**4): t1 = (i-b)/a t2 = (i-d)/c if int(t1) == t1 and int(t2) == t2 and t1 >= 0 and t2 >=0: print(i) # print(t2) # print(t1) flag = 1 break if flag == 0: print(-1)
7
PYTHON3
a,b=input().split() c,d=input().split() a,b,c,d=int(a),int(b),int(c),int(d) arr=[] flag=0 for i in range(0,1000): arr.append(b) b=b+a for i in range(0,1000): if arr.__contains__(d): flag=1 break d=d+c if flag==1: print(d) else: print(-1)
7
PYTHON3
a,b=map(int,input().split()) c,d=map(int,input().split()) z=-1 for i in range(1000): for j in range(1000): if b+a*i==d+c*j: z=b+a*i break if z>-1: break print(z)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, l, k; scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { if ((b + a * i) == (d + c * j)) { l = b + a * i; return cout << l, 0; } } } cout << "-1"; return 0; }
7
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:33554432") using namespace std; int a, b, c, d; long long gcd(long long aa, long long bb) { if (bb == 0) return aa; return gcd(bb, aa % bb); } long long lca(long long aa, long long bb) { return aa * bb / gcd(aa, bb); } int main() { cout.setf(ios::fixed, ios::floatfield); cout.precision(7); cin >> a >> b >> c >> d; int dd = max(b, d) - min(b, d); if ((a % c == 0 || c % a == 0) && dd % min(a, c) != 0) { cout << -1 << endl; return 0; } long long ans1 = b, ans2 = d; int cnrt = 0; while (ans1 != ans2 && cnrt < 100000) { if (ans1 < ans2) { ans1 += ((ans2 - ans1) / a) * a; if (ans1 < ans2) ans1 += a; } while (ans2 < ans1) { ans2 += ((ans1 - ans2) / c) * c; if (ans2 < ans1) ans2 += c; } cnrt++; } if (cnrt >= 100000) cout << -1 << endl; else cout << ans1 << endl; return 0; }
7
CPP
b, a = map(int, input().split(" ")) d, c = map(int, input().split(" ")) found = False for i in range(0, 100): if a + i*b - c >= 0 and (a + i*b - c) % d == 0: print(a+i*b) found = True break if not found: print ("-1")
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) def GCD(a,b): tmp=0 while(b != 0): tmp = a%b a=b b=tmp return a X=GCD(a,c) if((d-b)% X !=0): print(-1) else: y=0 while True: x=(d+c*y-b)//a if(x>=0 and (d+c*y-b)%a==0): print(b+a*x) break y+=1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int a1[1000], b1[1000]; bool ok = false; int main() { int a, b, c, d, k; scanf("%d%d%d%d", &a, &b, &c, &d); for (int i = 0; i < 1000; i++) { a1[i] = b + a * i; b1[i] = d + c * i; } for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000; j++) { if (a1[i] == b1[j]) { ok = true; k = i; break; } } if (ok) break; } if (ok) printf("%d\n", a1[k]); else puts("-1"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int a, b, c, d; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { scanf("%d%d%d%d", &a, &b, &c, &d); if (b < d) { swap(b, d); swap(a, c); } int xx = gcd(a, c); if ((b - d) % xx != 0) { printf("-1\n"); } else for (int i = 0;; i++) { if ((a * i + (b - d)) % c == 0) { printf("%d\n", b + a * i); break; } } }
7
CPP
import sys from math import gcd def solve(): a, b = map(int, input().split()) c, d = map(int, input().split()) if b > d: t1, t2 = a, b a, b = c, d c, d = t1, t2 g = gcd(a, c) for k in range(a // g): t = d - b + k * c if t % a == 0: ans = d + k * c print(ans) return print(-1) def debug(x, table): for name, val in table.items(): if x is val: print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr) return None if __name__ == '__main__': solve()
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) for i in range(b,100000,a): if i in range(d,100000,c): exit(print(i)) print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) v = set(range(b, 100 ** 2, a)) & set(range(d, 100 ** 2, c)) print(min(v) if v else -1) # Made By Mostafa_Khaled
7
PYTHON3
#Author: M@sud_P@rvez from math import * a,b=map(int,input().split()) c,d=map(int,input().split()) x=[]; y=[] for i in range(0,101): x.append(b+a*i) y.append(d+c*i) for i in range(0,101): for j in range(0,101): if x[i]-y[j]==0: print(x[i]) exit() print(-1)
7
PYTHON3
import sys import math def main(): a, b = map(int, input().split()) c, d = map(int, input().split()) arr = list() dr = 0 for i in range(0, 202): arr.append(b + dr*a) arr.append(d + dr*c) dr += 1 arr = sorted(arr) for i in range(0, len(arr)-1): if (arr[i] == arr[i+1]): return print(arr[i]) print(-1) main()
7
PYTHON3
import sys import math import itertools import collections 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=1): 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 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): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def wr(arr): return ' '.join(map(str, arr)) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) a, b = mi() c, d = mi() rick, morty = {b}, {d} while b < 10001: b += a rick.add(b) while d < 10001: d += c morty.add(d) t = rick & morty if len(t) == 0: print(-1) else: print(min(list(t)))
7
PYTHON3
import sys a, b = (int(x) for x in input().split()) c, d = (int(x) for x in input().split()) for i in range(b, 2 * a * b * c * d + 1, a): if i - d >= 0 and (i - d) % c == 0: print(i) sys.exit(0) print(-1)
7
PYTHON3
from fractions import gcd a,b = map(int,input().split()) c,d = map(int,input().split()) #ax+b=cy+d val = gcd(a,c) if (d-b)%val != 0: print(-1) else: i=b while True: if ((i-d)%c==0) and (i-d)/c>=0: break i=i+a print(i)
7
PYTHON3
# b + a * i = d + c * j ''' a * i - c * j = d - b b + a * (i - c) = d + c * j - a * c = d + c * (j - a) Giả sử (i, j)=> (i - c, j - a), (i - 2*c, j - 2*a), ... => (i - k * c, j - k * a) 1 <= a, b, c, d <= N Đk: i, j > N, không tồn tại nghiệm i, j <= N 0 <= i - k * c <= N (i - N) / c <= k = i/c 0 <= k (j - N) / a <= k = j/a Tồn tại k để i - k*c và j - k*a <= N ''' def GCD(a, b): while b != 0: r = a % b a = b b = r return a a, b = map(int,input().split()) c, d = map(int, input().split()) # if (d - b) % GCD(a, b): # # đi tìm nghiệm # i = 0 # while True: # j = (b + a * i - d) // c # if 0 <= j and b + a * i == d + c * j: # break # else: # i+=1 # print(b + a * i) # else: # print(-1) def solve(a, b, c, d): for i in range(0, 101): j = (b + a * i - d) // c if 0 <= j and b + a * i == d + c * j: return b + a * i return -1 print(solve(a,b,c,d))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> inline T BMOD(T p, T e, T m) { T ret = 1; while (e) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; e >>= 1; } return (T)ret; } template <class T> inline T MODINV(T a, T m) { return BMOD(a, m - 2, m); } template <class T> inline T isPrime(T a) { for (T i = 2; i <= sqrt(double(a)); i++) { if (a % i == 0) { return 0; } } return 1; } template <class T> inline T lcm(T a, T b) { return (a / __gcd(a, b)) * b; } template <class T> inline T power(T a, T b) { return (b == 0) ? 1 : a * power(a, b - 1); } template <class T> inline string toString(T t) { stringstream ss; ss << t; return ss.str(); } template <class T> inline long long toLong(T t) { stringstream ss; ss << t; long long ret; ss >> ret; return ret; } template <class T> inline int toInt(T t) { stringstream ss; ss << t; int ret; ss >> ret; return ret; } template <class T> inline bool leapYear(T t) { if (!(t % (T)400) || (!(t % (T)4) && t % (T)100)) return true; return false; } string buffer; int INT() { getline(cin, buffer); return toInt(buffer); } int LONG() { getline(cin, buffer); return toLong(buffer); } const int MX = 200010; const long long MD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long a, b, c, d; cin >> a >> b; cin >> c >> d; long long Ans = 1e12; for (long long i = 0; i < 1000000; i++) { long long l = 0, r = 1e6; long long x = b + i * a; while (l <= r) { long long m = (l + r) / 2ll; long long y = d + m * c; if (x == y) { Ans = min(Ans, x); break; } if (x < y) r = m - 1; else l = m + 1; } } if (Ans == 1e12) cout << -1 << endl; else cout << Ans << endl; return 0; }
7
CPP
b, a = map(int, input().split()) d, c = map(int, input().split()) ans = 10 ** 10 for i in range(111): for j in range(111): if a + i * b == c + j * d: ans = min(ans, a + i * b) if ans == 10 ** 10: ans = -1 print(ans)
7
PYTHON3
def nsd(x, y): while x != 0: y, x = x, y % x return y def nsk(x, y): return x * y / nsd(x, y) a, b = (int(i) for i in input().split()) c, d = (int(i) for i in input().split()) interval = nsk(a, c) n = 0.1 m = 0 while not (n.is_integer() and n >= 0): n = (b + m * a - d) / c m += 1 if n > interval + d + b or m > interval + d + b: print(-1) break if not (n > interval + d + b or m > interval + d + b): print(b + (m - 1) * a)
7
PYTHON3