solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> const int maxn = 1e5 + 1; std::vector<int> nxt[maxn]; std::map<unsigned long long, int> cnt; unsigned long long hash[maxn], root[maxn]; int n, x, y, ans, fa[maxn], siz[maxn], son[maxn], prime[maxn]; void init() { static const int maxm = 2e6; static int cnt, vis[maxm]; for (int i = 2; cnt <= n; i++) { if (!vis[i]) prime[cnt++] = i; for (int j = 0; j < cnt && i * prime[j] < maxm; j++) if (vis[i * prime[j]] = 1, i % prime[j] == 0) break; } return; } void dfs1(int u) { hash[u] = siz[u] = 1; for (int v : nxt[u]) if (v != fa[u]) fa[v] = u, dfs1(v), hash[u] += hash[v] * prime[siz[v]], siz[u] += siz[v]; ++cnt[hash[u]]; return; } void dfs2(int u) { son[u] = cnt.size(); --cnt[root[u]]; if (!cnt[root[u]]) cnt.erase(root[u]); for (int v : nxt[u]) if (v != fa[u]) { root[v] = (root[u] - hash[v] * prime[siz[v]]) * prime[n - siz[v]] + hash[v]; ++cnt[root[v]], --cnt[hash[v]], ++cnt[root[u] - hash[v] * prime[siz[v]]]; if (!cnt[hash[v]]) cnt.erase(hash[v]); dfs2(v); --cnt[root[v]], ++cnt[hash[v]], --cnt[root[u] - hash[v] * prime[siz[v]]]; if (!cnt[root[v]]) cnt.erase(root[v]); if (!cnt[root[u] - hash[v] * prime[siz[v]]]) cnt.erase(root[u] - hash[v] * prime[siz[v]]); } ++cnt[root[u]]; return; } int main() { std::cin.tie(0), std::cout.tie(0), std::ios::sync_with_stdio(false); std::cin >> n, init(); for (int i = 1; i < n; i++) std::cin >> x >> y, nxt[x].emplace_back(y), nxt[y].emplace_back(x); dfs1(1), root[1] = hash[1], dfs2(1); for (int i = 1; i <= n; i++) if (son[i] > son[ans]) ans = i; std::cout << ans << std::endl; return 0; }
10
CPP
import math R=lambda:list(map(int,input().split())) a,b=R() c,d=R() if abs(b-d)%math.gcd(a,c)!=0: print(-1) exit(0) while b != d: if b<d: b+=a else: d+=c print(b)
7
PYTHON3
def gcd(a,c): if c==0: return a return gcd(c,a%c) a,b=map(int,input().split()) c,d=map(int,input().split()) f=gcd(max(a,c),min(a,c)) if (b-d)%f!=0: print(-1) else: i=0 j=0 s1=b s2=d while True: if s1>s2: s2=d+i*c i+=1 elif s2>s1: s1=b+j*a j+=1 else: print(s1) break
7
PYTHON3
a ,b = map(int,input().split()) c ,d = map(int,input().split()) p = True for i in range(max(b,d),1000000): if (i-d)%c==0 and (i-b)%a==0: print(i) p = False break if p: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; for (int i = 0; i <= 1e6; i++) { long long x = (i * c) + d - b; if (x < 0) continue; if (x % a == 0) { cout << x + b; return 0; } } cout << "-1"; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long i, a, b, c, d; cin >> a >> b >> c >> d; for (i = 1; i < 10000000; i++) { if ((i - b) % a == 0) { if ((i - d) % c == 0) { if (i >= b) { if (i >= d) { cout << i; return 0; } } } } } cout << -1; }
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 if a + b > c + d: for i in range(a + b): list1.append(a * counter1 + b) counter1 += 1 for i in list1: if (i - d) % c == 0 and (i - d) >= 0: print(i) break if list1.index(i) == len(list1) - 1: print(-1) break else: for i in range(c + d): list2.append(c * counter2 + d) counter2 += 1 for i in list2: if (i - b) % a == 0 and (i - b) >= 0: print(i) break if list2.index(i) == len(list2) - 1: print(-1) break
7
PYTHON3
#!/usr/bin/python3 a,b = [int(x) for x in input().split()] c,d = [int(x) for x in input().split()] i = 0 j = 0 flag = 0 for i in range(1000000): s1 = b + a*i if (s1 >= d and 0 == (s1-d)%c): print(s1) flag = 1 break if (not flag): print("-1")
7
PYTHON3
# -*- coding: utf-8 -*- a, b = map(int, input().split(' ')) c, d = map(int, input().split(' ')) if d < b: x = b b = d d = x x = a a = c c = x k = 1 r = -1 while k < 100000: n = (d-b+c*(k-1))/a+1 if n == int(n): n = int(n) r = b+a*(n-1) break k += 1 print(r)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { int a, b, c, d, i = 1, ans; set<int> s1; set<int> s2; cin >> a >> b >> c >> d; if (b == d) { cout << b << endl; return 0; } s1.insert(b); s2.insert(d); int tmp = max(b - d, d - b); if (tmp % gcd(a, c) != 0) { cout << -1 << endl; return 0; } else { while (true) { tmp = b + i * a; if (s2.find(tmp) != s2.end()) { ans = tmp; break; } else { s1.insert(tmp); } tmp = d + i * c; if (s1.find(tmp) != s1.end()) { ans = tmp; break; } else { s2.insert(tmp); } i++; } } cout << ans << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 0; i < 10001; ++i) { if ((d - b + i * c) >= 0 && (d - b + i * c) % a == 0) { cout << d + i * c << endl; return 0; } } cout << -1 << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1; y = 0; return a; } else { long long d = exgcd(b, a % b, x, y); long long tmp = x; x = y; y = tmp - a / b * y; return d; } } long long Rand() { return 1LL * rand() * rand(); } int main() { long long A, B, C, D, x, y; long long a, b, c, d; cin >> A >> B >> C >> D; srand(time(0)); a = A; b = -C; c = D - B; long long gd = exgcd(a, b, x, y); if (c % gd != 0) { cout << "-1" << endl; return 0; } x = x * c / gd; y = y * c / gd; long long p = b / gd; x = (x % p + p) % p; if (x < 0) { x += abs(p); } while ((c - a * x) / b < 0) { x += abs(p); } cout << B + A * x << endl; return 0; }
7
CPP
def check(a, b, c, d): for i in range(101): for j in range(101): if (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(check(a, b, c, d))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; map<int, int> m; int main() { long long int a, b, c, d; cin >> a >> b >> c >> d; long long int cnt = 0; while (cnt < 100000) { if (b < 0) break; m[b] = 1; b += a; cnt++; } cnt = 0; while (cnt < 100000) { if (m[d]) { cout << d << '\n'; return 0; } if (d < 0) break; d += c; cnt++; } cout << -1 << '\n'; }
7
CPP
import math inp = lambda : map(int, input().split()) a, b = inp() c, d = inp() for i in range(10000): if (i * c + d - b) % a == 0 and (i * c + d - b) // a >= 0: print(i * c + d) quit() print(-1)
7
PYTHON3
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 if a + b > c + d: for i in range(c): list1.append(a * counter1 + b) counter1 += 1 for i in list1: if (i - d) % c == 0 and (i - d) >= 0: print(i) break if list1.index(i) == len(list1) - 1: print(-1) break elif a + b < c + d: for i in range(a): list2.append(c * counter2 + d) counter2 += 1 for i in list2: if (i - b) % a == 0 and (i - b) >= 0: print(i) break if list2.index(i) == len(list2) - 1: print(-1) break elif a + b == c + d: if b != d: print(a + b) else: print(b)
7
PYTHON3
import sys a,b = map(int,input().split()) c,d = map(int,input().split()) for i in range(1000): for j in range(1000): if b+(a*i)==d+(c*j): print(b+(a*i)) exit() print(-1)
7
PYTHON3
from fractions import gcd a, b = map(int, input().split()) c, d = map(int, input().split()) g = gcd(a, c) if (d - b) % g == 0: big_tab = [0 for _ in range(101 * 101)] for i in range(b, 101 * 101, a): big_tab[i] = 1 for i in range(d, 101 * 101, c): if big_tab[i]: print(i) break else: print(-1)
7
PYTHON3
a,b=list(map(int,input().split())) c,d=list(map(int,input().split())) l1=[] l2=[] for i in range(1,101): f1=(b+(i-1)*a) f2=(d+(i-1)*c) l1.append(f1) l2.append(f2) an=True for i in range(len(l1)): if l1[i] in l2: print(l1[i]) an=False break if an==True: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 0; i <= 1000000; ++i) { int curr = i * a + b - d; if (curr % c == 0) { curr /= c; if (curr >= 0) { printf("%d\n", i * a + b); return 0; } } } puts("-1"); return 0; }
7
CPP
#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[100000]; int main() { scanf("%d%d%d%d", &a, &b, &c, &d); for (int i = b; i < 100000; i += a) { visited[i] = 1; } for (int i = d; i < 100000; i += c) { if (visited[i]) { printf("%d", i); return 0; } } printf("-1"); return 0; }
7
CPP
a, b = map(int, input().split()) c, d = map(int, input().split()) r = set() now = b while now < d: now += a d %= c while True: if now % c == d: print(now) exit(0) if (now % c) in r: print(-1) exit(0) r.add(now % c) now += a
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; while (b != d && b < 1e8) { b < d ? b += a : d += c; } if (b == d) cout << b << endl; else cout << "-1\n"; return 0; }
7
CPP
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()) # Phuong trinh Diophante if(((d -b) % GCD(a, c)) != 0): print(-1) else: j = 0 while(True): i = (d + j*c -b) // a if( i >= 0 and ((d + j*c -b) % a == 0)): print(d + j*c) break j += 1
7
PYTHON3
from os import path import sys,time # mod = int(1e9 + 7) # import re from math import ceil, floor,gcd,log,log2 ,factorial,sqrt from collections import defaultdict ,Counter , OrderedDict , deque from itertools import combinations ,accumulate # from string import ascii_lowercase ,ascii_uppercase from bisect import * from functools import reduce from operator import mul star = lambda x: print(' '.join(map(str, x))) grid = lambda r :[lint() for i in range(r)] INF = float('inf') if (path.exists('input.txt')): sys.stdin=open('input.txt','r');sys.stdout=open('output.txt','w'); import sys from sys import stdin, stdout from collections import * from math import gcd, floor, ceil def st(): return list(stdin.readline().strip()) def inp(): return int(stdin.readline()) def inlt(): return list(map(int, stdin.readline().split())) def invr(): return map(int, stdin.readline().split()) def gcd(a,b): if a==0: return b return gcd(b%a,a) def gcdExtended(a, b): if a == 0 : return b, 0, 1 gcd, x1, y1 = gcdExtended(b%a, a) x = y1 - (b//a) * x1 ; y = x1 return gcd, x, y def LDA(a,b,c): g,x0,y0 = gcdExtended(a,b) x = x0*(c//g) y = y0*(c//g) return x,y def solve(): d1,a1 = invr() d2,a2 = invr() s = set(range(a1,10000,d1))&set(range(a2,10000,d2)) print(min(s)if s else -1) t = 1 #t = inp() for _ in range(t): solve()
7
PYTHON3
#include <bits/stdc++.h> int min(int x, int y) { if (x < y) return x; else return y; } int main() { int a, b, c, d; int i, j, n, m; scanf("%d%d", &a, &b); scanf("%d%d", &c, &d); n = b; m = d; i = 1; j = 1; int flag = 0xFFFFFFF; for (i = 0; i <= 1000; i++) { for (j = 0; j <= 1000; j++) { if (b + a * i == d + c * j) { flag = min(flag, b + a * i); } } } if (flag == 0xFFFFFFF) printf("-1\n"); else printf("%d\n", flag); }
7
CPP
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time sys.setrecursionlimit(10**7) inf = 10**20 mod = 10**9 + 7 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): a,b = LI() c,d = LI() t = 0 while b!=d: t += 1 if t > 100000: b = -1 break if b<d: b += a else: d += c return b print(main())
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; for (int i = 0; i < 9999; i++) { for (int j = 0; j < 9999; j++) { if (b + (a * i) == d + (c * j)) { cout << b + (a * i); return 0; } } } cout << -1 << endl; return 0; }
7
CPP
a,b=input().strip().split(' ') c,d=input().strip().split(' ') a,b,c,d=(int(a),int(b),int(c),int(d)) for i in range(1000): for j in range(1000): l1=b+i*a l2=d+j*c if l1==l2: print(l1) exit() print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MX = 1e4 + 9; const long long inf = (1ll << 60); int a, b, c, d; bool vis[MX * 100]; int main() { cin >> a >> b >> c >> d; for (int i = 0; b + (a * i) <= 1e6; i++) { vis[b + (a * i)] = 1; } for (int i = 0; d + (c * i) <= 1e6; i++) { if (vis[d + (c * i)]) { cout << d + (c * i); return 0; } } puts("-1"); }
7
CPP
a, b = map(int, input().split()) c, d = map(int, input().split()) for i in range(b, 1000000, a): if i % c == d % c and i >= d: print(i) exit(0) print(-1)
7
PYTHON3
#from dust i have come dust i will be 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
import sys import re input = lambda:sys.stdin.readline() import functools @functools.lru_cache(maxsize = 10000) def Main(): a, b = map(int, input().split()) c, d = map(int, input().split()) Rick = 0 Morty = 0 A = [] B = [] A.append(b) B.append(d) for i in range(1, 501): A.append(b + a*i) B.append(d + c*i) flag = 0 for i in range(501): for j in range(501): if A[i] == B[j]: print(A[i]) flag = 1 break if flag == 1: break if flag == 0: print( -1) if __name__ == '__main__': Main()
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+1): 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() { int a, b, c, d; cin >> a >> b >> c >> d; int ans = -1; for (int i = 0; i <= 1000; i++) { for (int j = 0; j <= 1000; j++) { if (b + i * a == d + j * c && (ans == -1 || b + i * a < ans)) { ans = b + i * a; } } } cout << ans << "\n"; return 0; }
7
CPP
a, b = map(int, input().split()) c, d = map(int, input().split()) if b < d: a, b, c, d = c, d, a, b s = set() while True: t = (b-d)%c if t in s: print("-1") break if t == 0: print(b) break b += a s.add(t)
7
PYTHON3
a,b=map(int,input().split()) c,d=map(int,input().split()) rick=[] monty=[] for i in range(0,101,1): rick.append(b+(i*a)) monty.append(d+(i*c)) c=0 for ele1 in rick: for ele2 in monty: if ele1==ele2: print(ele1) c=1 break if c==1: break if c==0: print(-1)
7
PYTHON3
# cook your dish here a,b=map(int,input().split()) c,d=map(int,input().split()) k=0 for i in range(101): for j in range(101): if(b+a*i==d+c*j): print(d+c*j) k=1 break if(k==1): break if(k==0): print('-1')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; long long rem, val = b - d; int iteration = 1; while (val < 0) { val += a; iteration++; } rem = val % c; long long start = rem; bool first = true; while (rem != 0 && iteration < 100000) { first = false; val += a; rem = val % c; iteration++; } if (rem == 0) { cout << b + (iteration - 1) * a << "\n"; } else { cout << -1 << "\n"; } }
7
CPP
import math r = lambda: map(int, input().split()) a, b = r() c, d = r() if abs(b - d) % math.gcd(a, c) != 0: print(-1) exit(0) while b != d: if b < d: b += a else: d+=c print(b)
7
PYTHON3
a , b = map(int , input().split()) c , d = map(int , input().split()) x = b y = d ok = 0 aa = [0] * 11111111 for i in range(100000): aa[x] = 1 x += a for i in range(100000): if aa[y] == 1: ok = 1 print(y) break y += c if ok == 0: print (-1)
7
PYTHON3
a, b = map(int, (input().split())) c, d = map(int, (input().split())) n = 0 def isEvenOrOdd(a, b): if a % 2 == 0 and b % 2 == 0: return "Even" elif a == 2*b: return "Odd" else: return "None" if (isEvenOrOdd(a, b) == "Even" and isEvenOrOdd(c, d) == "Odd") or (isEvenOrOdd(a, b) == "Odd" and isEvenOrOdd(c, d) == "Even"): print(-1) else: times = -1 while n <= 100: if (a*n - d + b) % c == 0 and (a*n - d + b) % c >= 0 and b + a*n >= b and d + ((a*n - d + b) // c)*c >= d: times = b + a*n break else: n += 1 print(times)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (((b & 1) && !(a & 1) && !(d & 1) && !(c & 1)) || (!(b & 1) && !(a & 1) && (d & 1) && !(c & 1))) { cout << -1 << endl; return 0; } long long l1 = b, l2 = d; while (l1 != l2 && l1 < 1e8) { if (l1 < l2) l1 += a; else l2 += c; } if (l1 < 1e8) cout << l1 << endl; else cout << -1 << endl; return 0; }
7
CPP
#include <bits/stdc++.h> int main() { int a, b, c, d, i, j, time1, time2, flag; scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); for (i = 0; i <= 100; i++) { time1 = a * i + b; for (j = 0; j <= 100; j++) { time2 = c * j + d; if (time1 == time2) { flag = time1; break; } } if (time1 == time2) break; else flag = -1; } printf("%d\n", flag); return 0; }
7
CPP
a, b = list(map(int, input().split())) c, d = list(map(int, input().split())) N = 100 for i in range(N + 1): v1 = b + i * a if v1 - d >= 0: r = (v1 - d) % c if r == 0: print(v1) exit() print(-1)
7
PYTHON3
n = input().split() a = int(n[0]) b = int(n[1]) m = input().split() c = int(m[0]) d = int(m[1]) for i in range(100): for j in range(100): if(a*i+b==c*j+d): print (b+a*i) exit() print ("-1")
7
PYTHON3
#------------------Important Modules------------------# from sys import stdin,stdout from bisect import bisect_left as bl from bisect import bisect_right as br from heapq import * from random import * input=stdin.readline prin=stdout.write from random import sample from collections import Counter,deque from fractions import * from math import sqrt,ceil,log2,gcd,cos,pi #dist=[0]*(n+1) mod=10**9+7 """ class DisjSet: def __init__(self, n): self.rank = [1] * n self.parent = [i for i in range(n)] def find(self, x): if (self.parent[x] != x): self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): xset = self.find(x) yset = self.find(y) if xset == yset: return if self.rank[xset] < self.rank[yset]: self.parent[xset] = yset elif self.rank[xset] > self.rank[yset]: self.parent[yset] = xset else: self.parent[yset] = xset self.rank[xset] = self.rank[xset] + 1 """ def f(arr,i,j,d,dist): if i==j: return nn=max(arr[i:j]) for tl in range(i,j): if arr[tl]==nn: dist[tl]=d #print(tl,dist[tl]) f(arr,i,tl,d+1,dist) f(arr,tl+1,j,d+1,dist) #return dist def ps(n): cp=0;lk=0;arr={} #print(n) #cc=0; while n%2==0: n=n//2 #arr.append(n);arr.append(2**(lk+1)) lk+=1 for ps in range(3,ceil(sqrt(n))+1,2): lk=0 cc=0 while n%ps==0: n=n//ps #cc=1 #arr.append(n);arr.append(ps**(lk+1)) lk+=1 if n!=1: #lk+=1 #arr[n]=1 #ans*=n; lk+=1 return False #return arr #print(arr) return True #count=0 #dp=[[0 for i in range(m)] for j in range(n)] #[int(x) for x in input().strip().split()] def gcd(x, y): while(y): x, y = y, x % y return x # Driver Code def factorials(n,r): #This calculates ncr mod 10**9+7 slr=n;dpr=r qlr=1;qs=1 mod=10**9+7 for ip in range(n-r+1,n+1): qlr=(qlr*ip)%mod for ij in range(1,r+1): qs=(qs*ij)%mod #print(qlr,qs) ans=(qlr*modInverse(qs))%mod return qlr def modInverse(b): qr=10**9+7 return pow(b, qr - 2,qr) #=============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func def power(arr): listrep = arr subsets = [] for i in range(2**len(listrep)): subset = [] for k in range(len(listrep)): if i & 1<<k: subset.append(listrep[k]) subsets.append(subset) return subsets def pda(n) : list=[];su=0 for i in range(1, int(sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : list.append(i) su+=i else : list.append(n//i);list.append(i) su+=i;su+=n//i # The list will be printed in reverse return su def dis(xa,ya,xb,yb): return sqrt((xa-xb)**2+(ya-yb)**2) #### END ITERATE RECURSION #### #=============================================================================================== #----------Input functions--------------------# def ii(): return int(input()) def ilist(): return [int(x) for x in input().strip().split()] def islist(): return list(map(str,input().split().rstrip())) def inp(): return input().strip() def google(test,ans): return "Case #"+str(test)+": "+str(ans); def overlap(x1,y1,x2,y2): if x2>y1: return y1-x2 if y1>y2: return y2-x2 return y1-x2; ###-------------------------CODE STARTS HERE--------------------------------########### def dist(x1,y1,x2,y2): return sqrt((x1-x2)**2+(y1-y2)**2) ######################################################################################### #t=int(input()) t=1 for p in range(t): #n,m=ilist() a,b=ilist() c,d=ilist() cc=0 ans=-1 for k in range(101): for p in range(101): if d+c*k==b+a*p: print(b+a*p) cc=1 break if cc==1: break if cc==0: print(-1)
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) res = -1 m = max(a, b, c, d) for i in range(0, m + 1): j = (a * i + b - d) // c jd = (a * i + b - d) % c if jd == 0 and a * i + b >= max(b, d): res = a * i + b break print(res)
7
PYTHON3
a,b = map(int,input().split()) c,d = map(int,input().split()) h =0 k = 0 while h<=100: if ((d+h*c-b)%a==0) and (d+h*c-b)>=0: k = 1 break h+=1 if k == 0: print('-1') else: print(d+h*c)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int ans = 0xfffff; for (int i = 0; i <= 1000; ++i) for (int j = 0; j <= 1000; ++j) if (b + a * i == d + c * j) ans = min(ans, b + a * i); if (ans == 0xfffff) puts("-1"); else cout << ans << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace ::std; int a, b, c, d; int ExtendGcd(int a, int b, int &x, int &y) { int d = a; if (b != 0) { d = ExtendGcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } void solve() { scanf("%d%d%d%d", &a, &b, &c, &d); int x, y; int g = ExtendGcd(a, c, x, y); if ((d - b) % g) { puts("-1"); return; } x *= (d - b) / g; int mod = c / g; x = (x % mod + mod) % mod; while (a * x + b < d) { x += mod; } printf("%d\n", a * x + b); } int main() { solve(); return 0; }
7
CPP
I = lambda: map(int, input().split()) a,b = I() c,d = I() for p in range(d, 10**5, c): if p in range(b, 10**5, a): print(p) break else: print(-1)
7
PYTHON3
def function(a, b, c, d): l1=[] l2=[] for i in range(1, 1000): eq1=b+(a*i)-a eq2=d+(c*i)-c if eq1 not in l1: l1.append(eq1) if eq2 not in l2: l2.append(eq2) if eq1 in l2: return eq1 if eq2 in l1: return eq2 #print(eq1) #print(eq2) return -1 if __name__=="__main__": a, b=map(int, input().rstrip().split()) c, d=map(int, input().rstrip().split()) print(function(a, b, c, d))
7
PYTHON3
import sys input = sys.stdin.readline a, b = map(int, input().split()) c, d = map(int, input().split()) for i in range(110): for j in range(110): if b + i * a == d + j * c: print(b + i * a) sys.exit(0) print(-1)
7
PYTHON3
def rickAndMorty(): a, b = map(int, input().split()) c, d = map(int, input().split()) rickStep = 0 mortyStep = 0 rick = b morty = d loop = 0 while(True): loop += 1 if(loop == 10000): print(-1) return if(rick == morty): print(rick) return if(rick > morty): morty = d + mortyStep * c mortyStep += 1 elif(rick < morty): rick = b + rickStep * a rickStep += 1 rickAndMorty()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a, b; long long c, d; int main() { while (cin >> a >> b) { cin >> c >> d; for (int i = 0; i <= 1000; ++i) { for (int j = 0; j <= 1000; ++j) { if ((b + a * i) == (d + c * j)) { cout << b + a * i << endl; return 0; } } } cout << "-1" << endl; } }
7
CPP
#include <bits/stdc++.h> using namespace std; map<long long, int> pr; int a, b, c, d; int main() { scanf("%d %d", &a, &b); scanf("%d %d", &c, &d); for (int i = 0; i < 200000; ++i) { pr[b + a * i]++; pr[d + c * i]++; if (pr[d + c * i] == 2 || pr[b + a * i] == 2) { printf("%d", pr[d + c * i] == 2 ? d + c * i : b + a * i); return 0; } } printf("-1"); return 0; }
7
CPP
a, b = map(int, input().split()) c, d = map(int, input().split()) 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(b + a*x) break y += 1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a, b, c, d; int main(void) { cin >> a >> b >> c >> d; for (int i = 0; i < 1000000; i++) { if (b < d) b += a; else if (d < b) d += c; else { cout << b << endl; return 0; } } cout << -1 << endl; return 0; }
7
CPP
rick_a, rick_b = map(int, input().split()) morty_a, morty_b = map(int, input().split()) _arr_rick = [] _arr_morty = [] _arr_rick.append(rick_b) _arr_morty.append(morty_b) _res = False for x in range(1, 101): _rick_cord = rick_b + x * rick_a _morty_cord = morty_b + x * morty_a _arr_rick.append(_rick_cord) _arr_morty.append(_morty_cord) for x in _arr_rick: for y in _arr_morty: if x == y: print(x) _res = True break if _res: break if not(_res): print(-1)
7
PYTHON3
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(5000): 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 a, b, c, d, a1[100001], b1[100001]; string second, s1; int main() { cin >> a >> c >> b >> d; a1[1] = c; b1[1] = d; for (int i = 2; i <= 100000; i++) a1[i] = c + i * a - a; for (int i = 2; i <= 100000; i++) { b1[i] = d + i * b - b; } for (int i = 1; i <= 1000; i++) { for (int j = 1; j <= 1000; j++) if (!(a1[j] - b1[i])) { cout << a1[j]; return 0; } } cout << -1; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; cin >> a >> b; cin >> c >> d; int k1, k2; bool f = 0; for (k1 = 0; k1 < 1000; k1++) { for (k2 = 0; k2 < 1000; k2++) { if (b + k1 * a == d + k2 * c) { f = 1; break; } } if (f) break; } if (f) { cout << b + k1 * a; } else cout << "-1\n"; }
7
CPP
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)
7
PYTHON3
import math #t=int(input()) #for i in range(t): a,b = map(int, input().strip().split(' ')) c,d = map(int, input().strip().split(' ')) f=0 k=0 if a%2==0 and b%2==0 and c%2==0 and d%2!=0: print(-1) elif c%2==0 and d%2==0 and a%2==0 and b%2!=0: print(-1) else: for i in range(0,101): for j in range(0,101): k=i*a - j*c if k==d-b: f=1 print(b+(i)*a) break if f==1: break if f==0: print(-1) #n=int(input()) #lst = list(map(int, input().strip().split(' ')))
7
PYTHON3
a, b = map(int, input().split(' ')) c, d = map(int, input().split(' ')) if d < b: x = b; b = d; d = x x = a; a = c; c = x k = 1; r = -1 while k < 100000: n = (d-b+c*(k-1))/a+1 if n == int(n): n = int(n); r = b+a*(n-1); break k += 1 print(r)
7
PYTHON3
a,b=[int(s) for s in input().split()] c,d=[int(s) for s in input().split()] s=[] for x in range(101): for y in range(101): l=b+x*a m=d+y*c if l==m: s.append(l) if len(s)==0: print(-1) else: print(s[0])
7
PYTHON3
a,b=map(int,input().split()) c,d=map(int,input().split()) t=0 for i in range(1000): m=(b-d+a*i)/c u=m-int(m) if u==0 and m>=0: print(d+c*int(m)) t=1 break if t==0: print("-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[10000]; int main() { scanf("%d%d%d%d", &a, &b, &c, &d); for (int i = b; i < 10000; i += a) { visited[i] = 1; } for (int i = d; i < 10000; i += c) { if (visited[i]) { printf("%d", i); return 0; } } printf("-1"); return 0; }
7
CPP
#include <bits/stdc++.h> int main() { int a, b, c, d; int sum1, sum2; while (~scanf("%d%d%d%d", &a, &b, &c, &d)) { int flag = 0; sum1 = 0; sum2 = 0; for (int i = 0; i <= 100; i++) { sum1 = b + i * a; for (int j = 0; j <= 100; j++) { sum2 = d + c * j; if (sum1 == sum2) { flag = 1; printf("%d\n", sum1); break; } } if (flag == 1) break; } if (flag == 0) printf("-1\n"); } return 0; }
7
CPP
# your code goes here 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
a,b=[int(f)for f in input().split()] c,d=[int(f)for f in input().split()] t=True def findy(x): return ((b-d)+a*x)/c for n in range(1000): if findy(n)==int(findy(n)) and findy(n)>=0: print(b+a*n) t=False break if t:print(-1) #print(findy(99))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { set<int> s; int a, b, c, d; cin >> a >> b >> c >> d; int mx = max(b * a, d * c); for (int i = 0; i <= mx; i++) s.insert(b + i * a); for (int i = 0; i <= mx; i++) if (s.find(d + c * i) != s.end()) { cout << d + c * i; return 0; } cout << -1; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ; int a, b, c, d; cin >> a >> b >> c >> d; while (b != d && b < 1e6) { (b < d ? b += a : d += c); } cout << (b == d ? b : -1); return 0; }
7
CPP
# # Yet I'm feeling like # There is no better place than right by your side # I had a little taste # And I'll only spoil the party anyway # 'Cause all the girls are looking fine # But you're the only one on my mind import sys # import re # inf = float("inf") # sys.setrecursionlimit(1000000) # abc='abcdefghijklmnopqrstuvwxyz' # abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod,MOD=1000000007,998244353 # vow=['a','e','i','o','u'] # dx,dy=[-1,1,0,0],[0,0,1,-1] # from collections import deque, Counter, OrderedDict,defaultdict # from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace # from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd,log10,atan,tan # from bisect import bisect_left,bisect_right # import numpy as np def get_array(): return list(map(int , sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() def extended_euclid(a,b): if a==0: return b,0,1 else: gcd,x,y=extended_euclid(b%a,a) return gcd,y-(b//a)*x,x a,b=get_ints() c,d=get_ints() # a,b=get_ints() gcd,x,y=extended_euclid(a,-c) # print(gcd,x,y) # x-=1;y-=1 if (d-b)%gcd!=0: print(-1) exit() while b!=d: if b<d: b+=a else: d+=c print(b) # print(f'{a}x{x} + {b}x{y} = {gcd}')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long M = 1000000007; int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { int a, b; cin >> a >> b; int c, d, ans = -1; cin >> c >> d; map<int, int> ump; for (int i = 0; i < 10000; i++) { ump[b + a * i] = 1; } for (int i = 0; i < 10000; i++) { if (ump[d + c * i] == 1) { ans = d + c * i; break; } } cout << ans << endl; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a = 0, b = 0, c = 0, d = 0; cin >> a >> b >> c >> d; if (b == d) { cout << b; return (0); } long long curr1 = b, curr2 = d; bool flag = false; for (int i = 0; i < 1e3; i++) { curr2 = d; for (int j = 0; curr2 <= curr1; j++) { if (curr2 == curr1) { flag = true; cout << curr1; return (0); } curr2 += c; } curr1 += a; } if (!flag) cout << "-1"; return (0); }
7
CPP
a, b = [int(x) for x in input().split()] c, d = [int(x) for x in input().split()] k = -1 for i in range(max(b, d), a * c + max(b, d)): if (i - b) % a == 0 and (i - d) % c == 0: k = i break print(k)
7
PYTHON3
#include <bits/stdc++.h> int main() { int a; int b; int c; int d; int i; int j; int time = -1; scanf("%d %d %d %d", &a, &b, &c, &d); for (i = 0; i < 101; i++) { for (j = 0; j < 101; j++) { if ((b + i * a) == (d + j * c) & (time == -1)) time = (b + i * a); } } printf("%d\n", time); return 0; }
7
CPP
b,a = map(int,input().split()) d,c = map(int,input().split()) ans = -1 ar = [a+b*i for i in range(200)] br = [c+d*i for i in range(200)] # print(*ar) # print(*br) i = 0 j = 0 while i<200 and j<200: if ar[i]==br[j]: ans = ar[i] break elif ar[i]<br[j]: i+=1 else: j+=1 print(ans)
7
PYTHON3
#include <bits/stdc++.h> int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); int tmp; if (b < d) { tmp = d; d = b; b = tmp; tmp = a; a = c; c = tmp; } int i; for (i = 0; i <= c && (b - d + i * a) % c; i++) { } if (i == c + 1) printf("-1\n"); else { int re = b + i * a; printf("%d\n", re); } return 0; }
7
CPP
a,b=map(int,input().split()) c,d=map(int,input().split()) k=1 j=1 l=[] l1=0 while k<=100: r1=((b-a+(a*k))) while j<=100: r2=(d-c+(c*j)) if r1==r2: l.append(r2) break else: j=j+1 j=1 if len(l)>0: break k=k+1 if len(l)>0: print(l[0]) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; while (b != d && b < 100000) { if (b < d) b += a; if (d < b) d += c; } if (b != d) cout << -1; else cout << b; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, k = 0; cin >> a >> b >> c >> d; while (k <= 1e7) { if (b == d) { cout << b; return 0; } if (d > b) b += a; else d += c; k++; } cout << -1; return 0; }
7
CPP
a, b = map(int, input().split()) c, d = map(int, input().split()) maxloop = 100 foundFlag = False for i in range(0, maxloop): for j in range(0, maxloop): if ( (b + a*i) == (d + c*j) ): print(b + a*i) foundFlag = True break if ((b + a * i) == (d + c * j)): break if (foundFlag == False): print(-1)
7
PYTHON3
inp1 = input().split() a=int(inp1[0]) b=int(inp1[1]) inp2 = input().split() c=int(inp2[0]) d=int(inp2[1]) def pgcd(a,b) : while a%b != 0 : a, b = b, a%b return b x=0 y=0 t=0 pg = pgcd(a,c) if abs(d-b) % pg == 0: #(si pgcd(a,c) multiple de d-b alors on a une solution #Une solution existe while 1:#les deux ne crient pas en meme temps on augmente x si ax+b sinon on augmente y if a*x+b == c*y+d: break #on découvre un point possible if a*x+b < c*y+d: x+=1 if a*x+b > c*y+d: y+=1 t= a*x+b print(t) else: print(-1)
7
PYTHON3
def gcd(a, b): return a if b==0 else gcd(b, a%b) a, b = map(int, input().split()) c, d = map(int, input().split()) g = gcd(a, c) dif = abs(b-d) if dif%g==0: for t in range(max(b, d), 1000000): if (t-b)%a==0 and (t-d)%c==0: print(t) break else: print(-1)
7
PYTHON3
import math a, b = map( int, input().split() ) c, d = map( int, input().split() ) if abs( b - d ) % math.gcd( a, c ) != 0 : exit( print( -1 ) ) while b != d : if b < d : b += a else : d += c print( b )
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d; cin >> a >> b >> c >> d; long long int e = (c - a) + (b - d); if (b == d) cout << b; else { for (long long int i = 0; i < 1005; i++) { for (long long int j = 0; j < 1005; j++) { if (i == 0 || j == 0) continue; if (i * c - j * a == e) { cout << b + (j - 1) * a; return 0; } } } cout << "-1"; } }
7
CPP
#include <bits/stdc++.h> using namespace std; const long long inf = (1LL << 60) - 1; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; void solve() { long long a, b, c, d, x; cin >> a >> b >> c >> d; map<long long, long long> mp; for (long long i = 0; i < 101; i++) mp[b + i * a]++; bool flag = 0; for (long long i = 0; i < 101; i++) { x = d + i * c; if (mp[x]) { flag = 1; break; } } if (flag) cout << x << "\n"; else cout << -1 << "\n"; } int main() { solve(); return 0; }
7
CPP
import sys def solve(): a, b = map(int, input().split()) c, d = map(int, input().split()) if b < d: a, c = c, a b, d = d, b for i in range(c): if (b - d + i*a) % c == 0: ans = b + i*a print(ans) return print(-1) if __name__ == '__main__': solve()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long MOD9 = 1e9 + 7; const long long MAX = 1e7 + 10; const double eps = 0.99; int dx[4] = {0, 0, 1, -1}; int dy[4] = {-1, 1, 0, 0}; int t, n, m, q, k; bool s[MAX]; int main() { ios_base::sync_with_stdio(false); long long a, b, c, d; cin >> a >> b >> c >> d; for (long long i = 0; i < 100001; i++) s[b + i * a] = 1; for (long long i = 0; i < 100001; i++) if (s[d + i * c]) return cout << d + i * c, 0; cout << -1; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int a, b, c, d; cin >> a >> b; cin >> c >> d; double term; for (int i = 0; i <= 100; i++) { term = (b + a * i - d) * 1.0 / c; if (term >= 0 && term == (int)term) { cout << b + a * i << "\n"; return 0; } } cout << -1 << "\n"; return 0; }
7
CPP
def form(a,b,c,d): n = 1000 for i in range(0,n+1): for j in range(0,n+1): if (b + a*i) == (d+c*j): return b+a*i break return -1 a,b = map(int, input().split()) c,d = map(int, input().split()) print(form(a,b,c,d))
7
PYTHON3
a, b = map(int, input().split()) c, d = map(int, input().split()) if b > d: b, d = d, b a, c = c, a d -= b for i in range(1000000): if (c * i + d) % a == 0: print(i * c + d + b) break else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; int morty = b, rick = d; int ans = -1; for (int i = 0; i < 1000; i++) { if (morty < rick) morty += a; if (rick < morty) rick += c; if (rick == morty) { ans = rick; break; } } cout << ans << endl; }
7
CPP
a,b=map(int,input().split()) c,d=map(int,input().split()) arr=[0]*1000000 flag=0 for i in range(100): arr[a*i+b]=1 for j in range(100): if arr[c*j+d]==1: flag=1 break if flag==0: print("-1") else: print(c*j+d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a, b, c, d; const int maxn = 10000000; long long m[maxn], n[maxn]; int main() { cin >> a >> b; cin >> c >> d; for (int i = 0; i < maxn; i++) { m[i] = b + i * a; n[i] = d + i * c; } int i = 0, j = 0; while (1) { while (m[i] < n[j]) { i++; } if (i >= maxn) break; while (n[j] < m[i]) j++; if (j >= maxn) break; if (m[i] == n[j]) { cout << m[i] << endl; return 0; } } cout << -1 << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, ans1, ans2, i, j, a, b, c, d; cin >> a >> b >> c >> d; for (i = 0; i < 1000; i++) { for (j = 0; j < 1000; j++) { ans1 = b + i * a; ans2 = d + j * c; if (ans1 == ans2) { cout << ans1, 0; return 0; } } } cout << -1 << endl; }
7
CPP
def ext_gcd(a, b): if a == 0: return b, 0, 1 else: g, x, y = ext_gcd(b % a, a) return g, y - (b // a) * x, x a, b = map(int, input().split(' ')) c, d = map(int, input().split(' ')) min = -1 for i in range(120): if (b + i*a) % c == d % c and (b + i*a) >= d: if min == -1 or min > b + i*a: min = b + i*a if (d + i*c) % a == b % a and (d + i*c) >= b: if min == -1 or min > d + i*c: min = d + i*c print(min)
7
PYTHON3