text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int fn[3], fm[3]; memset(fn, 0, sizeof(fn)); memset(fm, 0, sizeof(fm)); while (n != 1) { if (n % 2 == 0) { fn[0]++; n /= 2; } else if (n % 3 == 0) { fn[1]++; n /= 3; } else if (n % 5 == 0) { fn[2]++; n /= 5; } else break; } while (m != 1) { if (m % 2 == 0) { fm[0]++; m /= 2; } else if (m % 3 == 0) { fm[1]++; m /= 3; } else if (m % 5 == 0) { fm[2]++; m /= 5; } else break; } if (n != m) cout << "-1"; else { cout << abs(fn[0] - fm[0]) + abs(fn[1] - fm[1]) + abs(fn[2] - fm[2]); } }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { long long int a, b, count = 0; int ans = 0; cin >> a >> b; if (a == b) ans = 0; else { long long int hcf = 0; if (a > b) hcf = gcd(a, b); else hcf = gcd(b, a); long long int x = a / hcf, y = b / hcf; if ((x != 1 && x % 2 != 0 && x % 3 != 0 && x % 5 != 0) || (y != 1 && y % 2 != 0 && y % 3 != 0 && y % 5 != 0)) { ans = -1; } else { int found = 0, q = 0; while (x != 1 && !found) { if (x % 2 == 0) { x /= 2; count++; } else if (x % 3 == 0) { x /= 3; count++; } else if (x % 5 == 0) { x /= 5; count++; } else found = 1; } while (y != 1 && !found) { if (y % 2 == 0) { y /= 2; count++; } else if (y % 3 == 0) { y /= 3; count++; } else if (y % 5 == 0) { y /= 5; count++; } else found = 1; } if (!found) ans = count; else ans = -1; } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long a, b, counta2 = 0, counta3 = 0, counta5 = 0, countb2 = 0, countb3 = 0, countb5 = 0; cin >> a >> b; while (a % 2 == 0) { a = a / 2; counta2++; } while (a % 3 == 0) { a = a / 3; counta3++; } while (a % 5 == 0) { a = a / 5; counta5++; } while (b % 2 == 0) { b = b / 2; countb2++; } while (b % 3 == 0) { b = b / 3; countb3++; } while (b % 5 == 0) { b = b / 5; countb5++; } if (a != b) { cout << "-1" << '\n'; } else { long long ans = abs(counta2 - countb2) + abs(counta3 - countb3) + abs(counta5 - countb5); cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int trim(int n, int s[]) { int v[3] = {2, 3, 5}; for (int i = 0; i < 3; i++) { s[i] = 0; while (n % v[i] == 0) { n /= v[i]; s[i]++; } } return n; } int main() { int a, b; int s1[3], s2[3]; while (2 == scanf("%d%d", &a, &b)) { if (a == b) { printf("0\n"); continue; } a = trim(a, s1); b = trim(b, s2); if ((a > 1 || b > 1) && a != b) { printf("-1\n"); continue; } int result = 0; for (int i = 0; i < 3; i++) { result += max(s1[i], s2[i]) - min(s1[i], s2[i]); } printf("%d\n", result); } return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } int calc(int a, int b) { int g = gcd(a, b); long long c = (long long)(a / g) * (b / g); int cnt = 0; for (int i : vector<int>{2, 3, 5}) while (c % i == 0) { c /= i; ++cnt; } return (c == 1 ? cnt : -1); } int main() { int a, b; cin >> a >> b; cout << calc(a, b) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a, b; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { cin >> a >> b; int gcdab = gcd(a, b); a /= gcdab; b /= gcdab; int ans = 0; while (a % 2 == 0) a /= 2, ans++; while (a % 3 == 0) a /= 3, ans++; while (a % 5 == 0) a /= 5, ans++; while (b % 2 == 0) b /= 2, ans++; while (b % 3 == 0) b /= 3, ans++; while (b % 5 == 0) b /= 5, ans++; if (a == b) cout << ans << endl; else cout << "-1" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int a, b; cin >> a >> b; if (a == b) { cout << 0; return 0; } long long int f[3] = {2, 3, 5}; long long int af[6] = {}; long long int bf[6] = {}; while (a != 1) { bool once = false; for (int i = 0; i < 3; i++) { if (a % f[i] == 0) { af[f[i]]++; a /= f[i]; once = true; } } if (!once) break; } while (b != 1) { bool once = false; for (int i = 0; i < 3; i++) { if (b % f[i] == 0) { once = true; bf[f[i]]++; b /= f[i]; } } if (!once) break; } if (a != b) { cout << -1; return 0; } long long int chances = 0; for (int i = 0; i < 3; i++) { chances += abs(af[f[i]] - bf[f[i]]); } cout << chances; }
#include <bits/stdc++.h> const double pi = acos(-1.0); using namespace std; void solve() { long long int a, b; cin >> a >> b; if (a == b) { cout << 0 << endl; return; } map<int, int> mp, mp1; int d[3] = {2, 3, 5}; for (int i = 0; i < 3; i++) { if (a % d[i] == 0) { int c = 0; while (a % d[i] == 0) { c++; a /= d[i]; } mp[d[i]] = c; } } for (int i = 0; i < 3; i++) { if (b % d[i] == 0) { int c = 0; while (b % d[i] == 0) { c++; b /= d[i]; } mp1[d[i]] = c; } } if (a != b) { cout << -1 << endl; return; } long long int ans = 0; for (int i = 0; i < 3; i++) { ans += abs(mp[d[i]] - mp1[d[i]]); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long int mod = (long long int)(1e9) + 7LL; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a, b; cin >> a >> b; int a2 = 0, a3 = 0, a5 = 0, b2 = 0, b3 = 0, b5 = 0; while (a % 2 == 0) { a /= 2; a2++; } while (a % 3 == 0) { a /= 3; a3++; } while (a % 5 == 0) { a /= 5; a5++; } while (b % 2 == 0) { b /= 2; b2++; } while (b % 3 == 0) { b /= 3; b3++; } while (b % 5 == 0) { b /= 5; b5++; } if (a != b) cout << "-1"; else cout << abs(a5 - b5) + abs(a2 - b2) + abs(a3 - b3); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int a, b; cin >> a >> b; long long int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0; while (a % 2 == 0) { a = a / 2; c1++; } while (b % 2 == 0) { b = b / 2; c4++; } while (a % 3 == 0) { a = a / 3; c2++; } while (b % 3 == 0) { b = b / 3; c5++; } while (a % 5 == 0) { a = a / 5; c3++; } while (b % 5 == 0) { b = b / 5; c6++; } if (a == b) { cout << abs(c1 - c4) + abs(c2 - c5) + abs(c3 - c6); } else { cout << -1; } }
#include <bits/stdc++.h> using namespace std; void solve(void); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); cout << "\n"; } } void solve() { long long a, b; cin >> a >> b; int x[3] = {0, 0, 0}; int y[3] = {0, 0, 0}; int flag = 1; while (a > 1) { if (a % 2 == 0) x[0]++, a = a / 2; if (a % 3 == 0) x[1]++, a = a / 3; if (a % 5 == 0) x[2]++, a = a / 5; if (a % 2 != 0 and a % 3 != 0 and a % 5 != 0) { flag = -1; break; } } while (b > 1) { if (b % 2 == 0) y[0]++, b = b / 2; if (b % 3 == 0) y[1]++, b = b / 3; if (b % 5 == 0) y[2]++, b = b / 5; if (b % 2 != 0 and b % 3 != 0 and b % 5 != 0) { flag = -1; break; } } if (flag == -1 and a != b) { cout << -1; return; } else { cout << abs(x[1] - y[1]) + abs(y[2] - x[2]) + abs(x[0] - y[0]); } }
#include <bits/stdc++.h> int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); } using namespace std; int main() { int n, k; cin >> n >> k; if (n == k) { cout << 0 << endl; return 0; } int d = gcd(n, k); int a = n / d; int b = k / d; if (a % 2 != 0 && a % 3 != 0 && a % 5 != 0 && a != 1) { cout << -1 << endl; return 0; } if (b % 2 != 0 && b % 3 != 0 && b % 5 != 0 && b != 1) { cout << -1 << endl; return 0; } int ans = 0; while (a != 1) { if (a % 2 == 0) { ans++; a /= 2; } else if (a % 3 == 0) { ans++; a /= 3; } else if (a % 5 == 0) { ans++; a /= 5; } else { printf("-1\n"); return 0; } } while (b != 1) { if (b % 2 == 0) { ans++; b /= 2; } else if (b % 3 == 0) { ans++; b /= 3; } else if (b % 5 == 0) { ans++; b /= 5; } else { printf("-1\n"); return 0; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int N = int(1e5) + 10; int a, b, ans; int main() { scanf("%d%d", &a, &b); int primes[] = {2, 3, 5}; for (int p : primes) { int x = 0, y = 0; while (a % p == 0) a /= p, x++; while (b % p == 0) b /= p, y++; ans += abs(x - y); } if (a != b) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { long long gcd; if (a == 1) return 1; else if (b == 1) return 1; while (b) { long long t = b; b = a % b; a = t; } gcd = a; return gcd; } long long check(long long &n) { long long ck = 0; for (long long i = 2;;) { if (n % i == 0) { n /= i; ck++; } else break; } for (long long i = 3;;) { if (n % i == 0) { n /= i; ck++; } else break; } for (long long i = 5;;) { if (n % i == 0) { n /= i; ck++; } else break; } return ck; } int main() { long long a, b; cin >> a >> b; if (a == b) { cout << "0"; return 0; } long long x = gcd(a, b); a /= x; b /= x; long long sum1, sum2; sum1 = check(a); sum2 = check(b); if (a == 1 && b == 1) cout << sum1 + sum2 << endl; else cout << "-1" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std; long long b[100010]; int main() { long long a, b; cin >> a >> b; long long cnt2 = 0, cnt3 = 0, cnt5 = 0; while (a % 2 == 0) { cnt2++; a /= 2; } while (a % 3 == 0) { cnt3++; a /= 3; } while (a % 5 == 0) { cnt5++; a /= 5; } long long c2 = 0, c3 = 0, c5 = 0; while (b % 2 == 0) c2++, b /= 2; while (b % 3 == 0) c3++, b /= 3; while (b % 5 == 0) c5++, b /= 5; if (a != b) { cout << -1 << endl; return 0; } cout << abs(cnt2 - c2) + abs(cnt3 - c3) + abs(cnt5 - c5) << endl; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long l, long long s) { if (!(l % s)) return s; else return gcd(s, l % s); } long long abse(long long a, long long b) { if (a > b) return a - b; return b - a; } long long check(long long a, vector<long long>& v) { while (a % 2 == 0) { a /= 2; v[0]++; } while (a % 3 == 0) { a /= 3; v[1]++; } while (a % 5 == 0) { a /= 5; v[2]++; } if (a == 1) return 0; return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1, cas; for (cas = 1; cas <= T; cas++) { long long s, l; cin >> s >> l; if (s == l) { cout << 0; return 0; } if (s > l) swap(s, l); long long GCD = gcd(l, s); vector<long long> vss(3, 0), vll(3, 0); l /= GCD; s /= GCD; if (check(s, vss) || check(l, vll)) { cout << -1; return 0; } int cnt = abse(vss[0], vll[0]) + abse(vss[1], vll[1]) + abse(vss[2], vll[2]); cout << cnt; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; int A[10], B[10]; const int INF = 1e9 + 9; long long int a, b; void solve() { while (a % 2 == 0) { a /= 2; A[2]++; } while (a % 3 == 0) { a /= 3; A[3]++; } while (a % 5 == 0) { a /= 5; A[5]++; } while (b % 2 == 0) { b /= 2; B[2]++; } while (b % 3 == 0) { b /= 3; B[3]++; } while (b % 5 == 0) { b /= 5; B[5]++; } } int main() { scanf("%lld", &a); scanf("%lld", &b); solve(); if (a != b) { printf("-1"); return 0; } long long int cnt = 0; cnt += A[5] + B[5] - 2 * min(A[5], B[5]); cnt += A[3] + B[3] - 2 * min(A[3], B[3]); cnt += A[2] + B[2] - 2 * min(A[2], B[2]); printf("%lld", cnt); return 0; }
#include <bits/stdc++.h> using namespace std; int a, b, c, x, y, q; int main() { cin >> x >> y; a = x; b = y; while (b) { c = a % b; a = b; b = c; } x /= a; y /= a; while (x % 2 == 0) { x /= 2; q++; } while (x % 3 == 0) { x /= 3; q++; } while (x % 5 == 0) { x /= 5; q++; } while (y % 2 == 0) { y /= 2; q++; } while (y % 3 == 0) { y /= 3; q++; } while (y % 5 == 0) { y /= 5; q++; } if (x == y) cout << q; else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> p1, p2; void factorize(vector<int> &ar, int n) { int cnt = 0; while (n % 2 == 0) { cnt++; n /= 2; } ar.push_back(cnt); cnt = 0; while (n % 3 == 0) { cnt++; n /= 3; } ar.push_back(cnt); cnt = 0; while (n % 5 == 0) { cnt++; n /= 5; } ar.push_back(cnt); ar.push_back(n); } int main() { int a, b; cin >> a >> b; factorize(p1, a); factorize(p2, b); if (p1.back() != p2.back()) { cout << -1 << endl; return 0; } int cnt = 0; for (int i = 0; i < 4; i++) { cnt += abs(p1[i] - p2[i]); } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAX = 9999999; const long long modu = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n, m; cin >> n >> m; long long i, j, k; i = j = k = 0; while (n % 2 == 0) { n /= 2; i++; } while (n % 3 == 0) { n /= 3; j++; } while (n % 5 == 0) { n /= 5; k++; } long long i1, j1, k1; i1 = j1 = k1 = 0; while (m % 2 == 0) { m /= 2; i1++; } while (m % 3 == 0) { m /= 3; j1++; } while (m % 5 == 0) { m /= 5; k1++; } if (n != m) cout << -1; else { long long ans = abs(i - i1) + abs(j - j1) + abs(k - k1); cout << ans; } }
#include <bits/stdc++.h> using namespace std; int a[2]; int f[2][3]; void cal(int i) { while (a[i] % 2 == 0) { f[i][0]++; a[i] /= 2; } while (a[i] % 3 == 0) { f[i][1]++; a[i] /= 3; } while (a[i] % 5 == 0) { f[i][2]++; a[i] /= 5; } } int main() { scanf("%d %d", &a[0], &a[1]); if (a[0] == a[1]) { printf("0\n"); } else { cal(0); cal(1); if (a[0] != a[1]) { printf("-1\n"); } else { int ret = 0; for (int i = 0; i < 3; ++i) { ret += abs(f[0][i] - f[1][i]); } printf("%d\n", ret); } } return 0; }
#include <bits/stdc++.h> using namespace std; std::map<std::pair<int, int>, int> saved; int go(int a, int b) { if (a == b) return 0; if (a == 0 || b == 0) return INT_MAX; if (b > a) std::swap(a, b); int retVal = 0; std::map<std::pair<int, int>, int>::iterator it = saved.find(std::make_pair(a, b)); if (it == saved.end()) { int a1 = a % 2 ? INT_MAX : go(a / 2, b); int a2 = a % 3 ? INT_MAX : go(a / 3, b); int a3 = a % 5 ? INT_MAX : go(a / 5, b); retVal = min(a1, min(a2, a3)); } else { retVal = it->second; } saved[std::make_pair(a, b)] = retVal; if (retVal != INT_MAX) return retVal + 1; else return retVal; } int main() { int a = 0, b = 0; scanf("%d%d", &a, &b); int cnt = go(a, b); if (cnt == INT_MAX) printf("-1"); else printf("%d", cnt); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, n, two = 0, five = 0, three = 0, t = 0, f = 0, tt = 0, x, y, ans; scanf("%lld%lld", &a, &b); n = a; while (1) { if (n % 2 == 0) { n /= 2; two++; } else if (n % 3 == 0) { n /= 3; three++; } else if (n % 5 == 0) { n /= 5; five++; } else { x = n; break; } } n = b; while (1) { if (n % 2 == 0) { n /= 2; t++; } else if (n % 3 == 0) { n /= 3; tt++; } else if (n % 5 == 0) { n /= 5; f++; } else { y = n; break; } } if (x != y) { printf("-1\n"); } else { ans = abs(two - t) + abs(three - tt) + abs(five - f); printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; inline long long input() { long long n; cin >> n; return n; } long long pw(long long a, long long b) { return (!b ? 1 : (b & 1 ? a * pw(a * a, b / 2) : pw(a * a, b / 2))); } const int MAXN = 1e3 + 10; const int MOD = 1e9 + 7; const int MOD2 = 998244353; const long long INF = 8e18; int bmm(int a, int b) { if (a % b == 0) { return b; } return bmm(b, a % b); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int a, b; cin >> a >> b; int t = bmm(a, b), A = a / t, B = b / t; vector<int> v{2, 3, 5}; int cnt = 0; for (int i = 0; i < 3; i++) { while (A % v[i] == 0) { A /= v[i]; cnt++; } while (B % v[i] == 0) { B /= v[i]; cnt++; } } if (A != 1 || B != 1) { return cout << -1, 0; } cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e7 + 1; const int INF = 1e9 + 123; const int MOD = 1e9 + 7; const double EPS = 1e-9; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; void func(int &a, int &acnt2, int &acnt3, int &acnt5) { while (a % 2 == 0) { a /= 2; ++acnt2; } while (a % 3 == 0) { a /= 3; ++acnt3; } while (a % 5 == 0) { a /= 5; ++acnt5; } } int a, b, acnt2, acnt3, acnt5, bcnt2, bcnt3, bcnt5; int main() { ios_base ::sync_with_stdio(0); cin >> a >> b; func(a, acnt2, acnt3, acnt5); func(b, bcnt2, bcnt3, bcnt5); if (a == b) { cout << abs(bcnt2 - acnt2) + abs(bcnt3 - acnt3) + abs(bcnt5 - acnt5); } else { cout << -1; } return 0; }
#include <bits/stdc++.h> using namespace std; int op_cnt[6]; int main() { int a, b, s = 0; cin >> a >> b; for (int n = 2; n <= 5; n++) { while (a % n == 0) a /= n, op_cnt[n]++; while (b % n == 0) b /= n, op_cnt[n]--; s += abs(op_cnt[n]); } cout << (a == b ? s : -1); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int a2 = 0, a3 = 0, a5 = 0; int b2 = 0, b3 = 0, b5 = 0; while (a % 2 == 0) { ++a2; a /= 2; } while (a % 3 == 0) { ++a3; a /= 3; } while (a % 5 == 0) { ++a5; a /= 5; } while (b % 2 == 0) { ++b2; b /= 2; } while (b % 3 == 0) { ++b3; b /= 3; } while (b % 5 == 0) { ++b5; b /= 5; } if (a != b) { cout << "-1\n"; return 0; } int res = abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5); cout << res << '\n'; }
#include <bits/stdc++.h> using namespace std; int hcf(int a, int b) { if (a == 0) return b; if (b == 0) return a; while (b % a != 0) { int t = b % a; b = a; a = t; } return a; } int main() { int a, b; cin >> a >> b; if (a == b) { cout << 0 << "\n"; return 0; } else { if (a > b) { int t = a; a = b; b = t; } int x = hcf(a, b); int c = 0; a /= x; b /= x; while (a != 1) { if (a % 2 == 0) a /= 2; else if (a % 3 == 0) a /= 3; else if (a % 5 == 0) a /= 5; else { cout << -1 << "\n"; return 0; } c++; } while (b != 1) { if (b % 2 == 0) b /= 2; else if (b % 3 == 0) b /= 3; else if (b % 5 == 0) b /= 5; else { cout << -1 << "\n"; return 0; } c++; } cout << c << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int aa = a, bb = b; int t1, t2, k1, k2, c1, c2; t1 = t2 = k1 = k2 = c1 = c2 = 0; if (a != b) { while (a % 2 == 0) { a /= 2; t1++; } while (a % 3 == 0) { a /= 3; k1++; } while (a % 5 == 0) { a /= 5; c1++; } while (b % 2 == 0) { b /= 2; t2++; } while (b % 3 == 0) { b /= 3; k2++; } while (b % 5 == 0) { b /= 5; c2++; } if (a != b) { cout << "-1" << endl; } else { int ans = 0; if (t1 != t2) ans += abs(t1 - t2); if (k1 != k2) ans += abs(k1 - k2); if (c1 != c2) ans += abs(c1 - c2); cout << ans << endl; } } else cout << "0" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int ans = 0; int A[10], B[10]; void divv(int &a, int x) { while (a % x == 0) a /= x, A[x]++; } int main() { int a, b; cin >> a >> b; divv(a, 2), divv(a, 3), divv(a, 5); for (int i = 0; i < 6; i++) { B[i] = A[i], A[i] = 0; } divv(b, 2), divv(b, 3), divv(b, 5); for (int i = 0; i < 6; i++) { ans += abs(A[i] - B[i]); } int go = max(sqrt(a), sqrt(b)); for (int i = 1; i <= go; i++) { if ((a % i == 0 && b % i) || (a % i && b % i == 0)) { cout << -1; return 0; } else if (a % i == 0 && b % i == 0) { int p = a / i; int q = b / i; if ((a % p == 0 && b % p) || (a % q && b % q == 0)) { cout << -1; return 0; } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } long long gcd2(long long a, long long b, long long& x, long long& y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = gcd2(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } int mod = 1000000007; inline int add(int a, int b) { int ret = a + b; if (ret >= mod) ret -= mod; return ret; } bool find_any_solution(long long a, long long b, long long c, long long& x0, long long& y0, long long& g) { g = gcd2(abs(a), abs(b), x0, y0); if (c % g != 0) return false; x0 *= c / g; y0 *= c / g; if (a < 0) x0 *= -1; if (b < 0) y0 *= -1; return true; } void shift_solution(long long& x, long long& y, long long a, long long b, long long cnt) { x += cnt * b; y -= cnt * a; } const double pi = 3.14159265358979323846; int main() { ios::sync_with_stdio(false); cin.tie(0); int a, b, g, cnt = 0; cin >> a >> b; g = gcd(a, b); a /= g; b /= g; for (int p : {2, 3, 5}) { while (a % p == 0) { a /= p; cnt++; } } for (int p : {2, 3, 5}) { while (b % p == 0) { b /= p; cnt++; } } if (a != 1 || b != 1) cnt = -1; cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; int solve(int a, int b) { int ta = 0, tb = 0, tha = 0, thb = 0, fa = 0, fb = 0; while (a % 2 == 0) { a /= 2; ta++; } while (a % 3 == 0) { a /= 3; tha++; } while (a % 5 == 0) { a /= 5; fa++; } while (b % 2 == 0) { b /= 2; tb++; } while (b % 3 == 0) { b /= 3; thb++; } while (b % 5 == 0) { b /= 5; fb++; } if (a != b) return -1; return abs(ta - tb) + abs(tha - thb) + abs(fa - fb); } int main() { ios_base::sync_with_stdio(0); cin.tie(); int a, b; cin >> a >> b; int get = solve(a, b); cout << get << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimization("O3") const long long M = 10000000000; using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long a, b, ans = 0; cin >> a >> b; map<long long, long long> A, B; while (a != b) { if (b > a) { if (b % 2 == 0) { b /= 2; B[2]++; } else if (b % 3 == 0) { B[3]++; b /= 3; } else if (b % 5 == 0) { B[5]++; b /= 5; } else { cout << "-1"; return 0; } } else { if (a % 2 == 0) { A[2]++; a /= 2; } else if (a % 3 == 0) { A[3]++; a /= 3; } else if (a % 5 == 0) { A[5]++; a /= 5; } else { cout << "-1"; return 0; } } } cout << abs(A[5] - B[5]) + abs(A[2] - B[2]) + abs(A[3] - B[3]); return 0; }
#include <bits/stdc++.h> using namespace std; long long gl(long long a, long long b) { if (b == 0) return a; return gl(b, a % b); } int main() { bool f; long long a, b, t = 0; cin >> a >> b; long long g = gl(a, b); a = a / g; b = b / g; if (a == 1 && a == b) f = false; else f = true; while (f) { if (a % 5 == 0) a /= 5, t++; else if (a % 3 == 0) a /= 3, t++; else if (a % 2 == 0) a /= 2, t++; if (b % 5 == 0) b /= 5, t++; else if (b % 3 == 0) b /= 3, t++; else if (b % 2 == 0) b /= 2, t++; if (b % 5 != 0 && b % 3 != 0 && b % 2 != 0 && b != 1) { t = -1; break; } if (a % 5 != 0 && a % 3 != 0 && a % 2 != 0 && a != 1) { t = -1; break; } if (a == 1 && a == b) f = false; } cout << t << endl; }
#include <bits/stdc++.h> using namespace std; void compute(int a, map<int, set<int> >& m, int lvl = 0) { if (m.count(a)) { if (*m[a].begin() <= lvl) return; } m[a].insert(lvl); if (a == 1) return; if (a % 2 == 0) { compute(a / 2, m, lvl + 1); } if (a % 3 == 0) { compute(a / 3, m, lvl + 1); } if (a % 5 == 0) { compute(a / 5, m, lvl + 1); } } int main() { int a, b; cin >> a >> b; map<int, set<int> > m1; map<int, set<int> >::iterator iter; map<int, set<int> > m2; compute(a, m1); compute(b, m2); int result = 1000000000; for (iter = m1.begin(); iter != m1.end(); iter++) { if (m2.count(iter->first)) result = min(result, *iter->second.begin() + *m2[iter->first].begin()); } if (result == 1000000000) result = -1; cout << result << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1000 * 1000 * 1000 + 7; const int inf = 2 * 1000 * 1000 * 1000; const int maxn = 1000 * 1000; map<int, map<int, int> > was, d; int calc(int a, int b) { if (b < a) swap(a, b); if (was[a][b]) return d[a][b]; was[a][b] = true; d[a][b] = inf; if (a == b) return d[a][b] = 0; if (b % 2 == 0) d[a][b] = min(d[a][b], calc(a, b / 2) + 1); if (b % 3 == 0) d[a][b] = min(d[a][b], calc(a, b / 3) + 1); if (b % 5 == 0) d[a][b] = min(d[a][b], calc(a, b / 5) + 1); return d[a][b]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int a, b; cin >> a >> b; if (calc(a, b) != inf) cout << calc(a, b); else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> cntodd, cnteven; void solve() { long long a, b; cin >> a >> b; int compa[6] = {0, 0, 0, 0, 0, 0}; int compb[6] = {0, 0, 0, 0, 0, 0}; while (a % 2 == 0 or a % 5 == 0 or a % 3 == 0) { if (a % 2 == 0) compa[2]++, a /= 2; if (a % 3 == 0) compa[3]++, a /= 3; if (a % 5 == 0) compa[5]++, a /= 5; } while (b % 2 == 0 or b % 5 == 0 or b % 3 == 0) { if (b % 2 == 0) compb[2]++, b /= 2; if (b % 3 == 0) compb[3]++, b /= 3; if (b % 5 == 0) compb[5]++, b /= 5; } if (a != b) cout << -1 << "\n"; else cout << abs(compa[2] - compb[2]) + abs(compa[3] - compb[3]) + abs(compa[5] - compb[5]) << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; map<int, int> mp; int x[] = {2, 3, 5}; int c; c = a; for (int i = 0; i < 3; i++) { while (c % x[i] == 0) { mp[x[i]]++; c /= x[i]; } } mp[c]++; c = b; for (int i = 0; i < 3; i++) { while (c % x[i] == 0) { mp[x[i]]--; c /= x[i]; } } mp[c]--; int ans = 0, check = 0; for (auto it : mp) { if (it.first == 2 or it.first == 3 or it.first == 5) ans += (abs(it.second)); else if (it.second) check = 1; } cout << (check ? -1 : ans) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (a < b) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } int f(int a, int b) { int x = 0, y = 0, z = 0; while (a % 2 == 0) { a = a / 2; x++; } while (b % 2 == 0) { b = b / 2; x++; } while (a % 3 == 0) { a = a / 3; y++; } while (b % 3 == 0) { b = b / 3; y++; } while (a % 5 == 0) { a = a / 5; z++; } while (b % 5 == 0) { b = b / 5; z++; } if (a != 1 || b != 1) return -1; return x + y + z; } int main() { int n, k, y; cin >> n >> k; y = gcd(n, k); n /= y; k /= y; cout << f(n, k) << endl; return 0; }
#include <bits/stdc++.h> int init(int x, int y) { return (x > y) ? (x - y) : (y - x); } int main() { int a, b, i; int x[3] = {2, 3, 5}; int cnt_a[3], cnt_b[3]; while (scanf("%d%d", &a, &b) != EOF) { cnt_a[0] = cnt_a[1] = cnt_a[2] = 0; cnt_b[0] = cnt_b[1] = cnt_b[2] = 0; for (i = 0; i < 3; i++) { while (a % x[i] == 0) { cnt_a[i]++; a /= x[i]; } } for (i = 0; i < 3; i++) { while (b % x[i] == 0) { cnt_b[i]++; b /= x[i]; } } if (a != b && (a != 1 || b != 1)) printf("-1\n"); else { int ans = 0; for (i = 0; i < 3; i++) ans += init(cnt_a[i], cnt_b[i]); printf("%d\n", ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; map<int, int> factor(int number) { map<int, int> fc; for (int i = 2; i * i <= number; ++i) { while (number % i == 0) { fc[i]++; number = number / i; } } if (number != 1) { fc[number]++; } return fc; } bool isDevider(int n) { return n == 2 || n == 3 || n == 5; } int spow(int n, int p) { int res = 1; for (int i = 0; i < p; ++i) { res *= n; } return res; } int bpow(int a, int b) { if (b == 0) return 1; int t = bpow(a, b / 2); if (b & 1) return t * t * a; return t * t; } int toNumber(const map<int, int>& factors) { int res = 1; for (const auto& f : factors) { res *= spow(f.first, f.second); } return res; } int calc(int a, int b) { if (a == b) { return 0; } int res = 0; auto factorA = factor(a); auto factorB = factor(b); for (int i : vector<int>{2, 3, 5}) { res += abs(factorA[i] - factorB[i]); while (a % i == 0) a /= i; while (b % i == 0) b /= i; } return a == b ? res : -1; } int main() { int a, b; cin >> a >> b; cout << calc(a, b) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 2000; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; int a, b; vector<int> d = {2, 3, 5}; cin >> a >> b; vector<int> A(3), B(3); for (int i = 0; i < 3; i++) { int cp = 0; while (a % d[i] == 0) a /= d[i], cp++; A[i] = cp; cp = 0; while (b % d[i] == 0) b /= d[i], cp++; B[i] = cp; } if (a == b) { int ans = 0; for (int i = 0; i < 3; i++) { ans += (A[i] + B[i] - 2 * min(A[i], B[i])); } cout << ans << '\n'; } else cout << -1 << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int c(long long int n) { long long int c = 0; while (n % 2 == 0) { c++; n = n / 2; } while (n % 3 == 0) { c++; n = n / 3; } while (n % 5 == 0) { c++; n = n / 5; } if (n == 1) return c; else return -1; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int a, b, g; cin >> a >> b; g = gcd(a, b); a = a / g; b = b / g; if (c(a) < 0 || c(b) < 0) cout << -1; else cout << c(a) + c(b); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, B = 0, k = 0, l = 0, p = 0, n = 0, b = 0, t = 0, x = 0, y = 0; cin >> a >> B; while (a % 2 == 0) { k++; a /= 2; } while (a % 3 == 0) { l++; a /= 3; } while (a % 5 == 0) { p++; a /= 5; } while (B % 2 == 0) { n++; B /= 2; } while (B % 3 == 0) { b++; B /= 3; } while (B % 5 == 0) { t++; B /= 5; } x = a; y = B; if (x != y) { cout << "-1\n"; return 0; } else cout << (abs(k - n) + abs(b - l) + abs(p - t)) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; queue<pair<pair<int, int>, int> > q; set<pair<int, int> > s; int a, b; void input() { cin >> a >> b; } void evaluate() { q.push(make_pair(make_pair(a, b), 0)); while (!q.empty()) { if (s.find(q.front().first) == s.end()) { s.insert(q.front().first); pair<int, int> x = q.front().first; int y = q.front().second; q.pop(); if (x.first == x.second) { cout << y; exit(0); } y++; if (x.first % 2 == 0) { x.first /= 2; q.push(make_pair(x, y)); x.first *= 2; } if (x.first % 3 == 0) { x.first /= 3; q.push(make_pair(x, y)); x.first *= 3; } if (x.first % 5 == 0) { x.first /= 5; q.push(make_pair(x, y)); x.first *= 5; } if (x.second % 2 == 0) { x.second /= 2; q.push(make_pair(x, y)); x.second *= 2; } if (x.second % 3 == 0) { x.second /= 3; q.push(make_pair(x, y)); x.second *= 3; } if (x.second % 5 == 0) { x.second /= 5; q.push(make_pair(x, y)); x.second *= 5; } } else q.pop(); } } int main() { input(); evaluate(); cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long int N = 2e5 + 10; unordered_map<string, int> dp; string k(long long int a, long long int b) { string s = to_string(a); s += ","; s += to_string(b); return s; } long long int rec(long long int a, long long int b) { if (a == b) { return 0; } if (a < b) { long long int t = a; a = b; b = t; } if (dp.count(k(a, b)) != 0) { return dp[k(a, b)]; } long long int ans = INT_MAX; if (a % 2 == 0) { ans = min(rec(a / 2, b) + 1, ans); } if (a % 3 == 0) { ans = min(rec(a / 3, b) + 1, ans); } if (a % 5 == 0) { ans = min(rec(a / 5, b) + 1, ans); } return dp[k(a, b)] = ans; } void go() { long long int a, b; cin >> a >> b; long long int ans = rec(a, b); if (ans == INT_MAX) { cout << -1; return; } cout << ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long int t = 1; while (t--) go(); }
#include <bits/stdc++.h> using namespace std; int c[3], d[3]; int fj(int x) { while (x != 1 && x % 2 == 0) x /= 2, c[0]++; while (x != 1 && x % 3 == 0) x /= 3, c[1]++; while (x != 1 && x % 5 == 0) x /= 5, c[2]++; return x; } int ff(int s) { while (s != 1 && s % 2 == 0) s /= 2, d[0]++; while (s != 1 && s % 3 == 0) s /= 3, d[1]++; while (s != 1 && s % 5 == 0) s /= 5, d[2]++; return s; } int main() { int a, b; cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } if (fj(a) != ff(b)) { cout << -1 << endl; return 0; } cout << abs(c[0] - d[0]) + abs(c[1] - d[1]) + abs(c[2] - d[2]) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int x[3] = {2, 3, 5}; int gcd(int a, int b) { if (b == 0) return a; gcd(b, a % b); } int main() { int n, m; while (~scanf("%d%d", &n, &m)) { if (n == m) { printf("0\n"); continue; } int ans = 0; bool temp = 0; int u = gcd(n, m); n = n / u; m = m / u; if (n < m) swap(n, m); while (true) { bool flag = 0; for (int i = 0; i < 3; i++) { if (n % x[i] == 0) { n = n / x[i]; flag = 1; ans++; break; } } if (n == m) { temp = 1; break; } if (!flag) break; if (n < m) swap(n, m); } if (temp) printf("%d\n", ans); else printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000007; void __print(long long int x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { long long int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } void primeFactorization(long long int n, map<long long int, long long int> &m) { long long int ans = 1; while (n % 2 == 0) { m[2]++; n /= 2; } for (long long int i = 3; i * i <= n; i += 2) { while (n % i == 0) { m[i]++; n /= i; } } if (n > 2) { m[n]++; } } void solve() { long long int a, b; cin >> a >> b; map<long long int, long long int> ma, mb; primeFactorization(a, ma); primeFactorization(b, mb); long long int ans = 0; bool flag = true; ans += abs(ma[2] - mb[2]); ans += abs(ma[3] - mb[3]); ans += abs(ma[5] - mb[5]); for (auto it = ma.begin(); it != ma.end(); it++) { long long int num = it->first; if (num == 2 || num == 3 || num == 5) { continue; } if (mb[it->first] != it->second) { flag = false; } } for (auto it = mb.begin(); it != mb.end(); it++) { long long int num = it->first; if (num == 2 || num == 3 || num == 5) { continue; } if (ma[it->first] != it->second) { flag = false; } }; if (flag) { cout << ans << endl; } else { cout << -1 << endl; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; long a, b; int res = 0; void ProveraDeljenja(int deo) { int prvi = 0, drugi = 0; while (a % deo == 0) { a /= deo; prvi++; } while (b % deo == 0) { b /= deo; drugi++; } res += abs(prvi - drugi); } int main() { scanf("%ld %ld", &a, &b); if (a == b) { printf("0"); return 0; } ProveraDeljenja(2); ProveraDeljenja(3); ProveraDeljenja(5); if (a == b) printf("%d", res); else printf("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int a2 = 0, a3 = 0, a5 = 0, b2 = 0, b3 = 0, b5 = 0; while (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) { if (a % 2 == 0) a2++, a /= 2; if (a % 3 == 0) a3++, a /= 3; if (a % 5 == 0) a5++, a /= 5; } while (b % 2 == 0 || b % 3 == 0 || b % 5 == 0) { if (b % 2 == 0) b2++, b /= 2; if (b % 3 == 0) b3++, b /= 3; if (b % 5 == 0) b5++, b /= 5; } if (a != b) cout << -1; else cout << abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5); return 0; }
#include <bits/stdc++.h> using namespace std; long long nod(long long a, long long b) { if (a == 0 || b == 0) return a + b; if (a > b) return nod(a % b, b); else return nod(a, b % a); } int main() { long long a, b; cin >> a >> b; long long q = nod(a, b); long long a1 = a / q, b1 = b / q; long int res = 0; while (a1 % 2 == 0) { a1 /= 2; res++; } while (a1 % 3 == 0) { a1 /= 3; res++; } while (a1 % 5 == 0) { a1 /= 5; res++; } while (b1 % 2 == 0) { b1 /= 2; res++; } while (b1 % 3 == 0) { b1 /= 3; res++; } while (b1 % 5 == 0) { b1 /= 5; res++; } if (a1 == 1 && b1 == 1) cout << res; else cout << -1; }
#include <bits/stdc++.h> int main() { long n, k, a, b, a2 = {0}, a3 = {0}, a5 = {0}, b2 = {0}, b3 = {0}, b5 = {0}; scanf("%d %d", &a, &b); k = 1; while (k == 1) { if (a % 2 == 0) { a = a / 2; a2++; } else k = 0; } k = 1; while (k == 1) { if (a % 3 == 0) { a = a / 3; a3++; } else k = 0; } k = 1; while (k == 1) { if (a % 5 == 0) { a = a / 5; a5++; } else k = 0; } k = 1; while (k == 1) { if (b % 2 == 0) { b = b / 2; b2++; } else k = 0; } k = 1; while (k == 1) { if (b % 3 == 0) { b = b / 3; b3++; } else k = 0; } k = 1; while (k == 1) { if (b % 5 == 0) { b5++; b = b / 5; } else k = 0; } if (a != b) printf("-1"); else printf("%d", abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int a1 = 0, a2 = 0, a3 = 0, b1 = 0, b2 = 0, b3 = 0; while (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) { if (a % 2 == 0) { a1++; a /= 2; } if (a % 3 == 0) { a2++; a /= 3; } if (a % 5 == 0) { a3++; a /= 5; } } while (b % 2 == 0 || b % 3 == 0 || b % 5 == 0) { if (b % 2 == 0) { b1++; b /= 2; } if (b % 3 == 0) { b2++; b /= 3; } if (b % 5 == 0) { b3++; b /= 5; } } if (a != b) { cout << -1; return 0; } cout << abs(a1 - b1) + abs(a2 - b2) + abs(a3 - b3); return 0; }
#include <bits/stdc++.h> using namespace std; int fat(int a, int n, int *count) { if (a % n == 0) { (*count)++; a = fat(a / n, n, count); } return a; } int solve(int a, int b) { int a2, a3, a5, b2, b3, b5; a2 = a3 = a5 = b2 = b3 = b5 = 0; int p = 0; int k = fat(fat(fat(a, 2, &a5), 3, &a3), 5, &a2); if ((a2 == 0) && (a3 == 0) && (a5 == 0) && (a != 1)) p = 1; int t = fat(fat(fat(b, 2, &b5), 3, &b3), 5, &b2); if ((b2 == 0) && (b3 == 0) && (b5 == 0) && (b != 1)) p = 1; if ((k == t) && (p == 0)) return abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5); else return -1; } int main(int argc, char *argv[]) { int a, b; cin >> a >> b; if (a == b) { cout << "0\n"; return 0; } cout << solve(a, b) << '\n'; return 0; }
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int MAX = 1e5 + 7; using namespace std; map<pair<long long, long long>, long long> mp; long long compute(long long x, long long y) { if (x == y) return 0; if (mp.count({x, y})) return mp[{x, y}]; long long res = 1e18; if (x > 0) { if (x % 2 == 0) res = min(res, 1 + compute(x - (x / 2), y)); if (x % 3 == 0) res = min(res, 1 + compute(x - ((x * 2) / 3), y)); if (x % 5 == 0) res = min(res, 1 + compute(x - ((x * 4) / 5), y)); } if (y > 0) { if (y % 2 == 0) res = min(res, 1 + compute(x, y - (y / 2))); if (y % 3 == 0) res = min(res, 1 + compute(x, y - ((y * 2) / 3))); if (y % 5 == 0) res = min(res, 1 + compute(x, y - ((y * 4) / 5))); } return mp[{x, y}] = res; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long a, b; cin >> a >> b; long long cnt = compute(a, b); if (cnt == 1e18) cout << -1; else cout << cnt; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int inf = 2000000000; const long long int infLL = 9000000000000000000; const int mX = 1e9 + 123; long long int a, b, a2 = 0, a3 = 0, a5 = 0; void case_i() { cin >> a >> b; while (true) { if (a % 2 == 0) { a /= 2; a2++; } else if (a % 3 == 0) { a /= 3; a3++; } else if (a % 5 == 0) { a /= 5; a5++; } else break; } while (true) { if (b % 2 == 0) { b /= 2; a2--; } else if (b % 3 == 0) { b /= 3; a3--; } else if (b % 5 == 0) { b /= 5; a5--; } else break; } if (a != b) cout << -1 << endl; else cout << abs(a2) + abs(a3) + abs(a5) << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; case_i(); return 0; }
#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; int t = 0; cin >> a >> b; int mcd = gcd(a, b); int c1 = a / mcd, c2 = b / mcd; while (c1 % 2 == 0) { c1 >>= 1; t++; } while (c1 % 3 == 0) { c1 /= 3; t++; } while (c1 % 5 == 0) { c1 /= 5; t++; } if (c1 != 1) puts("-1"); else { while (c2 % 2 == 0) { c2 >>= 1; t++; } while (c2 % 3 == 0) { c2 /= 3; t++; } while (c2 % 5 == 0) { c2 /= 5; t++; } if (c2 != 1) puts("-1"); else cout << t << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int primes[3] = {2, 3, 5}; int a[2], b[2], cnt[2][3] = {}; cin >> a[0] >> a[1]; b[0] = a[0]; b[1] = a[1]; for (int i = 0, _i = (2); i < _i; ++i) { for (int j = 0, _j = (3); j < _j; ++j) { while (!(b[i] % primes[j])) { b[i] /= primes[j]; cnt[i][j]++; } } } int ans = -1; if (b[1] == b[0]) { ans = 0; for (int j = (3), _j = (0); --j >= _j;) { for (int i = 0, _i = (2); i < _i; ++i) if (cnt[i][j] > cnt[1 - i][j]) { ans += cnt[i][j] - cnt[1 - i][j]; } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, i = 0, ans = 0; cin >> a >> b; int v[] = {2, 3, 5}; while (i < 3) { int x = 0, y = 0; while (a % v[i] == 0) { a /= v[i]; x++; } while (b % v[i] == 0) { b /= v[i]; y++; } ans += abs(x - y); i++; } if (a != b) { cout << "-1"; } else { cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; void INPUT() {} long long A[3]; long long divison(long long n, long long t) { while (n > 0) { long long cnt = 0; if (n % 2 == 0) n /= 2, cnt++, A[0] += t; else if (n % 3 == 0) n /= 3, cnt++, A[1] += t; else if (n % 5 == 0) n /= 5, cnt++, A[2] += t; if (!cnt) break; } return n; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); INPUT(); long long a, b; cin >> a >> b; long long ans = 0; a = divison(a, 1); b = divison(b, -1); for (long long i = 0; i < 3; i++) ans += abs(A[i]); if (a != b) ans = -1; cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, c; long long x, y, z; int main() { long long n, m, ans = 0; bool t1 = 1, t2 = 1; cin >> n >> m; if (m == n) cout << 0; else { while (t1) { t1 = 0; if (n % 2 == 0) { a++; t1 = 1; n /= 2; } else if (n % 3 == 0) { b++; t1 = 1; n /= 3; } else if (n % 5 == 0) { c++; t1 = 1; n /= 5; } } while (t2) { t2 = 0; if (m % 2 == 0) { x++; t2 = 1; m /= 2; } else if (m % 3 == 0) { y++; t2 = 1; m /= 3; } else if (m % 5 == 0) { z++; t2 = 1; m /= 5; } } if (n != m) cout << -1; else { ans += abs(a - x); ans += abs(b - y); ans += abs(c - z); cout << ans; } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; using pcc = pair<char, char>; using pdd = pair<double, double>; using vi = vector<int>; using vs = vector<string>; using vll = vector<ll>; using vpii = vector<pii>; using vc = vector<char>; using vd = vector<double>; ll a, b, t, t1, th, th1, f, f1; int main(int argc, char *argv[]) { scanf("%lld%*c%lld%*c", &(a), &(b)); while (a % 2 == 0) { t++; a /= 2; } while (a % 3 == 0) th++, a /= 3; while (a % 5 == 0) f++, a /= 5; while (b % 2 == 0) t1++, b /= 2; while (b % 3 == 0) th1++, b /= 3; while (b % 5 == 0) f1++, b /= 5; if (a != b) printf("%d", (-1)); else printf("%lld", (abs(t - t1) + abs(f - f1) + abs(th - th1))); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; long long int a, b; int main() { while (cin >> a >> b) { if (a == b) cout << 0 << endl; else { int a1, a2, a3, b1, b2, b3; a1 = a2 = a3 = b1 = b2 = b3 = 0; int aa = a, bb = b; bool flag = 1; for (; flag != 0;) { flag = 0; if (aa % 2 == 0) { flag = 1; aa = aa / 2; a1++; } if (aa % 3 == 0) { flag = 1; aa = aa / 3; a2++; } if (aa % 5 == 0) { flag = 1; aa = aa / 5; a3++; } } flag = 1; for (; flag != 0;) { flag = 0; if (bb % 2 == 0) { flag = 1; bb = bb / 2; b1++; } if (bb % 3 == 0) { flag = 1; bb = bb / 3; b2++; } if (bb % 5 == 0) { flag = 1; bb = bb / 5; b3++; } } if (aa == bb) { int aaa = a1 - b1; int bbb = a2 - b2; int ccc = a3 - b3; if (aaa < 0) aaa = -aaa; if (bbb < 0) bbb = -bbb; if (ccc < 0) ccc = -ccc; int ans; ans = aaa + bbb + ccc; cout << ans << endl; } else cout << -1 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; long long int fexpo(long long int a, long long int b) { if (b == 0) return 1LL; if (b == 1) return a; if (b == 2) return a * a; if (b % 2 == 0) return fexpo(fexpo(a, b / 2), 2); else return a * fexpo(fexpo(a, (b - 1) / 2), 2); } int main() { long long int a, b; cin >> a >> b; int a_2, a_3, a_5; int b_2, b_3, b_5; a_2 = a_5 = a_3 = b_2 = b_3 = b_5 = 0; while (a % 2 == 0) { a = a / 2; a_2++; } while (a % 3 == 0) { a = a / 3; a_3++; } while (a % 5 == 0) { a = a / 5; a_5++; } while (b % 2 == 0) { b = b / 2; b_2++; } while (b % 3 == 0) { b = b / 3; b_3++; } while (b % 5 == 0) { b = b / 5; b_5++; } if (b != a) cout << "-1"; else cout << abs(b_5 - a_5) + abs(b_3 - a_3) + abs(a_2 - b_2); }
#include <bits/stdc++.h> using namespace std; void solve() { int a, b; cin >> a >> b; if (a == b) { cout << "0\n"; return; } int ans = 0; for (int i : {2, 3, 5}) { int c1 = 0; while (a % i == 0) { a /= i; c1++; } int c2 = 0; while (b % i == 0) { b /= i; c2++; } ans += abs(c1 - c2); } cout << (a == b ? ans : -1) << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int T[2][4]; int abs(int x, int y) { if (x - y > 0) return x - y; else return y - x; } int main() { int a, b; cin >> a >> b; while (!(a % 2)) { a /= 2; T[0][0]++; } while (!(a % 3)) { a /= 3; T[0][1]++; } while (!(a % 5)) { a /= 5; T[0][2]++; } while (!(b % 2)) { b /= 2; T[1][0]++; } while (!(b % 3)) { b /= 3; T[1][1]++; } while (!(b % 5)) { b /= 5; T[1][2]++; } if (a != b) cout << -1 << endl; else { cout << abs(T[0][0], T[1][0]) + abs(T[0][1], T[1][1]) + abs(T[0][2], T[1][2]) << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int a3 = 0, a2 = 0, a5 = 0, b2 = 0, b3 = 0, b5 = 0; while (a % 2 == 0 || a % 3 == 0 || a % 5 == 0 || b % 2 == 0 || b % 3 == 0 || b % 5 == 0) { if (a % 2 == 0) { a2++; a /= 2; } if (a % 3 == 0) { a3++; a /= 3; } if (a % 5 == 0) { a5++; a /= 5; } if (b % 2 == 0) { b2++; b /= 2; } if (b % 3 == 0) { b3++; b /= 3; } if (b % 5 == 0) { b5++; b /= 5; } } if (a != b) { cout << "-1"; return 0; } else cout << abs(a3 - b3) + abs(a2 - b2) + abs(a5 - b5); }
#include <bits/stdc++.h> long long fox(long long a, long long b); long long gcd(long long a, long long b); int main() { long long a, b, c, d; scanf("%lld%lld", &a, &b); d = (a > b) ? gcd(a, b) : gcd(b, a); c = fox(a / d, b / d); printf("%lld", c); return 0; } long long gcd(long long a, long long b) { if (b == (long long)0) return a; return gcd(b, a % b); } long long fox(long long a, long long b) { static long long count = (long long)0; long long larger, smaller; if (a == b) return count; count += (long long)1; if (a > b) { larger = a, smaller = b; } else { smaller = a, larger = b; } if (larger % (long long)2 == (long long)0) { if (larger == a) a /= (long long)2; else b /= (long long)2; return fox(a, b); } else if (larger % (long long)3 == (long long)0) { if (larger == a) a /= (long long)3; else b /= (long long)3; return fox(a, b); } else if (larger % (long long)5 == (long long)0) { if (larger == a) a /= (long long)5; else b /= (long long)5; return fox(a, b); } else return -1; }
#include <bits/stdc++.h> using namespace std; long long big = 1000000007; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long a, b; cin >> a >> b; int g = gcd(a, b); a /= g; b /= g; int ctr = 0; while (a % 2 == 0) { ctr += 1; a /= 2; } while (a % 3 == 0) { ctr += 1; a /= 3; } while (a % 5 == 0) { ctr += 1; a /= 5; } if (a != 1) { cout << -1 << '\n'; return 0; } while (b % 2 == 0) { ctr += 1; b /= 2; } while (b % 3 == 0) { ctr += 1; b /= 3; } while (b % 5 == 0) { ctr += 1; b /= 5; } if (b != 1) { cout << -1 << '\n'; return 0; } cout << ctr << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; void coef(int& a, int& x, int& y, int& z) { int i = 0, j = 0, k = 0; while (a % 2 == 0) { i++; a /= 2; } while (a % 3 == 0) { j++; a /= 3; } while (a % 5 == 0) { k++; a /= 5; } x = i; y = j; z = k; } void solve() { int a, b; cin >> a >> b; int x, y, z, x1, y1, z1; coef(a, x, y, z); coef(b, x1, y1, z1); if (b != a) { cout << "-1"; return; } cout << abs(x - x1) + abs(y - y1) + abs(z - z1); } int main() { int t = 1; for (int i = 0; i < t; i++) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int INF = INT_MAX; const int NINF = INT_MIN; int a, b; vector<int> veca, vecb; void Reduce(int& x, vector<int>& vec) { while (x % 2 == 0) { vec[0]++; x = x / 2; } while (x % 3 == 0) { vec[1]++; x = x / 3; } while (x % 5 == 0) { vec[2]++; x = x / 5; } } void Solve() { cin >> a >> b; veca = vecb = vector<int>(3, 0); Reduce(a, veca); Reduce(b, vecb); if (a != b) { cout << -1 << "\n"; return; } int ans = 0; for (int i = 0; i <= 2; i++) { ans += abs(veca[i] - vecb[i]); } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2000 * 100 + 10; map<long long, int> M, M2; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long a, b; scanf( "%lld" "%lld", &a, &b); if (a == b) { puts("0"); return 0; } long long d = gcd(a, b); a /= d; b /= d; long long aa = a; for (int i = 2; i * i <= a; i++) { if (aa % i == 0) { while (aa % i == 0) { M[i]++; aa /= i; } } } if (aa != 1) M[aa]++; long long bb = b; for (int i = 2; i * i <= b; i++) { if (bb % i == 0) { while (bb % i == 0) { M2[i]++; bb /= i; } } } if (bb != 1) M2[bb]++; long long ans = 0; for (map<long long, int>::iterator it = M.begin(); it != M.end(); it++) { if (it->first == 2) { ans += it->second; } else { if (it->first == 3) { ans += it->second; } else { if (it->first == 5) { ans += it->second; } else { puts("-1"); return 0; } } } } for (map<long long, int>::iterator it = M2.begin(); it != M2.end(); it++) { if (it->first == 2) { ans += it->second; } else { if (it->first == 3) { ans += it->second; } else { if (it->first == 5) { ans += it->second; } else { puts("-1"); return 0; } } } } printf("%lld", ans); return 0; }
#include <bits/stdc++.h> using namespace std; namespace ZBXW { void divide(int& x, int* cnt) { while (x % 2 == 0) { x /= 2; ++cnt[0]; } while (x % 3 == 0) { x /= 3; ++cnt[1]; } while (x % 5 == 0) { x /= 5; ++cnt[2]; } } void __main__() { int a, b; cin >> a >> b; int cnt_a[3] = {0, 0, 0}; int cnt_b[3] = {0, 0, 0}; divide(a, cnt_a); divide(b, cnt_b); if (a != b) { cout << -1 << endl; } else { cout << (abs(cnt_a[0] - cnt_b[0]) + abs(cnt_a[1] - cnt_b[1]) + abs(cnt_a[2] - cnt_b[2])) << endl; } } } // namespace ZBXW int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ZBXW::__main__(); return 0; }
#include <bits/stdc++.h> using namespace std; int Factors(int a, vector<int>& A) { int cnt = 0; while (a % 2 == 0) { cnt++; a /= 2; } A[0] = cnt; cnt = 0; while (a % 3 == 0) { cnt++; a /= 3; } A[1] = cnt; cnt = 0; while (a % 5 == 0) { cnt++; a /= 5; } A[2] = cnt; return a; } int main() { int a, b; cin >> a >> b; vector<int> A(3, 0); vector<int> B(3, 0); int ansA = Factors(a, A); int ansB = Factors(b, B); if (ansA != ansB) { cout << -1; return 0; } int tot = 0; int comm = 0; for (int i = 0; i < 3; i++) { tot += A[i] + B[i]; comm += min(A[i], B[i]) * 2; } cout << tot - comm; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 2e9; const long long INF64 = 9e18; const long double EPS = 1e-9; const long double PI = 3.14159265358979310000; const int N = 100001; const int M = 12001; int first[12], second[12]; int main() { long long a, b; cin >> a >> b; if (a == b) { cout << 0; return 0; } while (a % 2 == 0) { a /= 2; first[2]++; } while (a % 3 == 0) { a /= 3; first[3]++; } while (a % 5 == 0) { a /= 5; first[5]++; } while (b % 2 == 0) { b /= 2; second[2]++; } while (b % 3 == 0) { b /= 3; second[3]++; } while (b % 5 == 0) { b /= 5; second[5]++; } if (a != b) { cout << -1; return 0; } cout << abs(first[2] - second[2]) + abs(first[3] - second[3]) + abs(first[5] - second[5]); return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { int r = a % b; if (r == 0) return b; return gcd(b, r); } int main() { int a, b; cin >> a >> b; int g = gcd(a, b); int foxa = a / g; int foxb = b / g; int cnt = 0; while (foxa % 2 == 0 || foxa % 3 == 0 || foxa % 5 == 0) { if (foxa % 2 == 0) { foxa /= 2; cnt++; } if (foxa % 3 == 0) { foxa /= 3; cnt++; } if (foxa % 5 == 0) { foxa /= 5; cnt++; } } while (foxb % 2 == 0 || foxb % 3 == 0 || foxb % 5 == 0) { if (foxb % 2 == 0) { foxb /= 2; cnt++; } if (foxb % 3 == 0) { foxb /= 3; cnt++; } if (foxb % 5 == 0) { foxb /= 5; cnt++; } } if (foxa == 1 && foxb == 1) cout << cnt; else cout << -1; }
#include <bits/stdc++.h> using namespace std; int ar[100000] = {0}; int main() { int a, b; cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } int a2 = 0, a3 = 0, a5 = 0; int acheck = a; while (acheck % 2 == 0) { a2++; acheck /= 2; } while (acheck % 3 == 0) { a3++; acheck /= 3; } while (acheck % 5 == 0) { a5++; acheck /= 5; } int b2 = 0, b3 = 0, b5 = 0; int bcheck = b; while (bcheck % 2 == 0) { b2++; bcheck /= 2; } while (bcheck % 3 == 0) { b3++; bcheck /= 3; } while (bcheck % 5 == 0) { b5++; bcheck /= 5; } if (acheck == bcheck) { cout << abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5) << endl; } else cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; int dxx[] = {-1, 0, 1, 0}; int dyy[] = {0, 1, 0, -1}; bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) { return (a.second < b.second); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int a, b; cin >> a >> b; int pa[6] = {0}, push_back[6] = {0}; vector<int> prime; prime.push_back(2); prime.push_back(3); prime.push_back(5); for (int k : prime) { while (a % k == 0) { pa[k]++; a /= k; } while (b % k == 0) { push_back[k]++; b /= k; } } if (a != b) { cout << -1 << '\n'; return 0; } int ans = 0; for (auto k : prime) { ans += abs(pa[k] - push_back[k]); } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; const int m = 132000; int solve(long long a, long long b) { int two1 = 0, two2 = 0, three1 = 0, three2 = 0, five1 = 0, five2 = 0; while (a % 2 == 0) { two1++; a /= 2; } while (b % 2 == 0) { two2++; b /= 2; } while (a % 3 == 0) { three1++; a /= 3; } while (b % 3 == 0) { three2++; b /= 3; } while (a % 5 == 0) { five1++; a /= 5; } while (b % 5 == 0) { five2++; b /= 5; } if (a != b) return -1; return abs(two1 - two2) + abs(three1 - three2) + abs(five1 - five2); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long a, b; cin >> a >> b; cout << solve(a, b) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; class TC { public: void solve() { ll a, b; cin >> a >> b; ll two = 0, three = 0, five = 0; ll aa = f(a, 5, five); aa = f(aa, 3, three); aa = f(aa, 2, two); ll bb = f(b, 5, five, -1); bb = f(bb, 3, three, -1); bb = f(bb, 2, two, -1); if (aa != bb) { cout << -1 << endl; } else { cout << abs(two) + abs(three) + abs(five) << endl; } } ll f(ll no, ll num, ll &var, ll factor = 1) { while (no % num == 0) { no /= num; var += (factor); } return no; } }; int main() { ios_base::sync_with_stdio(false); int testcases = 1; for (int i = 1; i <= testcases; i++) { TC tc; tc.solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int a235[3]; int b235[3]; void devide(int& num, int* x235, int d) { int i; switch (d) { case 2: i = 0; break; case 3: i = 1; break; case 5: i = 2; break; default: break; } int cnt = 0; while (num % d == 0) { cnt++; num /= d; } x235[i] = cnt; } int main() { int a, b; cin >> a >> b; devide(a, a235, 2); devide(a, a235, 3); devide(a, a235, 5); devide(b, b235, 2); devide(b, b235, 3); devide(b, b235, 5); if (a == b) { long long ans = abs(a235[0] - b235[0]) + abs(a235[1] - b235[1]) + abs(a235[2] - b235[2]); cout << ans << endl; } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool prime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } void solve(int &n, int arr[]) { while (n % 2 == 0) { n = n / 2; arr[2]++; } while (n % 3 == 0) { n = n / 3; arr[3]++; } while (n % 5 == 0) { n = n / 5; arr[5]++; } } int main() { int a, b; cin >> a >> b; int arr1[6] = {0}; int arr2[6] = {0}; solve(a, arr1); solve(b, arr2); if (a == b) { int ret = 0; ret = ret + abs(arr1[2] - arr2[2]); ret = ret + abs(arr1[3] - arr2[3]); ret = ret + abs(arr1[5] - arr2[5]); cout << ret; } else { cout << -1; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int a, b; cin >> a >> b; int c1 = 0, c2 = 0, c3 = 0, c4 = 0, c5 = 0, c6 = 0; while (a % 2 == 0) { a = a / 2; c1++; } while (a % 3 == 0) { a = a / 3; c2++; } while (a % 5 == 0) { a = a / 5; c3++; } while (b % 2 == 0) { b = b / 2; c4++; } while (b % 3 == 0) { b = b / 3; c5++; } while (b % 5 == 0) { b = b / 5; c6++; } int res = abs(c1 - c4) + abs(c2 - c5) + abs(c3 - c6); if (a == b) { cout << res << '\n'; } else { cout << "-1" << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, x = 0, y = 0, z = 0, p = 0, q = 0, r = 0, m, n; cin >> a >> b; if (a == b) { cout << "0"; return 0; } while (a % 2 == 0) { a /= 2; x++; } while (a % 3 == 0) { a /= 3; y++; } while (a % 5 == 0) { a /= 5; z++; } m = a; while (b % 2 == 0) { b /= 2; p++; } while (b % 3 == 0) { b /= 3; q++; } while (b % 5 == 0) { b /= 5; r++; } n = b; if (m != n) cout << "-1"; else cout << abs(x - p) + abs(y - q) + abs(z - r); return 0; }
#include <bits/stdc++.h> using namespace std; map<int, int> factor(int number) { map<int, int> fc; for (int i = 2; i <= sqrt(number); ++i) { while (number % i == 0) { fc[i]++; number = number / i; } } if (number != 1) { fc[number]++; } return fc; } bool isDevider(int n) { return n == 2 || n == 3 || n == 5; } int spow(int n, int p) { int res = 1; for (int i = 0; i < p; ++i) { res *= n; } return res; } int bpow(int a, int b) { if (b == 0) return 1; int t = bpow(a, b / 2); if (b & 1) return t * t * a; return t * t; } int toNumber(const map<int, int>& factors) { int res = 1; for (const auto& f : factors) { res *= spow(f.first, f.second); } return res; } int calc(int a, int b) { if (a == b) { return 0; } int res = 0; auto factorA = factor(a); auto factorB = factor(b); for (int i : vector<int>{2, 3, 5}) { auto itA = factorA.find(i); auto itB = factorB.find(i); int ca = 0, cb = 0; if (itA != factorA.end() && itB != factorB.end()) { res += abs(itA->second - itB->second); itA->second = 0; itB->second = 0; } else if (itA != factorA.end()) { res += itA->second; itA->second = 0; } else if (itB != factorB.end()) { res += itB->second; itB->second = 0; } } return toNumber(factorA) == toNumber(factorB) ? res : -1; } int main() { int a, b; cin >> a >> b; cout << calc(a, b) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a, b, k1, k2, x[300000], y[300000]; set<int> s; int f(int p) { int res = 0; while (p % 2 == 0) { res++; p /= 2; } while (p % 3 == 0) { res++; p /= 3; } while (p % 5 == 0) { res++; p /= 5; } if (p != 1) return 1000000000; return res; } int main() { cin >> a >> b; int k1 = 0, k2 = 0; for (int i = 1; i <= a; i++) { if (i * i > a) break; if (a % i == 0) { k1++; x[k1] = i; if (i != a / i) { k1++; x[k1] = a / i; } } } for (int i = 1; i <= b; i++) { if (i * i > b) break; if (b % i == 0) { s.insert(i); if (i != b / i) { s.insert(b / i); } } } sort(x + 1, x + k1 + 1); sort(y + 1, y + k2 + 1); int j = 1, ans = 1000000000; for (int i = 1; i <= k1; i++) { if (s.find(x[i]) != s.end()) { ans = min(ans, f(a / x[i]) + f(b / x[i])); } } if (ans != 1000000000) cout << ans; else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx2") #pragma GCC optimization("unroll-loops") #pragma GCC optimize("O2") constexpr int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, -1, 0, 1, 1, -1, 1, -1}; constexpr long long INF = 1999999999999999997; constexpr int inf = INT_MAX; constexpr int MAXSIZE = int(1e6) + 5; constexpr auto PI = 3.14159265358979323846L; constexpr auto oo = numeric_limits<int>::max() / 2 - 2; constexpr auto eps = 1e-6; constexpr auto mod = 1000000007; constexpr auto MOD = 1000000007; constexpr auto MOD9 = 1000000009; constexpr auto maxn = 100006; void fastio() { ios_base::sync_with_stdio(false); cin.tie(NULL); } int v[2][3]; int main() { fastio(); int a[2]; cin >> a[0] >> a[1]; int b[] = {2, 3, 5}; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { while (a[i] % b[j] == 0) { a[i] /= b[j]; v[i][j]++; } } } if (a[0] != a[1]) { cout << "-1"; return 0; } int ans = 0; for (int i = 0; i < 3; i++) { ans += (abs(v[1][i] - v[0][i])); } cout << ans; }
#include <bits/stdc++.h> using namespace std; template <class T> void read(T& x) { cin >> x; } template <class A> void read(vector<A>& a) { for (auto& x : a) read(x); } template <class T> string to_string(const vector<T>& v) { string res; for (auto i = 0; i < ((int)v.size()); i++) res += to_string(v[i]) + " "; res.pop_back(); return res; } void DBG() { cerr << "]" << endl; } template <class H, class... T> void DBG(H h, T... t) { cerr << to_string(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } int32_t main(int argc, char const* argv[]) { ios_base::sync_with_stdio(false), cin.tie(NULL); int a, b; cin >> a >> b; int div[6] = {0}; for (int x : {2, 3, 5}) { while (a % x == 0) { div[x]++; a /= x; } while (b % x == 0) { div[x]--; b /= x; } } if (a != b) { cout << "-1" << endl; } else { int ans = 0; for (int d : div) ans += abs(d); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int power_of_5(long long int p); int power_of_3(long long int p); int power_of_2(long long int p); long long int gcd(long long int m, long long int n); int main() { int q, r, s, t, u, v; long long int a, b; cin >> a >> b; long long int g = gcd(a, b); long long int x = a / g; long long int y = b / g; q = power_of_5(x); r = power_of_3(x); s = power_of_2(x); t = power_of_5(y); u = power_of_3(y); v = power_of_2(y); if ((x == pow(5, q) * pow(3, r) * pow(2, s)) && (y == pow(5, t) * pow(3, u) * pow(2, v))) { cout << (s + q + r + t + u + v) << "\n"; } else cout << "-1\n"; return 0; } int power_of_5(long long int p) { int c1 = 0; while ((p % 5) == 0) { c1++; p = p / 5; } return c1; } int power_of_3(long long int p) { int c2 = 0; while ((p % 3) == 0) { c2++; p = p / 3; } return c2; } int power_of_2(long long int p) { int c3 = 0; while ((p % 2) == 0) { c3++; p = p / 2; } return c3; } long long int gcd(long long int m, long long int n) { long long int r; while (n != 0) { r = m % n; m = n; n = r; } return m; }
#include <bits/stdc++.h> using namespace std; long long a, b, ans; long long gcd(int a, int b) { while (b != 0) { long long r = b; b = a % b; a = r; } return a; } int main() { scanf("%lld%lld", &a, &b); if (a == b) { printf("0"); return 0; } long long c; if (b > a) c = a, a = b, b = c; c = gcd(a, b); a = a * b / c / c; while (a % 2 == 0) { a /= 2; ans++; } while (a % 3 == 0) { a /= 3; ans++; } while (a % 5 == 0) { a /= 5; ans++; } if (a != 1 && a != 0) ans = -1; printf("%lld", ans); }
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int a, b; cin >> a >> b; vector<int> x(3), y(3); while (a % 2 == 0) { a = a / 2; x[0]++; } while (a % 3 == 0) { a = a / 3; x[1]++; } while (a % 5 == 0) { a = a / 5; x[2]++; } while (b % 2 == 0) { b = b / 2; y[0]++; } while (b % 3 == 0) { b = b / 3; y[1]++; } while (b % 5 == 0) { b = b / 5; y[2]++; } if (a != b) cout << "-1"; else { int ans = 0; for (int i = 0; i < 3; i++) ans += abs(x[i] - y[i]); cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INFI = (1 << 30); const long long INFL = (1LL << 62); const double eps = 1e-7; const long double pi = acos(-1.0); const int md = 1e9 + 7; int main() { int a, b; map<int, int> m1, m2; cin >> a >> b; m1[a] = 0; m2[b] = 0; queue<int> q; q.push(a); while (!q.empty()) { int x = q.front(); int tm = m1[x]; q.pop(); if (x % 2 == 0 && (m1.find(x / 2) == m1.end() || tm + 1 < m1[x / 2])) { q.push(x / 2); m1[x / 2] = tm + 1; } if (x % 3 == 0 && (m1.find(x / 3) == m1.end() || tm + 1 < m1[x / 3])) { q.push(x / 3); m1[x / 3] = tm + 1; } if (x % 5 == 0 && (m1.find(x / 5) == m1.end() || tm + 1 < m1[x / 5])) { q.push(x / 5); m1[x / 5] = tm + 1; } } q.push(b); while (!q.empty()) { int x = q.front(); int tm = m2[x]; q.pop(); if (x % 2 == 0 && (m2.find(x / 2) == m2.end() || tm + 1 < m2[x / 2])) { q.push(x / 2); m2[x / 2] = tm + 1; } if (x % 3 == 0 && (m2.find(x / 3) == m2.end() || tm + 1 < m2[x / 3])) { q.push(x / 3); m2[x / 3] = tm + 1; } if (x % 5 == 0 && (m2.find(x / 5) == m2.end() || tm + 1 < m2[x / 5])) { q.push(x / 5); m2[x / 5] = tm + 1; } } int mn = INFI; for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) if (m2.find(it->first) != m2.end()) mn = min(mn, m2[it->first] + it->second); if (mn != INFI) cout << mn << '\n'; else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { int r; while (y) r = x % y, x = y, y = r; return x; } bool factor(int a, int &cnt) { cnt = 0; while (a % 2 == 0) a /= 2, cnt++; while (a % 3 == 0) a /= 3, cnt++; while (a % 5 == 0) a /= 5, cnt++; if (a != 1) return false; return true; } void input() { int a, b, x, y; scanf("%d %d", &a, &b); int uc = gcd(a, b); if (factor(a / uc, x) && factor(b / uc, y)) printf("%d", x + y); else printf("-1"); } int main() { input(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long modul = 1e9 + 7; const long long maxn = 2e5 + 5; const string fi = "input.txt"; const string fo = "output.txt"; long long a[maxn], b[maxn]; string c[1004]; long long ucln(long long m, long long n) { while (n != 0) { long long du = m % n; m = n; n = du; } return m; } long long ans(long long a, long long b) { long long x = ucln(a, b); a = a / x; b = b / x; long long cnt = 0; while (a % 2 == 0) { a = a / 2; cnt++; } while (a % 3 == 0) { a = a / 3; cnt++; } while (a % 5 == 0) { a = a / 5; cnt++; } while (b % 2 == 0) { b = b / 2; cnt++; } while (b % 3 == 0) { b = b / 3; cnt++; } while (b % 5 == 0) { b = b / 5; cnt++; } if (a != 1 or b != 1) return -1; return cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(); long long a, b; cin >> a >> b; cout << ans(a, b); }
#include <bits/stdc++.h> using namespace std; void dk_98() {} long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long findGCD(vector<long long> arr, long long n) { long long result = arr[0]; for (long long i = 1; i < n; i++) { result = gcd(arr[i], result); if (result == 1) { return 1; } } return result; } void solve() { long long a, b; cin >> a >> b; vector<long long> fa(6, 0), fb(6, 0); for (int i = 2; i <= 5; i++) { while (a % i == 0) { a /= i; fa[i]++; } } for (int i = 2; i <= 5; i++) { while (b % i == 0) { b /= i; fb[i]++; } } if (a != b) { cout << -1; } else { cout << abs(fa[2] - fb[2]) + abs(fa[3] - fb[3]) + abs(fa[5] - fb[5]); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; dk_98(); int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; struct Tnode { int x, y, z; } doing[1000050]; struct Type { int pos, val, size; Type *father, *son[2]; Type() {} Type(Type *f, int p, int v, int s) { pos = p; val = v; size = s; father = f; son[0] = son[1] = 0; } } memory[1000050], *root; struct Tree { int best, add; } tree[2222222]; int vs, n, m, data[1000050], x[1000050], y[1000050], z[1000050]; int Rand() { return (rand() << 15) | rand(); } bool Cmp(Tnode a, Tnode b) { return a.x < b.x; } int Half(int ask) { int low, mid, high; low = 0; high = m + 1; while (low + 1 < high) { mid = (low + high) >> 1; if (data[mid] <= ask) { low = mid; } else { high = mid; } } return low; } void Down(int root) { if (tree[root].add != 0) { tree[root].best += tree[root].add; tree[root << 1].add += tree[root].add; tree[(root << 1) | 1].add += tree[root].add; tree[root].add = 0; } return; } void Add(int root, int nowleft, int nowright, int askleft, int askright, int add) { int mid = (nowleft + nowright) >> 1; Down(root); if (nowright < askleft || askright < nowleft) { return; } if (askleft <= nowleft && nowright <= askright) { tree[root].add += add; Down(root); return; } Add(root << 1, nowleft, mid, askleft, askright, add); Add((root << 1) | 1, mid + 1, nowright, askleft, askright, add); tree[root].best = min(tree[root << 1].best, tree[(root << 1) | 1].best); return; } int Ask(int root, int nowleft, int nowright, int askleft, int askright) { int mid = (nowleft + nowright) >> 1; Down(root); if (nowright < askleft || askright < nowleft) { return 666666; } if (askleft <= nowleft && nowright <= askright) { return tree[root].best; } return min(Ask(root << 1, nowleft, mid, askleft, askright), Ask((root << 1) | 1, mid + 1, nowright, askleft, askright)); } void Update(Type *current) { if (!current) { return; } current->size = 1; if (current->son[0]) { current->size += current->son[0]->size; } if (current->son[1]) { current->size += current->son[1]->size; } return; } void Rotate(Type *current, int flag) { current->father->son[flag ^ 1] = current->son[flag]; if (current->son[flag]) { current->son[flag]->father = current->father; } current->son[flag] = current->father; if (current->father->father) { current->father->father ->son[current->father->father->son[0] != current->father] = current; } current->father = current->father->father; current->son[flag]->father = current; Update(current->son[flag]); return; } void Splay(Type *current, Type *target) { while (current->father != target) { if (current->father->father == target) { Rotate(current, current->father->son[1] != current); } else if (current->father->father->son[0] == current->father && current->father->son[0] == current) { Rotate(current->father, 1); Rotate(current, 1); } else if (current->father->father->son[1] == current->father && current->father->son[1] == current) { Rotate(current->father, 0); Rotate(current, 0); } else { Rotate(current, current->father->son[1] != current); Rotate(current, current->father->son[1] != current); } } Update(current); if (!target) { root = current; } return; } void Bigger(int ask, Type *target) { Type *current = root, *best = 0; while (current) { if (current->pos > ask) { best = current; current = current->son[0]; } else { current = current->son[1]; } } Splay(best, target); return; } void Smaller(int ask, Type *target) { Type *current = root, *best = 0; while (current) { if (current->pos < ask) { best = current; current = current->son[1]; } else { current = current->son[0]; } } Splay(best, target); return; } void Find(int ask, Type *target) { Type *current = root; while (current) { if (current->son[0]) { if (ask == current->son[0]->size + 1) { break; } } else if (ask == 1) { break; } if (current->son[0] && ask < current->son[0]->size + 1) { current = current->son[0]; } else { ask--; if (current->son[0]) { ask -= current->son[0]->size; } current = current->son[1]; } } Splay(current, target); return; } void Find_Left(Type *current) { while (true) { if (!current->son[0]) { break; } current = current->son[0]; } Splay(current, 0); return; } void Find_Right(Type *current) { while (true) { if (!current->son[1]) { break; } current = current->son[1]; } Splay(current, 0); return; } int main() { int i, j, temp, best, delta, rank, last, maxi, mx, my, mz; bool solved; Type *current; srand((unsigned)time(0)); scanf("%d", &n); m = 0; for (i = 1; i <= n; i++) { scanf("%d", &x[i]); data[++m] = x[i]; } for (i = 1; i <= n; i++) { scanf("%d", &y[i]); data[++m] = y[i]; } for (i = 1; i <= n; i++) { scanf("%d", &z[i]); data[++m] = z[i]; } sort(data + 1, data + m + 1); for (i = j = 1; i < m; i++) if (data[i + 1] != data[j]) { data[++j] = data[i + 1]; } m = j; for (i = 1; i <= m; i++) { doing[i].x = doing[i].y = doing[i].z = n + 1; } for (i = 1; i <= n; i++) { temp = Half(x[i]); doing[temp].x = min(doing[temp].x, i); } for (i = 1; i <= n; i++) { temp = Half(y[i]); doing[temp].y = min(doing[temp].y, i); } for (i = 1; i <= n; i++) { temp = Half(z[i]); doing[temp].z = min(doing[temp].z, i); } sort(doing + 1, doing + m + 1, Cmp); for (i = 1; i <= n; i++) { Add(1, 1, n, i, i, i); } memory[++vs] = Type(0, n + 1, 0, 3); root = &memory[vs]; memory[++vs] = Type(root, -666666, 0, 1); root->son[0] = &memory[vs]; memory[++vs] = Type(root, 666666, 0, 1); root->son[1] = &memory[vs]; best = 3 * n; mx = my = mz = 0; for (i = 1; i <= m; i++) { mx = max(mx, doing[i].x); my = max(my, doing[i].y); mz = max(mz, doing[i].z); } if (mx <= n) { best = min(best, mx); } if (my <= n) { best = min(best, my); } if (mz <= n) { best = min(best, mz); } maxi = 0; for (i = m; i >= 1; i--) { solved = false; Smaller(doing[i].y, 0); Bigger(doing[i].y, root); if (doing[i].z > root->son[1]->val) { if (!root->son[1]->son[0]) { memory[++vs] = Type(root->son[1], doing[i].y, doing[i].z, 1); current = root->son[1]->son[0] = &memory[vs]; Update(root->son[1]); Update(root); } else { solved = true; if (root->son[1]->son[0]->val < doing[i].z) { current = root->son[1]->son[0]; Splay(current, 0); delta = doing[i].z; if (doing[i].z == n + 1) { delta = 666666; } delta -= current->val; if (current->son[0]->size == 1) { Add(1, 1, n, 1, n, delta); } else { Find_Right(current->son[0]); Add(1, 1, n, root->pos, n, delta); } if (doing[i].z <= n) { current->val = doing[i].z; } else { current->val = 666666; } } } } else { current = 0; solved = true; } if (current) { Splay(current, 0); if (current->val == n + 1) { current->val = 666666; } if (!solved) { delta = current->val; if (current->son[0]->size == 1) { Find_Left(current->son[1]); delta -= root->val; if (root->val < current->val) { Add(1, 1, n, 1, current->pos - 1, delta); } } else { Find_Left(current->son[1]); delta -= root->val; if (root->val < current->val) { Splay(current, 0); Find_Right(current->son[0]); Add(1, 1, n, root->pos, current->pos - 1, delta); } } } while (true) { Splay(current, 0); rank = current->son[0]->size + 1; if (rank == 2) { break; } Find(rank - 1, 0); if (root->val > current->val) { break; } delta = current->val - root->val; last = root->pos - 1; if (rank == 3) { Add(1, 1, n, 1, last, delta); } else { Find(rank - 2, 0); Add(1, 1, n, root->pos, last, delta); } Find(rank - 2, 0); Find(rank, root); root->son[1]->son[0] = 0; Update(root->son[1]); Update(root); } } maxi = max(maxi, doing[i].z); if (doing[i - 1].x <= n) { best = min(best, doing[i - 1].x + Ask(1, 1, n, 1, n)); if (maxi <= n) { best = min(best, doing[i - 1].x + maxi); } } } printf("%d\n", best); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 300005; int n, m, ans, a[N], b[N], c[N], id[N], V[N], A[N], B[N], C[N]; multiset<int> res; set<pair<int, int> > H; bool cmp(const int i, const int j) { return A[i] > A[j]; } void ins(pair<int, int> z) { set<pair<int, int> >::iterator l, i, r = H.lower_bound(z); if (r->second >= z.second) return; i = H.insert(z).first, l = i, --l, res.erase(res.find(l->first + r->second)), res.insert(i->first + r->second); while (l->second < i->second) r = l--, res.erase(res.find(l->first + r->second)), H.erase(r); res.insert(l->first + i->second); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", a + i), V[m++] = a[i]; for (int i = 0; i < n; ++i) scanf("%d", b + i), V[m++] = b[i]; for (int i = 0; i < n; ++i) scanf("%d", c + i), V[m++] = c[i]; sort(V, V + m), m = unique(V, V + m) - V; for (int i = 0; i < n; ++i) a[i] = lower_bound(V, V + m, a[i]) - V; for (int i = 0; i < n; ++i) b[i] = lower_bound(V, V + m, b[i]) - V; for (int i = 0; i < n; ++i) c[i] = lower_bound(V, V + m, c[i]) - V; for (int i = 0; i < m; ++i) A[i] = B[i] = C[i] = 3 * n; for (int i = 0; i < n; ++i) if (A[a[i]] > n) A[a[i]] = i + 1; for (int i = 0; i < n; ++i) if (B[b[i]] > n) B[b[i]] = i + 1; for (int i = 0; i < n; ++i) if (C[c[i]] > n) C[c[i]] = i + 1; for (int i = 0; i < m + 1; ++i) id[i] = i; A[m] = 0, sort(id, id + m, cmp); H.insert(make_pair(0, 4 * n)), H.insert(make_pair(4 * n, 0)), res.insert(0), ans = A[*id]; for (int i = 0; i < m; ++i) ins(make_pair(B[id[i]], C[id[i]])), ans = min(ans, A[id[i + 1]] + *res.begin()); printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int Maxn = 1 << 30; int n; struct Node { int v, id, from; } seq[1001000]; struct NNode { int v[4]; void init() { v[1] = v[2] = v[3] = 3 * n; } } whe[1001000]; struct NNNode { int x, y; }; int a[4][100100], tot, cnt; set<NNNode> F; multiset<int> sum; bool operator<(NNNode x, NNNode y) { return x.x < y.x || (x.x == y.x && x.y < y.y); } bool cmp1(Node x, Node y) { return x.v < y.v; } bool cmp(NNode x, NNode y) { return x.v[1] > y.v[1]; } void insert(int x) { set<NNNode>::iterator s1, s2, s3; NNNode inwho = (NNNode){whe[x].v[2], whe[x].v[3]}; F.insert(inwho); s1 = F.find(inwho); s3 = s1; s3++; if ((*s3).x >= (*s1).x && (*s3).y >= (*s1).y) { F.erase(inwho); return; } s2 = s1; s2--; s3 = s1; s3++; if (sum.find((*s3).y + (*s2).x) != sum.end()) sum.erase(sum.find((*s3).y + (*s2).x)); while (true) { s1 = F.find(inwho); s2 = s1; s2--; if (!((*s2).x <= (*s1).x && (*s2).y <= (*s1).y)) break; s3 = s2; s3--; if (sum.find((*s2).y + (*s3).x) != sum.end()) sum.erase(sum.find((*s2).y + (*s3).x)); F.erase(s2); } s1 = F.find(inwho); s3 = s1; s3++; sum.insert((*s1).x + (*s3).y); s2 = s1; s2--; sum.insert((*s2).x + (*s1).y); } void work() { sort(whe + 1, whe + cnt + 1, cmp); F.insert((NNNode){3 * n + 2, 0}); F.insert((NNNode){0, 3 * n + 2}); sum.insert(0); int l = 1, ans = Maxn; for (int u = n; u >= 0; u--) { while (whe[l].v[1] > u) insert(l), l++; ans = min(ans, u + (*sum.begin())); } cout << ans; } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[1][i]), seq[++tot] = (Node){a[1][i], i, 1}; for (int i = 1; i <= n; i++) scanf("%d", &a[2][i]), seq[++tot] = (Node){a[2][i], i, 2}; for (int i = 1; i <= n; i++) scanf("%d", &a[3][i]), seq[++tot] = (Node){a[3][i], i, 3}; sort(seq + 1, seq + tot + 1, cmp1); a[seq[1].from][seq[1].id] = 1; cnt++; whe[1].init(); whe[cnt].v[seq[1].from] = seq[1].id; for (int i = 2; i <= tot; i++) { if (seq[i].v != seq[i - 1].v) cnt++, whe[cnt].init(); a[seq[i].from][seq[i].id] = cnt; whe[cnt].v[seq[i].from] = min(whe[cnt].v[seq[i].from], seq[i].id); } work(); }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:64000000") const int infi = 1e9 + 7; const long long infl = 1e18 + 7; long long get(const pair<long long, long long> &a) { return a.first + a.second; } struct diag_comp { bool operator()(const pair<long long, long long> &a, const pair<long long, long long> &b) const { long long s = get(a) - get(b); return s < 0 || s == 0 && a.first < b.first || s == 0 && a.first == b.first && a.second < b.second; } }; struct compx { bool operator()(const pair<long long, long long> &a, const pair<long long, long long> &b) const { return a.first < b.first || a.first == b.first && a.second > b.second; } }; struct compy { bool operator()(const pair<long long, long long> &a, const pair<long long, long long> &b) const { return a.second > b.second || a.second == b.second && a.first < b.first; } }; set<pair<long long, long long>, diag_comp> cur_point; set<pair<long long, long long>, compx> px; set<pair<long long, long long>, compy> py; long long get() { return get(*cur_point.begin()); } void add_new_point(pair<long long, long long> a) { cur_point.insert(a); px.insert(a); py.insert(a); return; } void add_point(pair<long long, long long> a) { pair<long long, long long> p1 = *py.upper_bound(make_pair(-infl, a.second)); pair<long long, long long> p2 = *(--px.upper_bound(make_pair(a.first, -infl))); if (p2.first < p1.first || p2.second > p1.second) return; vector<pair<long long, long long> > tmp; for (auto it = py.find(p1); *it != p2; ++it) tmp.push_back(*it); tmp.push_back(p2); for (int i = 0; i < ((int)tmp.size()); ++i) { py.erase(tmp[i]); px.erase(tmp[i]); cur_point.erase(tmp[i]); } pair<long long, long long> p = make_pair(p1.first, p2.second); add_new_point(make_pair(p.first, a.second)); add_new_point(make_pair(a.first, p.second)); return; } vector<int> all; void norm(vector<int> &all) { vector<int> tmp; sort(all.begin(), all.end()); if (all.size()) tmp.push_back(all[0]); for (int i = 1; i < (int)all.size(); ++i) { if (all[i] != all[i - 1]) tmp.push_back(all[i]); } all = tmp; return; } vector<vector<long long> > dp; int ar[3][100500]; vector<pair<long long, long long> > sob; int main() { cin.sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < (3); ++i) for (int j = 0; j < (n); ++j) cin >> ar[i][j]; for (int i = 0; i < (3); ++i) for (int j = 0; j < (n); ++j) all.push_back(ar[i][j]); norm(all); int m = all.size(); dp.resize(m); for (int i = 0; i < (m); ++i) dp[i].resize(3, infl / 100); for (int i = 0; i < (3); ++i) { for (int j = 0; j < (n); ++j) { int a = ar[i][j]; int k = lower_bound(all.begin(), all.end(), a) - all.begin(); dp[k][i] = min(dp[k][i], (long long)j); } } for (int i = 0; i < (m); ++i) { sob.push_back(make_pair(dp[i][0], i)); } sort(sob.begin(), sob.end()); reverse(sob.begin(), sob.end()); long long ans = n * 3; add_new_point(make_pair(0, 0)); int j = 0; for (int i = n - 1; i >= -1; --i) { for (; j < (int)sob.size() && sob[j].first > i; ++j) { int k = sob[j].second; add_point(make_pair(dp[k][1] + 1, dp[k][2] + 1)); } ans = min(ans, i + 1 + get()); } cout << ans; return 0; }