solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, SZ = 1e5 + 5;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
int dxx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dyy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int main() {
int i, j, fl = 0;
int a, b, c, d, m, n, tmp;
cin >> a >> b >> c >> d;
tmp = b - a + c - d;
for (i = 1; i <= 198; i++) {
for (j = 1; j <= 198; j++) {
if ((j * c - i * a) == tmp) {
fl = 1;
break;
}
}
if (fl) break;
}
if (fl)
cout << (d + (j - 1) * c) << endl;
else
cout << "-1\n";
return 0;
}
| 7 | CPP |
from sys import maxsize, stdout, stdin, stderr
# mod = int(1e9 + 7)
#import re # can use multiple splits
tup = lambda: map(int, stdin.readline().split())
I = lambda: int(stdin.readline())
lint = lambda: [int(x) for x in stdin.readline().split()]
S = lambda: stdin.readline().replace('\n', '').strip()
#def grid(r, c): return [lint() for i in range(r)]
def debug(*args, c=6): print('\033[3{}m'.format(c), *args, '\033[0m', file=stderr)
stpr = lambda x : stdout.write(f'{x}' + '\n')
star = lambda x : print(' '.join(map(str , x)))
from collections import defaultdict,Counter
from math import gcd
from bisect import bisect_left
from math import floor,ceil
a , b = tup()
c , d = tup()
for i in range(1,101):
f1 = b + (i-1)*a
for j in range(1,101):
f2 = d + (j-1)*c
#print(f1,f2, i, j)
if f2==f1:
print(f2)
exit()
print("-1")
# n = I()
# ls = lint()
# odp = maxsize
# odn = maxsize
# #print(odp , odn)
# s = 0
# for i in ls:
# if i > 0:
# s+=i
# if i%2:
# odp = min(odp , i)
# else:
# if i%2:
# odn = min(odn , -i)
# #print(s,odp , odn,s - min(odp,odn) )
# if s%2:
# print(s)
# else:
# print(s - min(odp , odn)) #if sum is even you subtract 1 odd value (1 min odd value)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, i, j;
long long int a1, b1, c1, d1;
map<long long int, long long int> ma;
cin >> a1 >> b1;
cin >> c1 >> d1;
for (i = 0; i < 1000006; i++) {
ma[b1 + i * a1] = 1;
}
for (i = 0; i < 1000006; i++) {
if (ma[d1 + i * c1] == 1) {
cout << d1 + i * c1;
return 0;
}
}
cout << -1;
return 0;
}
| 7 | CPP |
a,b=[int(i) for i in input().split()]
c,d=[int(i) for i in input().split()]
for i in range(0,10000):
if b+a*i>=d and (b+a*i-d)%c==0:
print(b+a*i)
exit(0)
print(-1)
| 7 | PYTHON3 |
import sys,math,io,os,time,itertools,collections
mod=10**9+7
sys.setrecursionlimit(10000)
i=sys.stdin.readline
p=sys.stdout.write
#use sys.stdout.write() (remember to convert to str b4 and concatenate "\n")
global start,end
#binary search
def isin(l,x):
left=0
right=len(l)-1
if x<l[0]:
return -1
while left<=right:
mid=left + (right -left)//2
if l[mid]==x:
return mid
elif l[mid]<x:
ans=mid
left=mid+1
else:
right=mid-1
return ans
#is palindrome or not
def ispal(l):
n=len(l)
for i in range(n//2+1):
if l[i]!=l[n-i-1]:
return False
return True
#coordinate compression
def ccarray(l):
d={l[k]:k for k in range(len(l))}
m=sorted(d)
return [d[m[k]] for k in range(len(l))]
#checks if prime or not
def is_prime(n):
if n<=3:
return n>1
if n%2==0 or n%3==0:
return False
k=5
while k**2<=n:
if n%k==0 or n%(k+2)==0:
return False
k+=6
return True
#sieve of eratosthenes
def sieve(n):
prime=[True for k in range(n+1)]
p=2
while p*p<=n:
if prime[p]==True:
for k in range(p*p,n+1,p):
prime[k]=False
p+=1
def main():
a,b=[int(k) for k in i().split()]
c,d=[int(k) for k in i().split()]
ans=0
if b>=d:
for k in range(c):
temp=b-d+k*a
if temp%c==0:
p(str(b+a*k)+"\n")
break
else:
p("-1\n")
else:
for k in range(a):
temp=d-b+k*c
if temp%a==0:
p(str(d+c*k)+"\n")
break
else:
p("-1\n")
t=1
#t=int(i())
start=time.perf_counter()
for _ in range(t):
main()
end=time.perf_counter()
#print(end-start) | 7 | PYTHON3 |
a, b = [int(i) for i in input().split()]
c, d = [int(i) for i in input().split()]
res = [0 for i in range(10000000)]
for i in range(10000):
res[int(b + a*i)] = 1
for i in range(10000):
if res[d + c*i] == 1:
print(d+c*i)
exit()
print(-1) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int g, x, y;
void e_gcd(int a, int b, int &g, int &x, int &y) {
if (!b) {
x = 1;
y = 0;
g = a;
} else {
e_gcd(b, a % b, g, y, x);
y -= a / b * x;
}
}
int main() {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int tmp = d - b;
e_gcd(a, c, g, x, y);
if (tmp % g)
printf("-1\n");
else {
int b1 = c / g;
x *= (tmp / g);
x = (x % b1 + b1) % b1;
while ((tmp - a * x) / c > 0) x += b1;
printf("%d\n", a * x + b);
}
return 0;
}
| 7 | CPP |
import math
a, b = map(int, input().split())
c, d = map(int, input().split())
while b != d:
if min(b, d) > 20000:
break
if b > d:
d += c
else:
b += a
print(b if b == d else -1)
| 7 | PYTHON3 |
#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 x[1000005];
int main() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
for (int i = b; i <= 1000005; i += a) x[i] = 1;
for (int i = d; i <= 1000005; i += c)
if (x[i]) {
cout << i << endl;
return 0;
}
cout << -1 << endl;
return 0;
}
| 7 | CPP |
a, b = (int(v) for v in input().split())
c, d = (int(v) for v in input().split())
r = {b+a*i for i in range(100)}
s = {d+c*i for i in range(100)}
i = r.intersection(s)
print(min(i) if len(i) else -1)
| 7 | PYTHON3 |
a, b = map(int, list(input().split()))
c, d = map(int, list(input().split()))
for i in range(0, 1000):
for j in range(0, 1000):
if a * i + b == c * j + d:
print(a * i + b)
exit(0)
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long Inf = 1e18;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long a, b, c, d;
cin >> a >> b >> c >> d;
for (int i = max(b, d); i <= 1e6; i++) {
if ((i - b) % a == 0 && (i - d) % c == 0) {
cout << i << '\n';
return 0;
}
}
cout << -1 << '\n';
return 0;
}
| 7 | CPP |
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
a, b = map(int, input().split())
c, d = map(int, input().split())
print(solve(a, b, c, d))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int a, b, c, d;
int i;
double j;
scanf("%d %d %d %d", &a, &b, &c, &d);
for (i = 0; i <= 100; ++i) {
j = (b + a * i - d) * 1.0 / c;
if (j >= 0 && j == (int)j) {
printf("%d\n", b + a * i);
return 0;
}
}
puts("-1");
return 0;
}
| 7 | CPP |
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from itertools import (
accumulate, combinations, combinations_with_replacement, groupby,
permutations, product)
from math import (acos, asin, atan, ceil, cos, degrees, factorial, gcd, hypot,
log2, pi, radians, sin, sqrt, tan)
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits
def inp():
return(int(input()))
def inlist():
return(list(map(int, input().split())))
def instr():
s = input()
return(list(s[:len(s)]))
def invr():
return(map(int, input().split()))
a, b = invr()
c, d = invr()
g = b - d
if g % gcd(a, c) == 0:
for y in range(101):
for x in range(101):
if x * c - y*a == b-d:
print(b+y*a)
sys.exit()
else:
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool sortv(const vector<int>& v1, const vector<int>& v2) {
return v1[0] < v2[0];
}
int main() {
ios_base::sync_with_stdio(false);
long long int a, b, x, y;
cin >> a >> b >> x >> y;
for (int i = 0; i <= 500; i++) {
for (int j = 0; j <= 500; j++) {
if (b + (i * a) == y + (j * x)) {
cout << b + (i * a);
return 0;
}
}
}
cout << -1;
return 0;
}
| 7 | CPP |
""" *** Author--Saket Saumya ***
IIITM
"""
import math
import os
import random
import re
from sys import stdin,stdout
from collections import Counter
def si():
return str(input())
def ii():
return int(input())
def mi():
return map(int, input().split())
def li():
return list(mi())
a,b=mi()
c,d=mi()
while(b!=d and b<1e5):
if b<d:
b+=a
else:
d+=c
if b==d:
print(b)
else:
print('-1')
| 7 | PYTHON3 |
#Winners never quit, quiters never win............................................................................
from collections import deque as de
import math
from collections import Counter as cnt
from functools import reduce
from typing import MutableMapping
def factors(n):
return set(reduce(list.__add__,
([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
class My_stack():
def __init__(self):
self.data = []
def my_push(self, x):
return (self.data.append(x))
def my_pop(self):
return (self.data.pop())
def my_peak(self):
return (self.data[-1])
def my_contains(self, x):
return (self.data.count(x))
def my_show_all(self):
return (self.data)
def isEmpty(self):
return len(self.data)==0
arrStack = My_stack()
def isPrime(n) :
if (n <= 1) :
return False
if (n <= 3) :
return True
if (n % 2 == 0 or n % 3 == 0) :
return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
def get_prime_factors(number):
prime_factors = []
while number % 2 == 0:
prime_factors.append(2)
number = number / 2
for i in range(3, int(math.sqrt(number)) + 1, 2):
while number % i == 0:
prime_factors.append(int(i))
number = number / i
if number > 2:
prime_factors.append(int(number))
return prime_factors
def get_frequency(list):
dic={}
for ele in list:
if ele in dic:
dic[ele] += 1
else:
dic[ele] = 1
return dic
def Log2(x):
return (math.log10(x) /
math.log10(2));
def isPowerOfTwo(n):
return (math.ceil(Log2(n)) == math.floor(Log2(n)));
#here we go......................
#winners never quit, quitters never win
a,b=map(int,input().split())
c,d=map(int,input().split())
dic={}
dic[b]=1
for i in range(1,100000):
dic[b+(a*i)]=1
ch=1
for i in range(100000):
if d + (c*i) in dic:
print(d+(c*i))
ch=0
break
if ch:
print(-1)
| 7 | PYTHON3 |
from sys import stdin, stdout
a, b = map(int, stdin.readline().rstrip().split())
c, d = map(int, stdin.readline().rstrip().split())
match=-1
set1=set()
for i in range(a+b+max([b,d])):
set1.add(b+i*a)
for i in range(a+b+max([b,d])):
if d+i*c in set1:
match=d+i*c
break
print(match)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, d, i = 1, j = 1;
cin >> a >> b >> c >> d;
long long int ans1 = b;
long long int ans2 = d;
while (1) {
if (ans1 > 10000 || ans2 > 10000) {
cout << "-1";
break;
}
if (ans1 == ans2) {
cout << ans1;
break;
} else {
if (ans1 < ans2) {
ans1 = b + (i * a);
i++;
} else {
ans2 = d + (j * c);
j++;
}
}
}
return 0;
}
| 7 | CPP |
a,b=map(int,input().split())
c,d=map(int,input().split())
flag=0
if(b==d):
print(b)
else:
for i in range(100):
for j in range(100):
if b+(i*a)==d+(j*c):
#print(b+(i*a))
flag=1
break
if(flag==1):
break
#else:
#flag=1
# break
if(flag==1):
print(b+(i*a))
#break
else:
print(-1)
| 7 | PYTHON3 |
a, b = map(int, input().split())
c, d = map(int, input().split())
for i in range(max(max(a, b), max(c, d)) + 1):
if a * i + b - d >= 0 and (a * i + b - d) % c == 0:
print(a * i + b)
exit()
print(-1) | 7 | PYTHON3 |
a, b = map(int, input().split())
c ,d = map(int, input().split())
e=set(range(b,10000,a))
f=set(range(d,10000,c))
g=e & f
print(min(g) if g else -1) | 7 | PYTHON3 |
def scream_times(a,b,c,d):
n = 1000
for i in range(n):
for j in range(n):
if(b+i*a == d+c*j):
return b+i*a
return -1
if __name__ == '__main__':
a,b = map(int,input().split())
c,d = map(int, input().split())
print(scream_times(a,b,c,d)) | 7 | PYTHON3 |
a,b = map(int,input().split())
c,d = map(int,input().split())
valid = [0]*(1000001)
cnt = b
while cnt <= 1000000:
valid[cnt] += 1
cnt += a
cnt = d
while cnt <= 1000000:
valid[cnt] += 1
cnt += c
for i in range(1000001):
if valid[i] == 2:
print(i)
break
else:
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int ans = inf;
for (int i = 0; i <= 1000; i++)
for (int j = 0; j <= 1000; j++) {
if (i * a + b == j * c + d) ans = min(ans, i * a + b);
}
if (ans == inf) ans = -1;
cout << ans << endl;
}
| 7 | CPP |
from fractions import gcd
import time
a, b = map(int, input().split())
c, d = map(int, input().split())
gap = abs(b-d)
if gap % (gcd(a, c)) != 0:
print(-1)
exit()
while True:
if b < d:
r = (d - b) % a
if r == 0:
print(d)
exit(0)
b = d + (a - r)
else:
r = (b - d) % c
if r == 0:
print(b)
exit(0)
d = b + (c - r)
| 7 | PYTHON3 |
a, b = map(int, input().split())
c, d = map(int, input().split())
if b == d:
print(b)
else:
s = set()
for i in range(0, 101):
s.add(b + a * i)
for i in range(0, 101):
temp = d + c * i
if temp in s:
print(temp)
exit(0)
print(-1)
| 7 | PYTHON3 |
import sys
def diff(a, b):
return abs(a - b)
line1 = input()
line2 = input()
#line1 = '3 5'
#line2 = '3 6'
#line1 = '20 2'
#line2 = '9 19'
#
#line1 = '2 1'
#line2 = '16 12'
interval1 = int(line1.split()[0])
offset1 = int(line1.split()[1])
interval2 = int(line2.split()[0])
offset2 = int(line2.split()[1])
diff_set = set()
if offset1 <= offset2:
scream1 = offset1
scream2 = offset2
int1 = interval1
int2 = interval2
else:
scream1 = offset2
scream2 = offset1
int1 = interval2
int2 = interval1
while scream1 != scream2:
d = diff(scream1, scream2)
if d in diff_set:
print(-1)
sys.exit(0)
else:
diff_set.add(d)
while scream2 > scream1:
scream1 += int1
while scream1 > scream2:
scream2 += int2
print(scream1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
map<long long int, bool> mp1, mp2;
int main() {
long long int n, i, b, a, c, d, sum1 = 0, sum2 = 0;
cin >> a >> b;
cin >> c >> d;
sum1 = b, sum2 = d;
mp1[b] = true;
mp2[d] = true;
for (;;) {
if (sum1 > 10e5) {
break;
}
sum1 += a;
mp1[sum1] = true;
}
for (;;) {
if (mp1[sum2] == true && mp2[sum2] == true) {
cout << sum2 << endl;
return 0;
}
if (sum2 > 10e5) {
break;
}
sum2 += c;
mp2[sum2] = true;
}
cout << "-1" << endl;
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]
num_list = []
if b == d:
print(b)
elif a > c:
for i in range(max(c, d // a + 1)):
num_list.append(a * i + b)
for i in num_list:
if (i - d) % c == 0 and i - d >= 0:
print(i)
break
if num_list.index(i) == len(num_list) - 1:
print(-1)
break
elif a < c:
for i in range(max(a, b // c + 1)):
num_list.append(c * i + d)
for i in num_list:
if (i - b) % a == 0 and i - b >= 0:
print(i)
break
if num_list.index(i) == len(num_list) - 1:
print(-1)
break
elif a == c:
if (max(b, d) - min(b, d)) % a == 0:
print(max(b, d))
else:
print(-1)
| 7 | PYTHON3 |
# b + x * a = d + y * c
# x * a - y * c = d - b
def main():
a, b = map(int, input().split())
c, d = map(int, input().split())
s1 = set([b + t * a for t in range(200)])
s2 = set([d + t * c for t in range(200)])
ans = float('inf')
for x in s1:
if x in s2:
ans = min(ans, x)
if ans == float('inf'):
print(-1)
else:
print(ans)
main()
| 7 | PYTHON3 |
# Description of the problem can be found at http://codeforces.com/contest/787/problem/0
a, b = map(int, input().split())
c, d = map(int, input().split())
lcm = a * c
for i in range(10000):
t1 = b + i * a
x2 = (t1 - d) // c
if t1 == d + x2*c and x2 >= 0:
print(t1)
quit()
print("-1") | 7 | PYTHON3 |
a, b = map(int, input().split())
c, d = map(int, input().split())
res = -1
k = 0
while (k < a or (d + (k - 1) * c - b) // a < 0) and res == -1:
if (d + k * c - b) % a == 0 and (d + k * c - b) // a >= 0:
res = d + k * c
k += 1
print(res) | 7 | PYTHON3 |
#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 < 100; i++) {
if (d % a == 0) {
cout << d + mn;
return 0;
}
d += c;
}
} else if (d < b) {
for (i = 0; i < 100; 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 |
ab = list(map(int, input().split()))
a = ab[0]
b = ab[1]
cd = list(map(int, input().split()))
c = cd[0]
d = cd[1]
num_list = []
if b == d:
print(b)
elif a >= c:
for i in range(max(c, d // a + 1)):
num_list.append(a * i + b)
for i in num_list:
if (i - d) % c == 0 and i - d >= 0:
print(i)
break
if num_list.index(i) == len(num_list) - 1:
print(-1)
break
elif a < c:
for i in range(max(a, b // c + 1)):
num_list.append(c * i + d)
for i in num_list:
if (i - b) % a == 0 and i - b >= 0:
print(i)
break
if num_list.index(i) == len(num_list) - 1:
print(-1)
break
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
for (long long i = 0; i <= 1000000; i++) {
long long pos = b + i * a;
if (pos >= d && ((pos - d) % c == 0)) {
cout << pos << endl;
return 0;
}
}
cout << -1 << endl;
}
| 7 | CPP |
import sys
import collections
from collections import Counter
import itertools
import math
import timeit
#input = sys.stdin.readline
#########################
# imgur.com/Pkt7iIf.png #
#########################
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):
divisors = []
for i in range(start, int(math.sqrt(n) + 1)):
if n % i == 0:
if n / i == i:
divisors.append(i)
else:
divisors.extend([i, n // i])
return divisors
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 flin(d, x, default = -1):
left = right = -1
for i in range(len(d)):
if d[i] == x:
if left == -1: left = i
right = i
if left == -1:
return (default, default)
else:
return (left, right)
def ceil(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 prr(a, sep=' '): print(sep.join(map(str, a)))
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
a, b = mi()
c, d = mi()
for i in range(101):
for j in range(101):
if b + i*a == d + j*c:
exit(print(b + i*a))
print(-1) | 7 | PYTHON3 |
a,b=map(int,input().split())
c,d=map(int,input().split())
for i in range(10**5+1):
x=b+(a*i)
if x>=d and (x-d)%c==0:print(x);exit()
print(-1)
| 7 | PYTHON3 |
def gcdex(a, b):
i, j, k, l = 1, 0, 0, 1
while b > 0:
a, b, i, j, k, l = b, a % b, k, l, i - (a // b) * k, j - (a // b) * l
return a, i, j
a1, b1 = map(int, input().split())
c1, d1 = map(int, input().split())
a = a1
b = -c1
c = d1 - b1
d, x, y = gcdex(abs(a), abs(b))
#print(d, x, y)
if c % d == 0:
a //= d
b //= d
c //= d
x *= c
y *= c
x %= b
y = (c - a * x) // b
#print('x', x, 'y', y)
#print(x * a1 + b1, c1 * y + d1)
while x < 0 or y < 0 or int(y) != y:
x += 1
#print('1)', x * a1 + b1, c1 * y + d1)
y = (a1 * x + b1 - d1) / c1
#print('x', x, 'y', y)
#print('2)', x * a1 + b1, c1 * y + d1)
print(x * a1 + b1)
else:
print(-1) | 7 | PYTHON3 |
import sys
input = sys.stdin.readline
a, b = map(int, input().strip().split())
c, d = map(int, input().strip().split())
# line1 => y = ax + b
# line2 => y = cx + d
arr1 = [b + a*i for i in range(100)]
arr2 = set([d + c*i for i in range(100)])
for ele in arr1:
if ele in arr2:
exit(print(ele))
print(-1)
| 7 | PYTHON3 |
def sol():
for i in range(100):
for j in range(100):
if a*i + b == c*j + d:
print(a*i + b)
return
print(-1)
return
a, b = map(int, input().split())
c, d = map(int, input().split())
sol()
| 7 | PYTHON3 |
a,b = map(int, input().split())
c,d = map(int, input().split())
flag = False
for i in range(500):
for j in range(500):
if b + a*i == d + c*j:
flag = True
print(b+a*i)
if flag: break #break vòng này nếu đã True
if flag: break #break vòng này nếu False
if not flag:
print(-1)
| 7 | PYTHON3 |
def gcd(a, b):
if b == 0: return a
return gcd(b, a % b)
a, b = [int(i) for i in input().split()]
c, d = [int(i) for i in input().split()]
g = gcd(a, c)
if g != 1 and (d - b) % gcd(a, c) != 0:
print (-1)
exit()
s1 = b
s2 = d
if s1 == s2:
print(s1)
exit()
while True:
while s2 < s1:
s2 += c
if s2 == s1:
print(s1)
exit()
s1 += a | 7 | PYTHON3 |
a, b = list(map(int, input().split()))
c, d = list(map(int, input().split()))
yes = True
for i in range(1, 100):
if a % i == c % i and c % i == 0:
if d % i != b % i:
yes = False
if not yes:
print(-1)
else:
s = set()
for i in range(1000):
s.add(b)
b += a
for i in range(1000):
if d in s:
print(d)
break
d += c
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int s[N];
bool mp[N];
int main() {
ios::sync_with_stdio(0);
int a, b, c, d;
while (cin >> a >> b >> c >> d) {
memset(mp, 0, sizeof(mp));
int f = -1;
for (int i = 0; i < (2000); i++) {
mp[b] = 1;
b += a;
}
for (int i = 0; i < (2000); i++) {
if (mp[d]) {
f = d;
break;
}
d += c;
}
cout << f << endl;
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long INF = 1e9 + 5;
vector<long long> v;
map<long long, long long> mp;
void solve() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
long long cnt = 0;
long long tmpb = b, tmpd = d;
for (long long i = 0; i <= 1e5 + 5; i++) {
tmpb = b + cnt * a;
tmpd = d + cnt * c;
mp[tmpb]++;
mp[tmpd]++;
cnt++;
}
for (auto x : mp) {
if (x.second >= 2) {
cout << x.first;
return;
}
}
cout << "-1\n";
}
int32_t main() {
long long t;
t = 1;
while (t--) {
solve();
}
}
| 7 | CPP |
a,b=map(int,input().split())
c,d=map(int,input().split())
if b==d:
print(b)
else:
if b<d:
b+=((d-b)//a)*a
for i in range(200):
if (b+i*a-d)%c==0:
print(b+i*a)
break
else:
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
return n | (1LL << i);
;
}
inline long long int resetBit(long long int n, long long int i) {
return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
a %= 1000000007;
(a < 0) && (a += 1000000007);
}
inline long long int modMul(long long int a, long long int b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a * b) % 1000000007;
}
inline long long int modAdd(long long int a, long long int b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
return (a + b) % 1000000007;
}
inline long long int modSub(long long int a, long long int b) {
a %= 1000000007, b %= 1000000007;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long int modPow(long long int b, long long int p) {
long long int r = 1LL;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1LL;
}
return r;
}
inline long long int modDiv(long long int a, long long int b) {
return modMul(a, modPow(b, 1000000007 - 2));
}
bool comp(const pair<int, int> &p1, const pair<int, int> &p2) {
return p1.second < p2.second;
}
long long int converter(string a) {
long long int i, mul = 1, r, t, ans = 0LL;
if (a.length() == 0) return 0;
for (i = a.length() - 1; i >= 0; i--) {
t = a[i] - '0';
r = t % 10;
ans += (mul * r);
mul = mul * 10;
}
return ans;
}
bool vis[1112346];
int main() {
long long int a, b;
long long int c, d, i;
cin >> a >> b;
cin >> c >> d;
for (i = 0; i <= 11000; ++i) {
vis[(i * a) + b] = true;
}
for (i = 0; i <= 11000; ++i) {
if (vis[(i * c) + d] == true) {
cout << (i * c) + d << '\n';
return 0;
}
}
cout << -1 << '\n';
return 0;
}
| 7 | CPP |
from math import floor
a, b = map(int, input().split())
c, d = map(int, input().split())
found = False
for i in range(105):
B = b + i * a
j = (B - d) / c
if j >= 0 and floor(j) == j:
print(b + i * a)
found = True
break
if not found:
print(-1)
| 7 | PYTHON3 |
def main():
a, b = [int(x) for x in input().split()]
c, d = [int(x) for x in input().split()]
rick = b
morty = a
for i in range(100):
for j in range(100):
if (b+i*a)==(d+j*c):
print(b+i*a)
return
print(-1)
return
main()
| 7 | PYTHON3 |
rick = [bool(0)] * 100001
morty = [bool(0)] * 100001
a, b = input().split(' ')
a = int(a)
b = int(b)
c, d = input().split(' ')
c = int(c)
d = int(d)
i = int(1)
krick = -1
rick_krick = b
morty_krick = d
while rick_krick <= 10000:
rick[rick_krick] = 1
rick_krick += a
while morty_krick <= 10000:
morty[morty_krick] = 1
morty_krick += c
while krick == -1 and i <= 10000:
if morty[i] == 1 and rick[i] == 1:
krick = i
i += 1
print(krick)
| 7 | PYTHON3 |
def input_ints():
return list(map(int, input().split()))
def main():
a, b = input_ints()
c, d = input_ints()
for t in range(10 ** 6):
if t >= b and t >= d and (t - b) % a == 0 and (t - d) % c == 0:
print(t)
return
print(-1)
if __name__ == '__main__':
main()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e7;
int a, b, c, d;
int main() {
cin >> a >> b >> c >> d;
for (int i = 0; i <= inf; i++) {
if (d - b + c * i >= 0 && (d - b + c * i) % a == 0) {
cout << d + i * c;
return 0;
}
}
cout << -1;
return 0;
}
| 7 | CPP |
a, b = map(int, input().split())
c, d = map(int, input().split())
s1 = set()
s2 = set()
f = False
b -= a
d -= c
for i in range(10 ** 4):
b += a
s1.add(b)
d += c
s2.add(d)
if (b in s2):
print(b)
f = True
break
if (d in s1):
print(d)
f = True
break
if not f:
print(-1) | 7 | PYTHON3 |
a, b = map(int, input().split())
c, d = map(int, input().split())
ans = None
for i in range(b, 100**2, a):
if i in range(d, 100**2, c):
ans = i
break
if ans == None:
ans = -1
print(ans) | 7 | PYTHON3 |
a, b = map(int, input().split())
c, d = map(int, input().split())
rik = [b + x*a for x in range(500)]
mor = [d + x*c for x in range(500)]
for r in rik:
if r in mor:
print(r)
break
else:
print(-1)
| 7 | PYTHON3 |
r = lambda: map(int,input().split())
a,b = r()
c,d = r()
# a + 2*b
common = set(range(b,100**2,a)) & set(range(d,100**2,c))
print (min(common) if common else -1) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, i, j;
cin >> a >> b >> c >> d;
int f = 0;
for (i = 0; i <= 100; i++) {
for (j = 0; j <= 100; j++) {
if (a * i + b == c * j + d) {
f = 1;
cout << a * i + b << endl;
break;
}
}
if (f == 1) break;
}
if (f == 0) cout << -1 << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d;
int main() {
while (~scanf("%d%d%d%d", &a, &b, &c, &d)) {
int ans = -1;
for (int i = 0; i < 10000; i++) {
int x = d - b;
int y = a * i - x;
if (y >= 0 && y % c == 0) {
ans = a * i + b;
break;
}
}
printf("%d\n", ans);
}
return 0;
}
| 7 | CPP |
a=list(map(int, input().split()))
b=list(map(int, input().split()))
p=0
for i in range(1000):
for j in range(1000):
if a[1]+i*a[0]==b[1]+j*b[0]:
count=a[1]+i*a[0]
p=1
break
if p==1:
break
if p==1:
print(count)
else:
print(-1) | 7 | PYTHON3 |
a, b = map(int,input().split())
c, d = map(int,input().split())
x = d - b
m = -1
n = 0
if x%a == 0:
while True:
m = m + 1
n = (x+m*c)/a
if n < 0:
continue
if (x+m*c)%a == 0:
print (d+m*c)
break
elif x%a != 0 and c%a == 0:
print(-1)
else:
while m <= a:
m = m + 1
if (x%a + (c%a)*m)%a == 0:
print (d+m*c)
exit()
print(-1) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e3 + 7;
const int INF = 1e9 + 7;
int n;
int main() {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int flag = INF;
for (int i = 0; i <= 1000; i++) {
for (int j = 0; j <= 1000; j++)
if (b + i * a == d + j * c) {
flag = min(flag, b + i * a);
break;
}
}
if (flag == INF) flag = -1;
printf("%d\n", flag);
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d;
int ans;
int main() {
scanf("%d%d%d%d", &a, &b, &c, &d);
ans = -1;
for (int i = 0; i <= 999; ++i)
for (int j = 0; j <= 999; ++j)
if (b + a * i == d + c * j)
ans = min(ans == -1 ? 1000000000 : ans, b + a * i);
printf("%d\n", ans);
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
int main() {
int a, b, c, d, x, i, z[105];
scanf("%d%d%d%d", &a, &b, &c, &d);
for (i = 0; i < 105; i++) z[i] = 0;
for (x = 0, i = 0; b != d; i++) {
if (a * i + b - d >= 0) {
if ((a * i + b - d) % c == 0)
break;
else if (z[(a * i + b - d) % c] == 1) {
x = 1;
break;
}
z[(a * i + b - d) % c] = 1;
}
}
if (x == 1)
printf("-1\n");
else
printf("%d\n", a * i + b);
return 0;
}
| 7 | CPP |
a,b=map(int,input().split())
c,d=map(int,input().split())
for i in range(100000):
if (b+i*a-d)%c==0 and b+i*a-d>=0:
print(b+i*a)
break
else:
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, i, j, flag;
while (~scanf("%d%d%d%d", &a, &b, &c, &d)) {
flag = 0;
for (i = 0; i <= 10000; i++) {
for (j = 0; j <= 10000; j++) {
if ((b + a * i) == (d + c * j)) {
flag = 1;
break;
}
if ((b + a * i) < (d + c * j)) {
break;
}
}
if (flag == 1) {
break;
}
}
if (flag) {
printf("%d\n", b + a * i);
} else {
printf("-1\n");
}
}
return 0;
}
| 7 | CPP |
def sum_(a_, d_):
return a_ + n * d_
a, b = map(int, input().split(' '))
c, d = map(int, input().split(' '))
lim = a * c + b + d
n = 0
sli = [sum_(b, a)]
while sli[n] <= lim:
n += 1
sli.append(sum_(b, a))
n = 0
slj = [sum_(d, c)]
while slj[n] <= lim:
n += 1
slj.append(sum_(d, c))
r = -1
for i in sli:
for j in slj:
if i == j:
r = j
break
if i == r:
break
print(r)
| 7 | PYTHON3 |
import sys
[a, b] = [int(x) for x in input().split(" ")]
[c, d] = [int(x) for x in input().split(" ")]
# aX + b == cY + d
maximum = max(a, b, c, d)
for i in range(maximum + 1):
y = (a * i + b - d) / c
x = (c * y + d - b) / a
answer = (a * i + b)
if (x).is_integer() and (y).is_integer() and answer >= max(b, d):
print(answer)
sys.exit()
print(-1)
| 7 | PYTHON3 |
import sys
a, b = map(int, input().split())
c, d = map(int, input().split())
for i in range(max(b,d),100000):
if (i-b) % a == 0 and (i-d) % c == 0:
print(i)
sys.exit()
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int b, a, d, c;
cin >> a >> b >> c >> d;
int k = 0;
while (b != d) {
if (d > b) {
b += a;
} else {
d += c;
}
k++;
if (k == 100005) {
cout << "-1";
return 0;
}
}
cout << b;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int a, b, c, d;
cin >> a >> b >> c >> d;
for (int i = max(b, d); i <= 1e5; ++i)
if ((i - b) % a == 0 && (i - d) % c == 0) return cout << i, 0;
cout << -1;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
while (a != b)
if (a > b)
a -= b;
else
b -= a;
return a;
}
int main() {
int a, b, c, d, i = 1, j = 1, k;
cin >> a >> b >> c >> d;
if (abs(d - b) % gcd(a, c))
cout << -1;
else {
while (b != d) {
if (b < d)
b += a;
else
d += c;
}
cout << b;
}
}
| 7 | CPP |
a,b=map(int,input().split())
c,d=map(int,input().split())
t=[]
for n1 in range(1,101):
for n2 in range(1,101):
if(b+(n1-1)*a==d+(n2-1)*c):
t.append(b+(n1-1)*a)
if len(t)!=0:
print(min(t))
else:
print(-1) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d, i, j = -1, x, y;
cin >> a >> b >> c >> d;
int t[100], s[100];
for (i = 0; i < 100; i++) {
j++;
x = b + a * i;
t[i] = {x};
y = d + c * j;
s[i] = {y};
}
for (i = 0; i < 100; i++) {
for (j = 0; j < 100; j++) {
if (t[i] == s[j]) {
cout << t[i];
return 0;
}
}
}
cout << "-1";
return 0;
}
| 7 | CPP |
ans=[]
a,b=map(int,input().split())
c,d=map(int,input().split())
l=[b,b+a]
ll=[d,d+c]
for i in range(100):
l+=[b+i*a]
for i in range(100):
ll+=[d+i*c]
for i in l:
if i in ll: ans+=[i]
if len(ans)==0:
print(-1)
else: print(ans[0])
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int p[100010], q[100010];
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
map<int, int> mp;
for (int i = 0; i <= 10000; i++) {
p[i] = b + a * i;
q[i] = d + c * i;
mp[q[i]] = 1;
}
for (int i = 0; i <= 10000; i++) {
if (mp[p[i]]) {
cout << p[i] << endl;
return 0;
}
}
cout << "-1" << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a, b, c, d;
cin >> a >> b >> c >> d;
int flag = 1;
for (int i = 0; i < 100; i++) {
int lhs = a * i + b;
int j = (lhs - d) / c;
if (j < 0) continue;
int rhs = c * j + d;
if (lhs == rhs) {
flag = 0;
cout << lhs << endl;
break;
}
}
if (flag) cout << -1 << endl;
return 0;
}
| 7 | CPP |
a,b = map(int, input().split())
c,d = map(int, input().split())
for i in range(1000 * 1000):
if (d - b + i * c) % a == 0 and (d - b + i * c) > -1:
print(d + i * c)
exit(0)
print(-1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1000000 + 5;
bool vis[maxn];
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
vis[b] = true;
for (int i = 1; b <= maxn - 5; i++) {
vis[b] = true;
b += a;
}
bool flag = false;
for (int i = 1; d <= maxn - 5; i++) {
if (vis[d]) {
flag = true;
break;
}
d += c;
}
if (flag)
printf("%d\n", d);
else
printf("-1\n");
return 0;
}
| 7 | CPP |
a,b=map(int,input().split())
s=0
c,d=map(int,input().split())
flag=0
while(d!=b):
if(s==100000):
print(-1)
b=d
flag=1
if(d<b):
d+=max(c*((b-d)//c),c)
else:
b+=max(a*((d-b)//a),a)
s+=1
if(flag==0):
print(b)
| 7 | PYTHON3 |
import math
def gcdex(a, b):
if b == 0:
return a, 1, 0
else:
d, x1, y1 = gcdex(b, a % b)
return d, y1, x1 - (a // b) * y1
n1, n2 = map(int, input().split())
n3, n4 = map(int, input().split())
a = n1
b = -n3
c = n4 - n2
d, x0, y0 = gcdex(abs(a), abs(b))
if c % d != 0:
print(-1)
else:
x0 *= c // d
y0 *= c // d
if (a < 0):
x0 *= -1
if (b < 0):
y0 *= -1
if b // d > 0:
while x0 < 0 or y0 < 0:
x0 += b//d
y0 -= a//d
if b // d < 0:
while x0 < 0 or y0 < 0:
x0 -= b//d
y0 += a//d
if b // d > 0:
while x0 >= 0 and y0 >= 0:
x0 -= b//d
y0 += a//d
x0 += b//d
y0 -= a//d
if b // d < 0:
while x0 >= 0 and y0 >= 0:
x0 += b//d
y0 -= a//d
x0 -= b//d
y0 += a//d
print(n2 + x0 * n1)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool x[10001000];
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
for (int i = 0; i < 100000; i++) {
x[b + i * a] = 1;
}
bool ok = false;
for (int i = 0; i < 100000; i++) {
int t = d + i * c;
if (x[t]) {
ok = true;
cout << t << endl;
break;
}
}
if (!ok) cout << -1 << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
short a, b, c, d;
cin >> a >> b >> c >> d;
short n1 = b, n2 = d;
do {
if (n2 > n1) do {
n1 += a;
if (n1 < 0) n1 = -1;
} while (n1 > 0 && n2 > n1);
if (n1 > n2) do {
n2 += c;
if (n2 < 0) n1 = -1;
} while (n1 > 0 && n1 > n2);
} while (n1 > 0 && n1 != n2);
cout << n1;
}
| 7 | CPP |
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())
const = b - d + c - a
# m*c + n*(-a) = const
# Linear Diophantine equation gives the below if condition.
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 |
l=input().split()
s=input().split()
a=int(l[0])
b=int(l[1])
c=int(s[0])
d=int(s[1])
t=[]
p=[]
f=1
i=0
while(i<1000):
t.append(b+i*a)
p.append(d+i*c)
if(b+i*a in p):
print(b+i*a)
f=0
break
if(d+i*c in t):
print(d+i*c)
f=0
break
i+=1
if(f):
print("-1") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(0);
int a, b, c, d;
scanf("%d %d", &a, &b);
scanf("%d %d", &c, &d);
for (int i = 0; i < 10000000; i++) {
int k = b + a * i;
if (k - d < 0) continue;
if ((k - d) % c == 0) {
printf("%d", k);
return 0;
}
}
printf("-1");
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
int i;
double j;
cin >> a >> b >> c >> d;
for (i = 0; i <= 100; ++i) {
j = (b + a * i - d) * 1.0 / c;
if (j >= 0 && j == (int)j) {
cout << (b + a * i);
return 0;
}
}
cout << -1 << endl;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const int INF = 1000001000;
const long long INFLL = INF * 1LL * INF;
const int mod = 1000 * 1000 * 1000 + 7;
const int mod9 = 1000 * 1000 * 1000 + 9;
const int modr = 99990001;
const long double PI = 3.1415926535897932385;
template <class T>
void zero(T val, T& first) {
first = val;
}
template <class T, class... Targs>
void zero(T val, T& first, Targs&... Fargs) {
first = val;
zero(val, Fargs...);
}
template <class T, class T2>
std::istream& operator>>(std::istream& is, pair<T, T2>& p) {
return is >> p.first >> p.second;
}
template <class T>
std::istream& readN(T& first, int n, int st = 0) {
for (int i = st, iend = (st + n - 1); i <= iend; i++) cin >> first[i];
return cin;
}
template <class T>
std::istream& readS(set<T>& first, int n) {
T second = *first.rbegin();
for (int i = 0, iend = (n - 1); i <= iend; i++) {
cin >> second;
first.insert(second);
}
return cin;
}
template <class T>
std::istream& read(T& first) {
return cin >> first;
}
template <class T, class... Targs>
std::istream& read(T& first, Targs&... Fargs) {
return read(first), read(Fargs...);
}
template <class T, class T2>
std::ostream& operator<<(std::ostream& os, pair<T, T2> p) {
return os << p.first << " " << p.second;
}
template <class T>
std::ostream& operator<<(std::ostream& os, vector<T> v) {
bool f = true;
for (auto second : v) {
if (!f) os << ' ';
os << second;
f = false;
}
return os;
}
template <class T>
std::ostream& operator<<(std::ostream& os, set<T> v) {
bool f = true;
for (auto second : v) {
if (!f) os << ' ';
os << second;
f = false;
}
return os;
}
template <class T>
std::ostream& operator<<(std::ostream& os, multiset<T> v) {
bool f = true;
for (auto second : v) {
if (!f) os << ' ';
os << second;
f = false;
}
return os;
}
template <class T, class T2>
std::ostream& operator<<(std::ostream& os, map<T, T2> v) {
bool f = true;
for (pair<T, T2> second : v) {
if (!f) os << ' ';
os << second.first << "=>" << second.second;
f = false;
}
return os;
}
template <class T>
std::ostream& outV(T first, char del = ' ') {
bool f = true;
for (auto second : first) {
if (!f) cout << del;
cout << second;
f = false;
}
return cout;
}
template <class T>
std::ostream& outN(T first, int n = -1, int st = 0) {
for (int i = st, iend = (n == -1 ? (int)first.size() - 1 : st + n - 1);
i <= iend; i++) {
cout << first[i];
if (i < iend) cout << ' ';
}
return cout;
}
template <class T>
std::ostream& outAN(T first, int n = -1, int st = 0) {
for (int i = st, iend = (n - 1); i <= iend; i++) {
cout << first[i];
if (i < iend) cout << ' ';
}
return cout;
}
template <class T>
std::ostream& outA2(T first, int n, int m) {
for (int i = 0, iend = (n - 1); i <= iend; i++) {
for (int j = 0, jend = (m - 1); j <= jend; j++)
cout << first[i][j] << (j == m - 1 ? '\n' : ' ');
}
return cout;
}
template <class T>
std::ostream& out(T first) {
return cout << first;
}
template <class T, class... Targs>
std::ostream& out(T first, Targs... Fargs) {
return out(first) << " ", out(Fargs...);
}
template <typename T>
void srt(T& a, int st, int fn, bool isArr) {
sort(a + st, a + fn + 1);
}
template <class T>
void srt(T& a, int st = 0, int fn = 0) {
sort(a.begin() + st, fn ? a.begin() + fn + 1 : a.end());
}
template <typename T>
T rev_num(T a) {
T r = 0;
for (; a; a /= 10) r = r * 10 + a % 10;
return r;
}
template <typename T>
void rev(T& a, int st, int fn, bool isArr) {
reverse(a + st, a + fn + 1);
}
template <class T>
void rev(T& a, int st = 0, int fn = 0) {
reverse(a.begin() + st, fn ? a.begin() + fn + 1 : a.end());
}
long long sqr(long long a) { return a * a; };
long long sqr(int a) { return a * 1LL * a; };
long double sqr(long double a) { return a * a; };
long double dist(pair<long long, long long> first,
pair<long long, long long> second) {
return sqrt(sqr(first.first - second.first) +
sqr(first.second - second.second));
}
long long phi(int n) {
int res = n;
for (long long i = 2; i * i <= n; i++)
if (n % i == 0) {
while (n % i == 0) n /= i;
res -= res / i;
}
if (n > 1) res -= res / n;
return res;
}
long long bpm(long long a, long long n = -2, long long m = mod) {
n = n < 0 ? n + m : n;
long long r = 1;
while (n) {
if (n & 1) r = (r * a) % m;
a = (a * a) % m;
n >>= 1;
}
return r;
}
unsigned long long gcd(unsigned long long a, unsigned long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
vector<int> ero_p, ero_l;
void ero(int n) {
ero_l.resize(n + 1);
ero_l[0] = -1;
for (int i = 2, iend = (n); i <= iend; i++)
if (!ero_l[i]) {
ero_p.push_back(i);
ero_l[i] = i;
for (long long j = i * 1LL * i; j <= n; j += i) {
ero_l[j] = i;
}
}
}
long long gcd_cb(long long a, long long b, long long& first,
long long& second) {
if (!b) {
first = 1;
second = 0;
return a;
}
long long x1, y1, g;
g = gcd_cb(b, a % b, x1, y1);
first = y1;
second = x1 - a / b * y1;
return g;
}
vector<long long> fact;
void fact_prec(int n = 20) {
fact.resize(n + 1);
fact[0] = 1;
for (int i = 1, iend = (n); i <= iend; i++) {
fact[i] = fact[i - 1] * i;
}
}
vector<long double> factd;
void fact_precd(int n = 146) {
factd.resize(n + 1);
factd[0] = 1;
for (int i = 1, iend = (n); i <= iend; i++) {
factd[i] = factd[i - 1] * i;
}
}
string str(long long a) {
string r = "";
for (; a; a /= 10) r += a % 10 + '0';
rev(r);
return r;
}
template <class T>
int bitc(T first) {
int r = 0;
for (T d = first; d >= 0; d >>= 1) r += d & 1;
return r;
}
const int N = 7000006;
int main() {
long long a, b, c, d;
read(a, b, c, d);
long long ans = -1;
for (int i = 0, iend = (100005 - 1); i <= iend; i++) {
long long time = a * i + b;
if (time >= d && (time - d) % c == 0) {
ans = time;
break;
}
}
out(ans) << "\n";
return 0;
}
| 7 | CPP |
#The Monster
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 |
def gcd(a,b):
if a < 0:
a = -a
if b < 0:
b = -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+j*c):
print(b + i*a)
break
i+=1 | 7 | PYTHON3 |
def main():
a, b = map(int, input().split())
c, d = map(int, input().split())
for i in range(100000):
if i - b >= 0 and (i - b) % a == 0:
if i - d >= 0 and (i - d) % c == 0:
print(i)
return
print(-1)
main() | 7 | PYTHON3 |
a,b = map(int,input().split())
c,d = map(int,input().split())
for i in range(0,1000):
for j in range(0,1000):
if b + a*i == d + c*j:
print(b + a*i)
exit(0)
print(-1)
| 7 | PYTHON3 |
import sys
a, b = map(int, input().split())
c, d = map(int, input().split())
s = set()
for i in range(1001):
s.add( b + i * a)
for i in range(1000):
num = d + c * i
if num in s:
print(num)
sys.exit()
print(-1)
| 7 | PYTHON3 |
inp = input().split()
a = int(inp[0])
b = int(inp[1])
inp = input().split()
c = int(inp[0])
d = int(inp[1])
for x in range(101):
for y in range(101):
if (a*x+b) == (c*y+d):
print(a*x+b)
exit(0)
print(-1) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a, b, c, d;
while (cin >> a >> b >> c >> d) {
map<long long int, long long int> p;
for (long long int i = 0; i <= 100; i++) {
long long int x = b + a * i;
p[x] = 1;
}
bool f = true;
for (long long int i = 0; i <= 100; i++) {
long long int x = d + c * i;
if (p[x]) {
cout << x << endl;
f = false;
break;
}
}
if (f) cout << "-1" << endl;
}
}
| 7 | CPP |
a,b = map(int,input().split())
c,d = map(int,input().split())
for i in range(100000):
if(b+a*i-d)>=0 and (b+a*i-d)%c==0:
print(b+a*i)
break
else:
print(-1) | 7 | PYTHON3 |
i=1
yes=-1
y=0
a,b=map(int,input().split())
c,d=map(int,input().split())
#both Equations are in a form of straight line.
#the case is if we draw one line segment prependicular on y then
#definetly, the two points will pass through that line and Morty and Rick must be equal.
#i*a+b=j*c+d,
if(b==d):
print(d)
else:
for j in range(100):
i=(j*c+d-b)/a
if (i-int(i)==0 and i>=0):
yes=i
if(yes!=-1):
break
if (yes==-1):
print(-1)
else:
y=yes*a+b
print(int(y))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int i = 0, j = 0;
while (b != d && i <= 100 && j <= 100) {
if (b > d) {
d += c;
j++;
} else {
b += a;
i++;
}
}
if (i <= 100 && j <= 100)
cout << d << endl;
else
cout << -1 << endl;
return 0;
}
| 7 | CPP |
a,b = map(int,input().split(' '))
c,d = map(int,input().split(' '))
for x in range(0,101):
f = False
for y in range(0,101):
if (a*x+b) - (c*y+d) == 0:
print(a*x+b)
f = True
break
if f:
break;
if f == False:
print(-1)
| 7 | PYTHON3 |