text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, ing[N], hv[N], gamesToPlay[N]; bool check(long long int games) { long long int canMod = 0; for (int i = 0; i < n; i++) { canMod += (games - gamesToPlay[i]); if (canMod >= games) { return true; } } return false; } int main() { cin >> n; int mx = 0; for (int i = 0; i < n; i++) { cin >> gamesToPlay[i]; mx = max(mx, gamesToPlay[i]); } long long int ans = 1e16; long long int l = mx, r = 1e16; while (l <= r) { long long int mid = (l + r) / 2; if (check(mid)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[100001]; unsigned long long n, sum; bool valid(unsigned long long mid) { if (a[0] > mid) return 0; else if (a[0] <= mid) { unsigned long long x = a[0] * (n - 2) + (mid - a[0]) * (n - 1); return (x >= sum); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); reverse(a, a + n); for (int i = 1; i < n; i++) sum += a[i]; unsigned long long s = 0, e = 5e15; while (s < e) { unsigned long long mid = (s + (e - s) / 2); if (valid(mid)) e = mid; else s = mid + 1; } cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, s = 0, x, m = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; s += x; m = max(m, x); } long long t = s / (n - 1); if (s % (n - 1)) t++; cout << max(m, t); }
#include <bits/stdc++.h> int main() { long long n; std::cin >> n; std::vector<long long> a(n, 0); long long sum = 0, max = 0; for (int i = 0; i < n; ++i) { std::cin >> a[i]; sum += a[i]; if (a[i] > max) { max = a[i]; } } long long ans = std::ceil(1. * (sum) / (n - 1)); std::cout << std::max(ans, max) << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = (1e5) + 10; long long n, sum, mx, tmp; int main() { scanf("%lld", &n); for (int i = 0; i < n; i++) { scanf("%lld", &tmp); mx = max(tmp, mx); sum += tmp; } long long ans = ceil(1.0 * sum / (n - 1)); ans = max(mx, ans); printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int N; long long arr[100004]; bool valid(long long r) { long long s = 0; for (int i = 0; i < N; ++i) s += (r - arr[i]); return s >= r; } int main() { cin >> N; long long lo = 0, hi = (1e9) * (1e5) + 100; for (int i = 0; i < N; ++i) { cin >> arr[i]; lo = max(lo, arr[i]); } while (lo < hi) { long long mid = lo + ((hi - lo) >> 1ll); if (valid(mid)) hi = mid; else lo = mid + 1; } cout << lo << endl; }
#include <bits/stdc++.h> long long int dif(long long int a, long long int b) { if (a >= b) { a -= b; } return a; } long long int power(long long int a, long long int b) { long long int z = 1; while (b) { if (b & 1) { z *= a; z %= 1000000007; } a *= a; a %= 1000000007; b /= 2; } return z % 1000000007; } using namespace std; template <typename Arg1> void __f(Arg1&& arg1) { cout << fixed << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(Arg1&& arg1, Args&&... args) { cout << fixed << arg1 << " "; __f(args...); } template <typename Arg1> void __g(Arg1&& arg1) { cin >> arg1; } template <typename Arg1, typename... Args> void __g(Arg1&& arg1, Args&&... args) { cin >> arg1; __g(args...); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; __g(n); long long int sum = 0, x, mx = -1; for (long long int i = 0; i < n; ++i) { __g(x); mx = max(mx, x); sum += x; } cout << fixed << max(mx, (long long int)ceil((sum * 1.0) / (n - 1) * 1.0)) << "\n"; ; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 1e9 + 7; int run_test() { ll n; cin >> n; vector<ll> v(n); ll maxi = 0; ll sum = 0; for (int i = 0; i < n; i++) { cin >> v[i]; maxi = max(maxi, v[i]); sum += v[i]; } cout << max(maxi, (sum + n - 2) / (n - 1)) << "\n"; return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt; tt = 1; while (tt--) { run_test(); } }
#include <bits/stdc++.h> using namespace std; vector<long long> numgg(long n) { vector<long long> vec(n + 1, 0); for (int i = 2; i <= n; i++) for (int k = i; k <= n; k += i) vec[k]++; return vec; } void speed() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } vector<long long> dvisors(long long n) { vector<long long> vec; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) vec.push_back(i); if (n % i == 0 && n % (n / i) == 0) vec.push_back((n / i)); } return vec; } int dp[100000]; const int max_n = 2e5 + 10; int cnt[26]; int main() { speed; long long arr[max_n]; long long n, sum = 0; cin >> n; long long x[n]; for (int i = 0; i < n; i++) { cin >> x[i]; sum += x[i]; } sort(x, x + n); long long v = x[n - 1]; n--; if (ceil((long double)sum / n) >= v) cout << (long long)ceil((long double)sum / n); else cout << v; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, m = 0, i, z = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a; z += a; if (a > m) m = a; } a = z / (n - 1); if (z % (n - 1) != 0) a++; cout << max(a, m); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/stack:64000000") using namespace std; const double PI = 2 * acos(0.0); const double eps = 1e-9; template <class T> T jog(T a, T b) { return a + b; } template <class T> T bog(T a, T b) { return a - b; } template <class T> T gon(T a, T b) { return a * b; } template <class T> T sq(T x) { return x * x; } template <class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <class T> T lcm(T a, T b) { return (a / gcd(a, b)) * b; } template <class T> T power(T a, T p) { T res = 1, x = a; while (p) { if (p & 1) res = res * x; x = x * x; p >>= 1; } return res; } template <class T> T cordinatlenth(T a, T b, T c, T d) { return sqrt(sq(a - c) + sq(b - d)); } template <class T> string toString(T n) { ostringstream oss; oss << n; oss.flush(); return oss.str(); } int toInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; } long long bigmod(long long a, long long p, long long mod) { long long res = 1, x = a; while (p) { if (p & 1) res = (res * x) % mod; x = (x * x) % mod; p >>= 1; } return res; } int diraction1[] = {-1, 0, 0, 1, 1, -1, -1, 1}; int diraction2[] = {0, -1, 1, 0, 1, -1, 1, -1}; int horsed1[] = {-2, -2, -1, 1, 2, 2, 1, -1}; int horsed2[] = {1, -1, -2, -2, -1, 1, 2, 2}; void Input(); const int INF = 2147483647; const long long LINF = 9223372036854775807ll; bool isok(long long x); int n, in[100010], tmp[100010]; int main() { Input(); return 0; } void Input() { cin >> n; long long sum = 0; for (int i = 0; i < n; i++) cin >> in[i], sum += in[i]; long long ans = sum / (n - 1); ans += (bool)(sum % (n - 1)); for (int i = 0; i < n; i++) ans = ans < in[i] ? in[i] : ans; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 20; long long a[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, sum = 0, mx = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; mx = max(mx, a[i]); sum += a[i]; } cout << max((long long)((sum + n - 2) / (n - 1)), mx) << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100000; int main(void) { long long int sum, Max, a[N], i, n, m; scanf("%lld", &n); Max = -1; sum = 0; for (i = 0; i < n; i++) { scanf("%lld", &a[i]); if (Max == -1 || a[i] > Max) { Max = a[i]; } sum += a[i]; } m = sum / (n - 1); if (m * (n - 1) < sum) { m++; } if (m < Max) { m = Max; } printf("%lld\n", m); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long sum = 0, ai, maxa = 0, n; cin >> n; for (int i = 0; i < n; i++) { cin >> ai; sum += ai; if (ai > maxa) maxa = ai; } cout << ((maxa > (long long)ceil((double)sum / (double)(n - 1))) ? maxa : (long long)ceil((double)sum / (double)(n - 1))) << endl; return 0; }
#include <bits/stdc++.h> int main() { int cantidad; long long int i; long long int minimo_rondas = 0; long long int suma = 0; scanf("%d", &cantidad); int jugadores[cantidad]; for (i = 0; i < cantidad; i++) { scanf("%d", &jugadores[i]); suma += jugadores[i]; if (jugadores[i] > minimo_rondas) minimo_rondas = jugadores[i]; } for (i = minimo_rondas; i * (cantidad - 1) < suma; i++) ; printf("\n%d", i); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int sum = 0; long long int maxi = INT_MIN; for (long long int i = 0; i < n; i++) { long long int x; cin >> x; sum += x; maxi = max(maxi, x); } if (maxi <= ceil(sum / (n - 1 * 1.0))) cout << (long long int)(ceil(sum / (n - 1 * 1.0))) << endl; else cout << (long long int)maxi << endl; }
#include <bits/stdc++.h> using namespace std; bool isPrime(long long int n) { if (n <= 1) return 0; if (n <= 3) return 1; if (n % 2 == 0 || n % 3 == 0) return 0; for (long long int i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return 0; return 1; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return (a * b) / (gcd(a, b)); } void swap(int& a, int& b) { a = a ^ b; b = a ^ b; a = a ^ b; } inline void solve() { long long int a, b; cin >> a >> b; int x = 0, y = 0, z = 0; while (a % 2 == 0) { a = a / 2; x++; } while (a % 3 == 0) { a = a / 3; y++; } while (a % 5 == 0) { a = a / 5; z++; } while (b % 2 == 0) { b = b / 2; x--; } while (b % 3 == 0) { b = b / 3; y--; } while (b % 5 == 0) { b = b / 5; z--; } if (a != b) cout << -1 << '\n'; else cout << abs(x) + abs(y) + abs(z) << '\n'; } signed main() { auto start_time = clock(); cerr << setprecision(3) << fixed; cout << setprecision(15) << fixed; ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); auto end_time = clock(); cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, count = 0; cin >> a >> b; if (a == b) { cout << 0; return 0; } bool flag = true; long long a1 = 0, b1 = 0, c1 = 0, a2 = 0, b2 = 0, c2 = 0, x = 1, y = 1; while (flag) { if (a % 2 == 0) { a1++; a /= 2; } else if (a % 3 == 0) { b1++; a /= 3; } else if (a % 5 == 0) { c1++; a /= 5; } else { x = a; flag = false; } } bool f = true; while (f) { if (b % 2 == 0) { a2++; b /= 2; } else if (b % 3 == 0) { b2++; b /= 3; } else if (b % 5 == 0) { c2++; b /= 5; } else { y = b; f = false; } } if (x != y) { cout << -1; } else { cout << abs(a1 - a2) + abs(b1 - b2) + abs(c1 - c2); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a, b; cin >> a >> b; int cnt = 0; int x = a, y = b; pair<int, pair<int, int>> ad, bd; while (a % 2 == 0) { a /= 2; ++ad.first; } while (a % 3 == 0) { a /= 3; ++ad.second.first; } while (a % 5 == 0) { a /= 5; ++ad.second.second; } while (b % 2 == 0) { b /= 2; ++bd.first; } while (b % 3 == 0) { b /= 3; ++bd.second.first; } while (b % 5 == 0) { b /= 5; ++bd.second.second; } if (a != b) return cout << -1, 0; else { return cout << (abs(ad.first - bd.first) + abs(ad.second.first - bd.second.first) + abs(ad.second.second - bd.second.second)), 0; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 6, MAX = 2e5 + 5, OO = 1000000009; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long x, long long y) { return (x * y / gcd(x, y)); } long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); } int a, b, aa, bb, ans; void Check(int p) { while (a % p == 0) { a /= p; aa++; } while (b % p == 0) { b /= p; bb++; } ans += abs(aa - bb); aa = bb = 0; } int main() { scanf("%d %d", &a, &b); Check(2); Check(3); Check(5); return printf("%d", a == b ? ans : -1), 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18 + 42; template <typename T> void pop_front(std::vector<T> &vec) { assert(!vec.empty()); vec.erase(vec.begin()); } bool compPairF(pair<long long, long long> v1, pair<long long, long long> v2) { return v1.first < v2.first; } bool compPairS(pair<long long, long long> v1, pair<long long, long long> v2) { return v1.second < v2.second; } long long gcd(long long a, long long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } long long power(long long x, long long y) { long long temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } vector<long long> primefactorisation(long long n) { vector<long long> ans; long long t = 2; while (n > 1) { if (n % t == 0) { ans.push_back(t); n = n / t; } else { t++; } } return ans; } void sol() { long long n, m; cin >> n >> m; long long ans = 0; long long t = 2; while (n > 1 || m > 1) { if (t > 5 && m != n) { cout << -1 << endl; return; } if (n == m) { break; } if (n % t == 0 && m % t == 0) { n = n / t; m = m / t; } else { if (n % t != 0 && m % t != 0) t++; else if (t > 5) { cout << -1 << endl; return; } else if (n % t == 0) { n = n / t; ans++; } else if (m % t == 0) { m = m / t; ans++; } else { t++; } } } if (n != m) { cout << -1 << endl; return; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int a = 1; while (a--) { sol(); } }
#include <bits/stdc++.h> using namespace std; int nok(int a, int b) { while (a != b) { if (a >= b) a = a - b; else b = b - a; } return min(a, b); } int main() { int a, b; cin >> a >> b; int k = nok(a, b); a = a / k; b = b / k; int t1 = 0, t2 = 0, t; while (a % 2 == 0) { a = a / 2; t1++; } while (a % 3 == 0) { a = a / 3; t1++; } while (a % 5 == 0) { a = a / 5; t1++; } while (b % 2 == 0) { b = b / 2; t2++; } while (b % 3 == 0) { b = b / 3; t2++; } while (b % 5 == 0) { b = b / 5; t2++; } if (a == 1 && b == 1) { t = t1 + t2; } else { t = -1; } cout << t; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long x, y, ans = 0; cin >> x >> y; int arr[3] = {2, 3, 5}; for (int i = 0; i < 3; i++) { int c1 = 0, c2 = 0; while (x % arr[i] == 0) { x /= arr[i]; c1++; } while (y % arr[i] == 0) { y /= arr[i]; c2++; } ans += max(c1, c2) - min(c1, c2); } if (x == y) cout << ans << endl; else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (x < y) return gcd(y, x); else if (x % y == 0) return y; return gcd(y, x % y); } int js(int n) { int s = 0; if (n % 2 == 0) { s += 1; n /= 2; while (n % 2 == 0) { n /= 2; s += 1; } } if (n % 3 == 0) { s += 1; n /= 3; while (n % 3 == 0) { n /= 3; s += 1; } } if (n % 5 == 0) { s += 1; n /= 5; while (n % 5 == 0) { n /= 5; s += 1; } } if (n == 1) return s; return 0; } int main() { int i, k1, k2, a, b, n1, n2, s; while (scanf("%d%d", &a, &b) != EOF) { if (a == b) { printf("0\n"); continue; } i = gcd(a, b); s = 0; a = a / i; b = b / i; if (a != 1 && b != 1) { k1 = js(a); k2 = js(b); if (k1 != 0 && k2 != 0) { printf("%d\n", k1 + k2); continue; } else { printf("-1\n"); continue; } } if (a == 1) s = js(b); else if (b == 1) s = js(a); if (s) { printf("%d\n", s); continue; } else { printf("-1\n"); continue; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 5, MOD = 1e9 + 7; map<pair<int, int>, bool> m; int main() { int a, b; scanf("%d %d", &a, &b); int cnt = 0; queue<pair<pair<int, int>, int> > q; q.push({{a, b}, 0}); while (!q.empty()) { auto a = q.front(); q.pop(); if (m[{a.first.first, a.first.second}]) continue; m[{a.first.first, a.first.second}] = 1; if (a.first.first == a.first.second) return cout << a.second, 0; if (a.first.first % 2 == 0 && !m[{a.first.first / 2, a.first.second}]) q.push({{a.first.first / 2, a.first.second}, a.second + 1}); if (a.first.first % 3 == 0 && !m[{a.first.first / 3, a.first.second}]) q.push({{a.first.first / 3, a.first.second}, a.second + 1}); if (a.first.first % 5 == 0 && !m[{a.first.first / 5, a.first.second}]) q.push({{a.first.first / 5, a.first.second}, a.second + 1}); if (a.first.second % 2 == 0 && !m[{a.first.first, a.first.second / 2}]) q.push({{a.first.first, a.first.second / 2}, a.second + 1}); if (a.first.second % 3 == 0 && !m[{a.first.first, a.first.second / 3}]) q.push({{a.first.first, a.first.second / 3}, a.second + 1}); if (a.first.second % 5 == 0 && !m[{a.first.first, a.first.second / 5}]) q.push({{a.first.first, a.first.second / 5}, a.second + 1}); } printf("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; const double eps = (1e-9); const int MOD = 1000000007; int dcmp(double x, double y) { return fabs(x - y) <= eps ? 0 : x > y ? -1 : 1; } int findPow(int &x, int exp) { int ret = 0; while (x % exp == 0) { x /= exp; ret++; } return ret; } int main() { ios_base::sync_with_stdio(false); int a, b; cin >> a >> b; int arr[2] = {a, b}; int p[2][3]; for (int i = 0; i < 2; ++i) { p[i][0] = findPow(arr[i], 2); p[i][1] = findPow(arr[i], 3); p[i][2] = findPow(arr[i], 5); } if (arr[0] != arr[1]) cout << -1 << endl; else { int ans = 0; for (int i = 0; i < 3; ++i) { ans += abs(p[0][i] - p[1][i]); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int a, b; int ans = 0; bool ok = true; int gcd(int a, int b) { if (b == 0) return a; else return gcd(b, a % b); } void solve(int a) { while (a % 2 == 0) { a /= 2; ans++; } while (a % 3 == 0) { a /= 3; ans++; } while (a % 5 == 0) { a /= 5; ans++; } if (a != 1) ok = false; } int main() { scanf("%d%d", &a, &b); int x = gcd(a, b); a = a / x; b = b / x; solve(a); solve(b); if (ok) printf("%d\n", ans); else puts("-1"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b; cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } long long ans = 0; for (int i = 5; i >= 2; i--) { long long a1 = 0, b1 = 0; if (i != 4) { while (a % i == 0) { a1++; a /= i; } while (b % i == 0) { b1++; b /= i; } ans += abs(a1 - b1); } } if (a == b) cout << ans << endl; else cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b; cin >> a >> b; long long int no2a = 0, no3a = 0, no5a = 0, no2b = 0, no3b = 0, no5b = 0; if (a == b) { cout << 0; exit(0); } while (a % 2 == 0) { no2a++; a /= 2; } while (a % 3 == 0) { no3a++; a /= 3; } while (a % 5 == 0) { no5a++; a /= 5; } while (b % 2 == 0) { no2b++; b /= 2; } while (b % 3 == 0) { no3b++; b /= 3; } while (b % 5 == 0) { no5b++; b /= 5; } if (a != b) { cout << -1; exit(0); } cout << abs(no2a - no2b) + abs(no3a - no3b) + abs(no5a - no5b); return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a < b) return (gcd(b, a)); if (b == 0) return a; return gcd(b, a % b); } int ff(int a, int i) { int c = 0; while (a % i == 0) { a = a / i; c++; } return (c); } int main() { long long a, b, x, y, n2, n22, n3, n33, n5, n55, ans; cin >> a >> b; n2 = ff(a, 2); n22 = ff(b, 2); n3 = ff(a, 3); n33 = ff(b, 3); n5 = ff(a, 5); n55 = ff(b, 5); x = (a / (pow(2, n2) * pow(3, n3) * pow(5, n5))); y = (b / (pow(2, n22) * pow(3, n33) * pow(5, n55))); if (x != y) { cout << -1; } else { ans = abs(n2 - n22) + abs(n3 - n33) + abs(n5 - n55); cout << ans; } }
#include <bits/stdc++.h> using namespace std; int ans = 0; void eq(int *a, int *b, int num) { if (*a == *b) { return; } int op1 = 0, op2 = 0; while (*a % num == 0) { *a = *a / num; op1++; } while (*b % num == 0) { *b = *b / num; op2++; } ans += abs(op1 - op2); } int main() { int a, b; scanf("%d %d", &a, &b); eq(&a, &b, 5); eq(&a, &b, 3); eq(&a, &b, 2); if (a == b) { printf("%d\n", ans); } else printf("-1\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (a % b == 0) return b; else return gcd(b, a % b); } 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 p = gcd(max(a, b), min(a, b)); a = a / p; b = b / p; int ans = 0; while (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) { if (a % 2 == 0) { a /= 2; ans++; } if (a % 3 == 0) { a /= 3; ans++; } if (a % 5 == 0) { a /= 5; ans++; } } while (b % 2 == 0 || b % 3 == 0 || b % 5 == 0) { if (b % 2 == 0) { b /= 2; ans++; } if (b % 3 == 0) { b /= 3; ans++; } if (b % 5 == 0) { b /= 5; ans++; } } if (a == 1 && b == 1) cout << ans; else cout << "-1\n"; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } int main() { int m, n; while (scanf("%d%d", &m, &n) != EOF) { int t = gcd(m, n); int a = m / t, b = n / t; 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 == 1 && b == 1) cout << ans << endl; else cout << -1 << endl; } }
#include <bits/stdc++.h> using namespace std; set<pair<int, int> > st; map<pair<int, int>, int> mp; queue<pair<int, int> > q; int main() { int a, b; cin >> a >> b; q.push(make_pair(a, b)); st.insert(make_pair(a, b)); mp[make_pair(a, b)] = 0; while (!q.empty()) { pair<int, int> nows = q.front(); int step = mp[nows]; int x = nows.first, y = nows.second; q.pop(); if (x == y) { cout << step << endl; return 0; } pair<int, int> np; if (x % 2 == 0) { np = make_pair(x / 2, y); if (st.find(np) == st.end()) { mp[np] = step + 1; st.insert(np); q.push(np); } } if (x % 3 == 0) { np = make_pair(x / 3, y); if (st.find(np) == st.end()) { mp[np] = step + 1; st.insert(np); q.push(np); } } if (x % 5 == 0) { np = make_pair(x / 5, y); if (st.find(np) == st.end()) { mp[np] = step + 1; st.insert(np); q.push(np); } } if (y % 2 == 0) { np = make_pair(x, y / 2); if (st.find(np) == st.end()) { mp[np] = step + 1; st.insert(np); q.push(np); } } if (y % 3 == 0) { np = make_pair(x, y / 3); if (st.find(np) == st.end()) { mp[np] = step + 1; st.insert(np); q.push(np); } } if (y % 5 == 0) { np = make_pair(x, y / 5); if (st.find(np) == st.end()) { mp[np] = step + 1; st.insert(np); q.push(np); } } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> ostream &operator<<(ostream &O, pair<T1, T2> &t) { return O << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream &operator<<(ostream &O, vector<T> &t) { for (int _ = 0; _ < (int)t.size(); ++_) O << t[_] << " "; return O; } int res = (2147483647); void rec(int a, int b, int c) { if (a == b) { res = min(res, c); return; } if (c >= res) return; if (a < b) swap(a, b); if ((a & 1) == 0) rec(a >> 1, b, c + 1); if (a % 3 == 0) rec(a / 3, b, c + 1); if (a % 5 == 0) rec(a / 5, b, c + 1); } int main() { int a, b; cin >> a >> b; rec(a, b, 0); cout << (res == (2147483647) ? -1 : res); return 0; } int cpp4cf_main() { freopen("B.cpp", "r", stdin); char s[99]; bool f; while (true) { cin >> s; if (cin.eof()) break; if (strstr(s, "/*")) { cin >> s; if (strstr(s, "Test")) { cin >> s; if (strstr(s, "on")) { cout << "Output: "; cpp4cf_main(); cout << "\nAnswer: "; f = false; while (true) { cin >> s; if (strstr(s, "*/")) break; if (strstr(s, "//")) { if (f) cout << endl; else f = true; } else cout << s << " "; } cout << "\n\n"; } } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e9 + 55; const int inf = 1e9 + 77; const int MOD = 1e9 + 7; const double PI = acos(-1.0); const double eps = 1e-7; vector<int> primes; bitset<int(1e7) + 2> isprime; void seive() { isprime[0] = isprime[1] = 1; for (int i = 2; i < int(1e6); ++i) { if (!isprime.test(i)) { primes.push_back(i); for (int j = i * 2; j < int(1e6) + 2; j += i) { isprime.set(j); } } } } const int n = 1e6 + 55; int p[2][n]; bool check(int n) { if (n < int(1e6)) return !isprime.test(n); for (auto i : primes) if (n % i == 0) return false; return true; } void factor(int a, int f) { long long id = 0, pf = primes[0]; while (pf * pf <= a) { while (a % pf == 0) { a /= pf; p[f][pf]++; } pf = primes[++id]; } if (a != 1) p[f][a]++; } int main() { seive(); int a, b; scanf("%d", &a); scanf("%d", &b); bool t1 = true; if (!check(a)) { factor(a, 0); t1 = false; } bool t2 = true; if (!check(b)) { factor(b, 1); t2 = false; } if (t1 && t2) { printf("-1"); return 0; } if (t1 || t2) { if (t1) { if (t1 > int(1e6)) { printf("-1"); return 0; } } else { if (t2 > int(1e6)) { printf("-1"); return 0; } } } if (t1) factor(a, 0); if (t2) factor(b, 1); int res1 = 0; for (int i = 7; i < n; ++i) { if (abs(p[1][i] - p[0][i]) > 0) { res1 = -1; break; } } if (res1 == -1) { printf("-1"); return 0; } for (int i = 2; i <= 5; ++i) { res1 += abs(p[0][i] - p[1][i]); } printf("%d", res1); return 0; }
#include <bits/stdc++.h> using namespace std; int findhcf(int a, int b) { int r; if (a < b) { int tmp = a; a = b; b = tmp; } while (b > 0) { r = a % b; a = b; b = r; } return a; } int findNumFactor(int *num, int fac) { int num_factors = 0; while ((*num) % fac == 0) { num_factors++; *num /= fac; } return num_factors; } int findMinOperations(int w1, int w2) { int a2 = findNumFactor(&w1, 2); int a3 = findNumFactor(&w1, 3); int a5 = findNumFactor(&w1, 5); int num_operations_w1 = a2 + a3 + a5; int b2 = findNumFactor(&w2, 2); int b3 = findNumFactor(&w2, 3); int b5 = findNumFactor(&w2, 5); int num_operations_w2 = b2 + b3 + b5; if ((w1 == 1 && w2 == 1) || w1 == w2) { return num_operations_w1 + num_operations_w2; } return -1; } int main() { int w1, w2; scanf("%d %d", &w1, &w2); int hcf = findhcf(w1, w2); int minOps = findMinOperations(w1 / hcf, w2 / hcf); cout << minOps << endl; }
#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; cin >> a >> b; if (a == b) { cout << 0; return 0; } int c = gcd(a, b); int ans = 0; for (int i = 0; i < 1; i++) { while ((a / c) % 2 == 0) { a /= 2; ans++; } while ((a / c) % 3 == 0) { a /= 3; ans++; } while ((a / c) % 5 == 0) { a /= 5; ans++; } while ((b / c) % 2 == 0) { b /= 2; ans++; } while ((b / c) % 3 == 0) { b /= 3; ans++; } while ((b / c) % 5 == 0) { b /= 5; ans++; } } if (a != b || a != c) cout << -1; else cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) { return a; } return gcd(b, a % b); } int func(int n) { int c = 0; for (int i = 2; i <= 5; i++) { if (n % i == 0) { while (n % i == 0) { n /= i; c++; } } } if (n != 1) { return -1; } else { return c; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; if (a == b) { cout << 0; exit(0); } int k = gcd(a, b); int a1 = a / k; int b1 = b / k; long long res = func(a1); long long res1 = func(b1); if (res == -1 || res1 == -1) { cout << -1; exit(0); } cout << res + res1; }
#include <bits/stdc++.h> using namespace std; int a1[3]; int main(int argc, char *argv[]) { int a, b; cin >> a >> b; int temp = 0, temp1 = 0, ans = 0; while (a % 2 == 0) { a /= 2; temp += 1; } while (b % 2 == 0) { b /= 2; temp1 += 1; } ans = max(temp1, temp) - min(temp1, temp); temp = temp1 = 0; while (a % 3 == 0) { a /= 3; temp += 1; } while (b % 3 == 0) { b /= 3; temp1 += 1; } ans += max(temp1, temp) - min(temp1, temp); temp = temp1 = 0; while (a % 5 == 0) { a /= 5; temp += 1; } while (b % 5 == 0) { b /= 5; temp1 += 1; } ans += max(temp1, temp) - min(temp1, temp); temp = temp1 = 0; if (a != b) { cout << -1 << endl; return 0; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long i, j, n, k, a[6], b[6], ans = 0, s, m; int main() { cin >> n >> m; while (n % 2 == 0) { n /= 2; a[2]++; } while (m % 2 == 0) { m /= 2; b[2]++; } while (n % 3 == 0) { n /= 3; a[3]++; } while (m % 3 == 0) { m /= 3; b[3]++; } while (n % 5 == 0) { n /= 5; a[5]++; } while (m % 5 == 0) { m /= 5; b[5]++; } if (n != m) cout << -1; else cout << abs(a[2] - b[2]) + abs(a[3] - b[3]) + abs(a[5] - b[5]); return 0; }
#include <bits/stdc++.h> using namespace std; int n, t, b, c; int sum[105][3], a[105]; int gcd(int a, int b) { while (b) { int t = a % b; a = b; b = t; } return a; } bool ok(int va) { while (va % 2 == 0) va /= 2; while (va % 3 == 0) va /= 3; while (va % 5 == 0) va /= 5; return va == 1; } int main() { int a, b; scanf("%d%d", &a, &b); if (a == b) printf("0\n"); else { int pre = gcd(a, b); int prea = a / pre; int preb = b / pre; if (ok(prea) && ok(preb)) { int sum = 0; while (prea % 2 == 0) { prea /= 2; sum++; } while (prea % 3 == 0) { prea /= 3; sum++; } while (prea % 5 == 0) { prea /= 5; sum++; } while (preb % 2 == 0) { preb /= 2; sum++; } while (preb % 3 == 0) { preb /= 3; sum++; } while (preb % 5 == 0) { preb /= 5; sum++; } printf("%d\n", sum); } else printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, a1 = 0, a2 = 0, a3 = 0, b1 = 0, b2 = 0, b3 = 0; cin >> a >> b; while (a % 2 == 0) { a = a / 2; a1++; } while (a % 3 == 0) { a = a / 3; a2++; } while (a % 5 == 0) { a = a / 5; a3++; } while (b % 2 == 0) { b = b / 2; b1++; } while (b % 3 == 0) { b = b / 3; b2++; } while (b % 5 == 0) { b = b / 5; b3++; } if (a != b) cout << -1 << endl; if (a == b) cout << abs(a1 - b1) + abs(a2 - b2) + abs(a3 - b3) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int total_operation = 0; bool is_prime(long long n) { if (n == 2) return true; if (n % 2 == 0 || n < 2) return false; for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } int countprime_in_range(int n) { vector<bool> isprime(n + 1, true); isprime[0] = isprime[1] = false; for (int i = 2; i * i <= n; ++i) { if (isprime[i]) { for (int j = 2 * i; j <= n; j += i) { isprime[j] = 0; } } } int cnt = 0; for (int i = 0; i <= n; i++) { if (isprime[i]) cnt++; } return cnt; } int main() { int fact1[3], fact2[3]; fact1[0] = fact1[1] = fact1[2] = 0; fact2[0] = fact2[1] = fact2[2] = 0; int A, B; cin >> A >> B; if (A == B) { cout << "0"; return 0; } while (A) { if (!(A % 2)) fact1[0]++, A /= 2; else if (!(A % 3)) fact1[1]++, A /= 3; else if (!(A % 5)) fact1[2]++, A /= 5; else break; } while (B) { if (!(B % 2)) fact2[0]++, B /= 2; else if (!(B % 3)) fact2[1]++, B /= 3; else if (!(B % 5)) fact2[2]++, B /= 5; else break; } if (A != B) cout << "-1"; else cout << abs(fact1[0] - fact2[0]) + abs(fact1[1] - fact2[1]) + abs(fact1[2] - fact2[2]); }
#include <bits/stdc++.h> using namespace std; int nn = 0; int poe(int num, int b) { nn = 0; while (num % b == 0) { nn++; num /= b; } return num; } int main() { int a, b; scanf("%d %d", &a, &b); if (a == b) { printf("0\n"); return 0; } int tempa, tempb; tempa = a; tempb = b; int a2 = 0, a5 = 0, a3 = 0, b2 = 0, b3 = 0, b5 = 0; tempa = poe(tempa, 2); a2 = nn; tempa = poe(tempa, 3); a3 = nn; tempa = poe(tempa, 5); a5 = nn; tempb = poe(tempb, 2); b2 = nn; tempb = poe(tempb, 3); b3 = nn; tempb = poe(tempb, 5); b5 = nn; if (tempa == tempb) { int ans = abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5); printf("%d\n", ans); } else { printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b; cin >> a >> b; long long x = 0, y = 0, f = 0, t = 0, tw = 0, f1 = 0, t1 = 0, tw1 = 0; long long p = 0, q = 0; while (a) { x = 0; if (a % 5 == 0) { f += 1; a /= 5; x = 1; } else if (a % 3 == 0) { t += 1; a /= 3; x = 1; } else if (a % 2 == 0) { tw += 1; a /= 2; x = 1; } if (x == 0) { p = a; break; } } while (b) { y = 0; if (b % 5 == 0) { f1 += 1; b /= 5; y = 1; } else if (b % 3 == 0) { t1 += 1; b /= 3; y = 1; } else if (b % 2 == 0) { tw1 += 1; b /= 2; y = 1; } if (y == 0) { q = b; break; } } if (p != q) cout << "-1" << "\n"; else { cout << abs(f - f1) + abs(t - t1) + abs(tw - tw1) << "\n"; } }
#include <bits/stdc++.h> using namespace std; int k = 0; int cmmdc(int a, int b) { int r; r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } int valid(int a, int b) { if (a == b) return 1; while (a % 2 == 0) a = a / 2; while (a % 3 == 0) a = a / 3; while (a % 5 == 0) a = a / 5; if (a != 1) return -1; while (b % 2 == 0) b = b / 2; while (b % 3 == 0) b = b / 3; while (b % 5 == 0) b = b / 5; if (b != 1) return -1; return 1; } int verifica(int a, int b) { if (a == b) return 0; if (a > b) { if (a % 2 == 0) { return 1 + verifica(a / 2, b); k++; } if (a % 3 == 0) { return 1 + verifica(a / 3, b); k++; } if (a % 5 == 0) { return 1 + verifica(a / 5, b); k++; } } else { if (b % 2 == 0) { return 1 + verifica(a, b / 2); k++; } if (b % 3 == 0) { return 1 + verifica(a, b / 3); k++; } if (b % 5 == 0) { return 1 + verifica(a, b / 5); } } return k; } int main() { int a, b, cmm, ok; cin >> a >> b; cmm = cmmdc(a, b); a = a / cmm; b = b / cmm; ok = valid(a, b); if (ok == 1) cout << verifica(a, b); else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; unsigned int gcd(unsigned int a, unsigned int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { unsigned int a, b; cin >> a >> b; unsigned int g = gcd(a, b); unsigned int ra = a / g, rb = b / g; unsigned int r = 0; while (ra % 2 == 0) ra /= 2, ++r; while (ra % 3 == 0) ra /= 3, ++r; while (ra % 5 == 0) ra /= 5, ++r; while (rb % 2 == 0) rb /= 2, ++r; while (rb % 3 == 0) rb /= 3, ++r; while (rb % 5 == 0) rb /= 5, ++r; bool ok = ra == 1 && rb == 1; if (ok) cout << r << endl; else cout << -1 << endl; return 0; }
#include <bits/stdc++.h> int main() { int a, b; int op = 0; int two[2] = {0}; int three[2] = {0}; int five[2] = {0}; int t1, t2; scanf("%d%d", &a, &b); while (a % 2 == 0) { two[0]++; a /= 2; } while (a % 3 == 0) { three[0]++; a /= 3; } while (a % 5 == 0) { five[0]++; a /= 5; } while (b % 2 == 0) { two[1]++; b /= 2; } while (b % 3 == 0) { three[1]++; b /= 3; } while (b % 5 == 0) { five[1]++; b /= 5; } if (a != b) op = -1; else { while (two[0] != 0 && two[1] != 0) { two[0]--; two[1]--; } while (three[0] != 0 && three[1] != 0) { three[0]--; three[1]--; } while (five[0] != 0 && five[1] != 0) { five[0]--; five[1]--; } op = two[0] + two[1] + three[0] + three[1] + five[0] + five[1]; } printf("%d", op); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; vector<long long> prime; bitset<1000030> vis; void seive() { vis.set(); vis[0] = false; vis[1] = false; vis[2] = true; prime.push_back(2); for (long long i = 3; i <= 1000030; i += 2) { if (vis[i] == 1) { prime.push_back(i); for (long long j = i * i; j <= 1000030; j += i) { vis[j] = 0; } } } } bool isPrime(long long n) { if (n < 2) return false; if (n < 4) return true; if (!(n & 1)) return false; if (n % 3 == 0) return false; long long lim = sqrt(n); for (long long i = 5; i <= lim; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } map<long long, long long> primeFact(long long n) { map<long long, long long> make_pair; while (n % 2 == 0) { make_pair[2]++; n = n / 2; } for (long long i = 3; i <= sqrt(n); i += 2) { while (n % i == 0) { make_pair[i]++; n = n / i; } } if (n > 2) { make_pair[n]++; } return make_pair; } long long binomialCoeff(long long n, long long k) { if (k > n) return 0; long long C[n + 1][k + 1]; long long i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = (C[i - 1][j - 1] % (1000000007) + C[i - 1][j] % (1000000007)) % (1000000007); } } return C[n][k]; } long long countDivisors(long long n) { long long cnt = 0; for (long long i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) cnt++; else { cnt += 2; } } } return cnt; } long long reverse_number(long long n) { long long rev = 0, r; while (n != 0) { r = n % 10; rev = rev * 10 + r; n /= 10; } return rev; } long long cnt[100005]; void solve() { long long a, b; cin >> a >> b; vector<long long> v1(6, 0), v2(6, 0); map<long long, long long> m1 = primeFact(a), m2 = primeFact(b); long long p1 = 1, p2 = 1; for (auto p : m1) { if (p.first == 2 || p.first == 5 || p.first == 3) { p1 *= pow(p.first, p.second); v1[p.first] = p.second; } } for (auto p : m2) { if (p.first == 2 || p.first == 5 || p.first == 3) { p2 *= pow(p.first, p.second); v2[p.first] = p.second; } } if (a / p1 != b / p2) { cout << "-1" << "\n"; return; } cout << abs(v1[2] - v2[2]) + abs(v1[3] - v2[3]) + abs(v1[5] - v2[5]) << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; class node { public: long long int val; int level; int id; node(long long int a, int i, int l = 1) { val = a; level = l; id = i; } }; int main() { queue<node> q; long long int a, b; cin >> a >> b; node a1(a, 1); node a2(b, 2); q.push(a1); q.push(a2); map<long long int, int> m1; map<long long int, int> m2; int ans = -1; while (!q.empty()) { node b = q.front(); q.pop(); if (b.id == 1) { m1[b.val] = b.level; if (m2[b.val] != 0) { ans = b.level + m2[b.val]; break; } if (b.val % 2 == 0) q.push(node(b.val / 2, b.id, b.level + 1)); if (b.val % 3 == 0) q.push(node(b.val / 3, b.id, b.level + 1)); if (b.val % 5 == 0) q.push(node(b.val / 5, b.id, b.level + 1)); } else if (b.id == 2) { m2[b.val] = b.level; if (m1[b.val] != 0) { ans = b.level + m1[b.val]; break; } if (b.val % 2 == 0) q.push(node(b.val / 2, b.id, b.level + 1)); if (b.val % 3 == 0) q.push(node(b.val / 3, b.id, b.level + 1)); if (b.val % 5 == 0) q.push(node(b.val / 5, b.id, b.level + 1)); } } if (ans == -1) cout << ans << endl; else { cout << ans - 2 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = (int)1073741824; const int OO = (int)1e9; int main(void) { int a, b; while (cin >> a >> b) { int cnt1_2 = 0, cnt1_3 = 0, cnt1_5 = 0, cnt2_2 = 0, cnt2_3 = 0, cnt2_5 = 0; while (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) { if (a % 2 == 0) { a /= 2; cnt1_2++; } if (a % 3 == 0) { a /= 3; cnt1_3++; } if (a % 5 == 0) { a /= 5; cnt1_5++; } } while (b % 2 == 0 || b % 3 == 0 || b % 5 == 0) { if (b % 2 == 0) { b /= 2; cnt2_2++; } if (b % 3 == 0) { b /= 3; cnt2_3++; } if (b % 5 == 0) { b /= 5; cnt2_5++; } } if (a != b) cout << "-1\n"; else cout << abs(cnt1_2 - cnt2_2) + abs(cnt1_3 - cnt2_3) + abs(cnt1_5 - cnt2_5) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a, b, A[6], B[6]; int main() { cin >> a >> b; for (n = 2; n < 6; n++) { while (a % n == 0) { a /= n; A[n]++; } while (b % n == 0) { b /= n; B[n]++; } } int res = (a != b) ? -1 : abs(A[2] - B[2]) + abs(A[3] - B[3]) + abs(A[5] - B[5]); cout << res << endl; }
#include <bits/stdc++.h> using namespace std; long long x, y; map<long long, long long> m1, m2; vector<long long> xs; void primeFactors(long long n, map<long long, long long> &m) { while (n % 2 == 0) { m[2]++; n = n / 2; } while (n % 3 == 0) { m[3]++; n = n / 3; } while (n % 5 == 0) { m[5]++; n = n / 5; } if (n > 2) { xs.push_back(n); } } int main() { cin >> x >> y; primeFactors(x, m1); primeFactors(y, m2); if (xs.size() == 1) { cout << -1; return 0; } if (xs.size() == 2 && xs[0] != xs[1]) { cout << -1; return 0; } for (auto it : m1) { if (m2.find(it.first) == m2.end()) { m2[it.first] = 0; } } for (auto it : m2) { if (m1.find(it.first) == m1.end()) { m1[it.first] = 0; } } long long cnt = 0; int f = 0; for (auto it : m2) { if (m1[it.first] != 0 && m2[it.first] != 0) { f = 1; } cnt += abs(m1[it.first] - m2[it.first]); } cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const long long inf = 1e9; int main() { int a, b; cin >> a >> b; int ans = 0; for (int div : {2, 3, 5}) { int pa = 0; while (a % div == 0) { a /= div; pa++; } int pb = 0; while (b % div == 0) { b /= div; pb++; } ans += abs(pa - pb); } if (a != b) cout << -1 << "\n"; else cout << ans << "\n"; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:33554432") using namespace std; int a, b; int A[3], B[3]; int p[3] = {2, 3, 5}; int main() { cin >> a >> b; int res = 0; for (int i = 0; i < 3; ++i) { while (a % p[i] == 0) { a /= p[i]; A[i]++; } while (b % p[i] == 0) { b /= p[i]; B[i]++; } } if (a != b) { cout << -1 << endl; return 0; } for (int i = 0; i < 3; ++i) res += max(A[i], B[i]) - min(A[i], B[i]); cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } map<long long, long long> ma; void pf(long long n) { while (n % 2 == 0) { ma[2]++; n = n / 2; } for (unsigned long long int i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { ma[i]++; n = n / i; } } if (n > 2) ma[n]++; } int main() { long long a, b; cin >> a >> b; if (a == b) cout << "0"; else { long long r = gcd(a, b); long long k = a / r; long long c = b / r; pf(k); pf(c); map<long long, long long>::iterator it; long long count = 0; for (it = ma.begin(); it != ma.end(); it++) { if (it->first != 2 && it->first != 5 && it->first != 3) { cout << "-1"; return 0; } count += (it->second); } cout << count; } }
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 5; int n, m; int dir[3] = {2, 3, 5}, ans; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int l, r; while (~scanf("%d%d", &n, &m)) { ans = 0; int flag = 1; int temp = gcd(n, m); n /= temp; m /= temp; while (flag) { flag = 0; for (int i = 0; i < 3; i++) { if (n % dir[i] == 0) { n /= dir[i]; ans++; flag = 1; break; } } } flag = 1; while (flag) { flag = 0; for (int i = 0; i < 3; i++) { if (m % dir[i] == 0) { m /= dir[i]; ans++; flag = 1; break; } } } if (m != 1 || n != 1) ans = -1; printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int is_factor(long long int a) { int cnt = 0; while (a > 1 && (a % 2 == 0 || a % 3 == 0 || a % 5 == 0)) { if (a % 2 == 0) { a = a / 2; cnt++; } else if (a % 3 == 0) { a = a / 3; cnt++; } else if (a % 5 == 0) { a = a / 5; cnt++; } } if (a == 1) return cnt; else return -1; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { long long int hcf = gcd(a, b); return ((a / hcf) * b); } int main() { long long int a, b; cin >> a >> b; long long int lcm_ = lcm(a, b); long long int to_div = (lcm_ / a) * (lcm_ / b); long long int ans = is_factor(to_div); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; vector<int> v; int main() { int a, b; cin >> a >> b; for (int i = 1; i * i <= a; i++) { if (a % i == 0) { if (b % i == 0) v.push_back(i); if (b % (a / i) == 0) v.push_back(a / i); } } int ans = -1; for (int i = 0; i < v.size(); i++) { int d[2] = {a / v[i], b / v[i]}; int p[2] = {0, 0}; int a[3] = {2, 3, 5}; for (int k = 0; k < 2; k++) { for (int j = 0; j < 3; j++) { while (d[k] % a[j] == 0) { d[k] /= a[j]; p[k]++; } } } if (d[0] == 1 && d[1] == 1) { if (ans == -1 || p[0] + p[1] < ans) ans = p[0] + p[1]; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 10000000; int lp[N + 1]; vector<int> pr; void prime() { for (int i = 2; i <= N; ++i) { if (lp[i] == 0) { lp[i] = i; pr.push_back(i); } for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j) lp[i * pr[j]] = pr[j]; } return; } int a_div[100000]; int b_div[100000]; int main() { prime(); int pr_size = pr.size(); int a, b; scanf("%d%d", &a, &b); int tmp_a = 1, tmp_b = 1; int tmp = a; for (int i = 0; pr[i] <= a && i < pr_size; i++) { while (true) { if (tmp % pr[i] == 0) { a_div[i]++; tmp /= pr[i]; } else break; } } if (tmp != 1) { tmp_a = tmp; } tmp = b; for (int i = 0; pr[i] <= b && i < pr_size; i++) { while (true) { if (tmp % pr[i] == 0) { b_div[i]++; tmp /= pr[i]; } else break; } } if (tmp != 1) { tmp_b = tmp; } bool is_possible = true; int sol = 0; for (int i = 0; i < 100000; i++) { if (a_div[i] != b_div[i]) { if (pr[i] != 2 && pr[i] != 3 && pr[i] != 5) { is_possible = false; break; } sol += abs(a_div[i] - b_div[i]); } } if (tmp_a != tmp_b) is_possible = false; if (is_possible == true) { printf("%d\n", sol); } else { printf("-1\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; long long calc(long long &a, long long prime[]) { prime[2] = prime[3] = prime[5] = 0; while (a > 1 && a % 2 == 0) { prime[2]++; a /= 2; } while (a > 1 && a % 3 == 0) { prime[3]++; a /= 3; } while (a > 1 && a % 5 == 0) { prime[5]++; a /= 5; } return a; } int main() { long long a, b; cin >> a >> b; if (a == b) { cout << 0; return 0; } long long primeA[10]; long long primeB[10]; calc(a, primeA); calc(b, primeB); if (a != b) { cout << -1; return 0; } long long res2 = min(primeA[2], primeB[2]); long long res3 = min(primeA[3], primeB[3]); long long res5 = min(primeA[5], primeB[5]); long long res = 0; res += primeA[2] - res2; res += primeA[3] - res3; res += primeA[5] - res5; res += primeB[2] - res2; res += primeB[3] - res3; res += primeB[5] - res5; cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, ans = 0, val[10], i, k = 0; cin >> n >> m; if (n == m) { cout << "0" << endl; return 0; } int arr[] = {2, 3, 5}; for (i = 0; i < 3; i++) { int count = 0; if (n % arr[i] == 0) { while (n % arr[i] == 0) { n /= arr[i]; count++; } } val[k++] = count; } for (i = 0; i < 3; i++) { int count = 0; if (m % arr[i] == 0) { while (m % arr[i] == 0) { m /= arr[i]; count++; } } val[k++] = count; } if (n != m) cout << "-1" << endl; else { for (int i = 0; i < 3; i++) { ans += abs(val[3 + i] - val[i]); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res % mod) * (a % mod), res %= mod; a = (a % mod) * (a % mod); a %= mod; b >>= 1; } return res; } long long divide(long long a, int b) { return ((a % mod) * binpow(b, mod - 2) % mod) % mod; } long long nCr(long long n, long long k) { long long res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res = ((res % mod) * ((n - i) % mod)) % mod; res = divide(res, i + 1); } return res; } int a, b; bool check(int n) { return n >= 7; } int solve(int x, int y) { if (x == y) return 0; int ret = 1000000; if (x % 3 == 0) ret = min(ret, 1 + solve(x / 3, y)); if (x % 2 == 0) ret = min(ret, 1 + solve(x / 2, y)); if (x % 5 == 0) ret = min(ret, 1 + solve(x / 5, y)); if (y % 3 == 0) ret = min(ret, 1 + solve(x, y / 3)); if (y % 2 == 0) ret = min(ret, 1 + solve(x, y / 2)); if (y % 5 == 0) ret = min(ret, 1 + solve(x, y / 5)); return ret; } int main() { ios ::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; map<int, int> cnt_a, cnt_b; set<int> st; for (int i = 2; i * i <= a; i++) { while (a % i == 0) { cnt_a[i]++; a /= i; st.insert(i); } } if (a > 1) cnt_a[a]++, st.insert(a); for (int i = 2; i * i <= b; i++) { while (b % i == 0) { cnt_b[i]++; b /= i; st.insert(i); } } if (b > 1) cnt_b[b]++, st.insert(b); int cnt = 0; for (auto i : st) { if (i >= 7 && (!cnt_a[i] || !cnt_b[i])) { return cout << "-1", 0; } cnt += abs(cnt_a[i] - cnt_b[i]); } cout << cnt; }
#include <bits/stdc++.h> using namespace std; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; long long ans, n, k, m, x, nb, curans, myprev, leafs, nodes, h; string s, key; long long diva[10]; long long divb[10]; void solve() { cin >> n >> k; memset(diva, 0, sizeof diva); memset(divb, 0, sizeof divb); long long dv[3] = {2, 3, 5}; for (int i = 0; i < 3; i++) while (n % dv[i] == 0) n /= dv[i], diva[dv[i]]++; for (int i = 0; i < 3; i++) while (k % dv[i] == 0) k /= dv[i], divb[dv[i]]++; if (n != k) { cout << -1; return; } ans = 0; for (int i = 0; i <= 5; i++) ans += abs(diva[i] - divb[i]); cout << ans << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000000; map<int, int> mp, mp2; map<int, int>::iterator it; int n, m; int tmp[3] = {2, 3, 5}, flag, t; void bfs(int val, int cnt) { queue<int> q; mp[val] = cnt; q.push(val); while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < 3; ++i) { if (x % tmp[i] == 0 && x != 0) { if (mp.count(x / tmp[i]) == 0) { mp[x / tmp[i]] = mp[x] + 1; q.push(x / tmp[i]); } } } } } void bfs1(int val, int cnt) { if (mp.count(val) != 0) { flag = mp[val]; return; } queue<int> q; mp2[val] = cnt; q.push(val); while (!q.empty() && flag == -1) { int x = q.front(); q.pop(); for (int i = 0; i < 3 && flag == -1; ++i) { if (x % tmp[i] == 0 && x != 0 && flag == -1) { if (mp.count(x / tmp[i]) != 0 && flag == -1) { flag = mp[x / tmp[i]] + mp2[x] + 1; return; } if (mp2.count(x / tmp[i]) == 0) { mp2[x / tmp[i]] = mp2[x] + 1; q.push(x / tmp[i]); } } } } } int main() { while (cin >> n >> m) { if (n == m) { cout << "0\n"; continue; } mp.clear(); mp2.clear(); t = 0; bfs(n, 0); flag = -1; bfs1(m, 0); cout << flag << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b; long long foo(long long n) { long long x = 0, y = 0; while (a % n == 0) a /= n, x++; while (b % n == 0) b /= n, y++; return abs(x - y); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> a >> b; if (a == b) { cout << 0; return 0; } long long count = 0; count += foo(2); count += foo(3); count += foo(5); if (a != b) { cout << "-1"; } else cout << count; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> Powers(int n) { vector<int> powers; for (int i = 2; i * i <= n; i++) { int tmp = n; int count = 0; while (n % i == 0) { powers.push_back(i); n /= i; } } if (n != 1) { powers.push_back(n); } return powers; } int main() { int a, b; cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } vector<int> v1 = Powers(a); int a2 = 0, a3 = 0, a5 = 0, b2 = 0, b3 = 0, b5 = 0; int x1 = 1, y1 = 1; vector<int> x, y; for (int i = 0; i < v1.size(); i++) { if (v1[i] == 2) { a2++; } else if (v1[i] == 3) { a3++; } else if (v1[i] == 5) { a5++; } else { x1 *= v1[i]; x.push_back(v1[i]); } } vector<int> v2 = Powers(b); for (int i = 0; i < v2.size(); i++) { if (v2[i] == 2) { b2++; } else if (v2[i] == 3) { b3++; } else if (v2[i] == 5) { b5++; } else { y1 *= v2[i]; y.push_back(v2[i]); } } if (x1 != y1) { cout << -1 << endl; } else { int ans = abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int a, b; cin >> a >> b; multiset<int> aa, bb, t; vector<int> d(100, 0), in(100, 0); int x = a; for (auto p : {2, 3, 5}) { while (x % p == 0) { aa.insert(p); x /= p; } } if (x > 1) aa.insert(x); x = b; for (auto p : {2, 3, 5}) { while (x % p == 0) { bb.insert(p); x /= p; } } if (x > 1) bb.insert(x); auto it = set_intersection((aa).begin(), (aa).end(), (bb).begin(), (bb).end(), in.begin()); for (auto i = in.begin(); i != it; i++) { t.insert(*i); } it = set_difference((aa).begin(), (aa).end(), (t).begin(), (t).end(), in.begin()); int la = -1, lb = -1; if (it != in.begin()) { auto nw = it; nw--; la = *nw; } int cnt = 0; cnt += it - in.begin(); it = set_difference((bb).begin(), (bb).end(), (t).begin(), (t).end(), in.begin()); if (it != in.begin()) { auto nw = it; nw--; lb = *nw; } cnt += it - in.begin(); if (la > 5 || lb > 5) cnt = -1; cout << cnt << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int ans = 0, d[6] = {}; for (int i = 2; i < 6; i++) { while (a % i == 0) a /= i, d[i]++; while (b % i == 0) b /= i, d[i]--; ans += abs(d[i]); } cout << (a - b ? -1 : ans); return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (x < y) swap(x, y); if (x % y == 0) return y; return gcd(y, x % y); } int main() { int num[3] = {2, 3, 5}; int a, b; cin >> a >> b; int xa = a, xb = b; for (int i = 0; i < 3; i++) while (xa % num[i] == 0) { xa /= num[i]; } for (int i = 0; i < 3; i++) while (xb % num[i] == 0) { xb /= num[i]; } if (xa != xb) cout << "-1" << endl; else { int ans = 0; int k = a / gcd(a, b); for (int i = 0; i < 3; i++) while (k % num[i] == 0) { k /= num[i]; ans++; } k = b / gcd(a, b); for (int i = 0; i < 3; i++) while (k % num[i] == 0) { k /= num[i]; ans++; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2e9 + 5; int main() { cin.tie(0); cin.sync_with_stdio(0); int a, b; cin >> a >> b; if (a < b) swap(a, b); int ans = 0; map<int, int> memoa, memob; queue<int> qa, qb; qa.push(a), qb.push(b); memoa[a] = 0, memob[b] = 0; while (!qa.empty()) { int x = qa.front(); qa.pop(); if (x % 2 == 0) { if (memoa.count(x / 2) == 0) { memoa[x / 2] = memoa[x] + 1; qa.push(x / 2); } } if (x % 3 == 0) { if (memoa.count(x / 3) == 0) { memoa[x / 3] = memoa[x] + 1; qa.push(x / 3); } } if (x % 5 == 0) { if (memoa.count(x / 5) == 0) { memoa[x / 5] = memoa[x] + 1; qa.push(x / 5); } } } while (!qb.empty()) { int x = qb.front(); qb.pop(); if (x % 2 == 0) { if (memob.count(x / 2) == 0) { memob[x / 2] = memob[x] + 1; qb.push(x / 2); } } if (x % 3 == 0) { if (memob.count(x / 3) == 0) { memob[x / 3] = memob[x] + 1; qb.push(x / 3); } } if (x % 5 == 0) { if (memob.count(x / 5) == 0) { memob[x / 5] = memob[x] + 1; qb.push(x / 5); } } } int best = INF; for (auto &p : memoa) if (memob.count(p.first)) best = min(best, p.second + memob[p.first]); cout << (best == INF ? -1 : best); }
#include <bits/stdc++.h> using namespace std; void BFS(map<int, int> &m1, int t) { queue<pair<int, int>> q1; set<int> set_help; set<int>::iterator p; q1.push(make_pair(t, 0)); m1[t] = 0; set_help.insert(t); int Value, step; while (q1.empty() == 0) { Value = q1.front().first; step = q1.front().second; if (Value % 2 == 0) { p = set_help.find(Value / 2); if (p == set_help.end()) { m1[Value / 2] = step + 1; q1.push(make_pair(Value / 2, step + 1)); set_help.insert(Value / 2); } } if (Value % 3 == 0) { p = set_help.find(Value / 3); if (p == set_help.end()) { m1[Value / 3] = step + 1; q1.push(make_pair(Value / 3, step + 1)); set_help.insert(Value / 3); } } if (Value % 5 == 0) { p = set_help.find(Value / 5); if (p == set_help.end()) { m1[Value / 5] = step + 1; q1.push(make_pair(Value / 5, step + 1)); set_help.insert(Value / 5); } } q1.pop(); } } int main() { int a, b; ifstream ff("file.txt"); cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } map<int, int> ma, mb; BFS(ma, a); BFS(mb, b); map<int, int>::iterator p = ma.begin(), p1; int Stap_Min = INT_MAX, Step; for (p; p != ma.end(); p++) { p1 = mb.find(p->first); if (p1 != mb.end()) { Step = p->second + p1->second; if (Step < Stap_Min) { Stap_Min = Step; } } } if (Stap_Min == INT_MAX) { cout << -1 << endl; } else { cout << Stap_Min << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int di(int &n, int x) { int count = 0; while (n % x == 0) { n /= x; count++; } return count; } int main() { int a, b; cin >> a >> b; int x[3], y[3]; x[0] = di(a, 2); x[1] = di(a, 3); x[2] = di(a, 5); y[0] = di(b, 2); y[1] = di(b, 3); y[2] = di(b, 5); if (a != b) cout << -1; else { cout << abs(x[0] - y[0]) + abs(x[1] - y[1]) + abs(x[2] - y[2]); } cin >> a; return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } int main() { long long num[] = {2, 3, 5}; long long n, m, p, q, d; long long x, y; long long i, j, k, cunt = 0; long long mark = 1; cin >> n >> m; x = n, y = m; for (i = 0; i < 3; i++) { while (n % num[i] == 0) n /= num[i]; while (m % num[i] == 0) m /= num[i]; } if (n != m) { cout << -1 << endl; return 0; } d = gcd(x, y); p = x / d; q = y / d; for (i = 0; i < 3; i++) { while (p % num[i] == 0) { p /= num[i]; cunt++; } while (q % num[i] == 0) { q /= num[i]; cunt++; } } cout << cunt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b; int a2 = 0, a3 = 0, a5 = 0; int b2 = 0, b3 = 0, b5 = 0; cin >> a >> b; 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 << "\n"; else cout << abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:65536000") template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } template <class T1, class T2, class T3, class T4> void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6 << endl; } int main() { int cont = 0, a, b, ans = 0; int arr[] = {2, 3, 5}; scanf("%d %d", &a, &b); for (int i = 0; i < 3; i++) { cont = 0; while (a % arr[i] == 0) { cont++; a /= arr[i]; } while (b % arr[i] == 0) { cont--; b /= arr[i]; } ans += abs(cont); } if (a != b) ans = -1; printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; class node { public: long long int second; long long int length; }; int main() { long long int m, n, i, j, flag1 = 0, flag2 = 0, flag = 0; vector<long long int> setter[2]; cin >> m >> n; if (m == n) { cout << "0\n"; return 0; } for (i = 2; m > 1;) { if (i > 5 && flag1 == 0) { flag = 1; break; } if (m % i == 0) { if (i == 2 || i == 3 || i == 5) flag1 = 1; setter[0].push_back(i); m = m / i; } else i++; } for (i = 2; n > 1 && flag == 0;) { if (i > 5 && flag2 == 0) { flag = 1; break; } if (n % i == 0) { if (i == 2 || i == 3 || i == 5) flag2 = 1; setter[1].push_back(i); n = n / i; } else i++; } long long int count = 0; for (i = 0, j = 0; flag == 0 && i < setter[0].size() && j < setter[1].size();) { if (setter[0][i] == setter[1][j]) { count += 2; i++; j++; } else if (setter[0][i] > setter[1][j]) { if (setter[1][j] > 5) { flag = 1; break; } j++; } else { if (setter[0][i] > 5) { flag = 1; break; } i++; } } if (flag == 0) { if (i == setter[0].size()) { for (; j < setter[1].size() && flag == 0; j++) { if (setter[1][j] > 5) { flag = 1; break; } } } else if (j == setter[1].size()) { for (; i < setter[0].size() && flag == 0; i++) { if (setter[0][i] > 5) { flag = 1; break; } } } } if (flag == 1) cout << "-1\n"; else cout << setter[0].size() + setter[1].size() - count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long arr[3] = {0}, arr1[3] = {0}, n, m, c = 0, q = 0; cin >> n >> m; while (1) { c = 0; if (n % 2 == 0) { n /= 2; arr[0]++; c++; } else if (n % 3 == 0) { n /= 3; arr[1]++; c++; } else if (n % 5 == 0) { n /= 5; arr[2]++; c++; } if (m % 2 == 0) { m /= 2; arr1[0]++; c++; } else if (m % 3 == 0) { m /= 3; arr1[1]++; c++; } else if (m % 5 == 0) { m /= 5; arr1[2]++; c++; } if (c == 0) { if (n == m) { break; } else { cout << "-1"; exit(0); } } } for (int i = 0; i < 3; i++) { m = arr[i] - arr1[i]; if (m < 0) m *= -1; q += m; } cout << q; return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b != 0) return gcd(b, a % b); else return a; } int main() { int a, b, ans; while (scanf("%d %d", &a, &b) != EOF) { ans = 0; if (a == b) { puts("0"); } else { int GCD = gcd(a, b); int m1 = a / GCD, m2 = b / GCD; bool flag = 1; while (m1 != 1) { if (m1 % 5 == 0) m1 /= 5; else if (m1 % 3 == 0) m1 /= 3; else if (m1 % 2 == 0) m1 /= 2; else { flag = 0; break; } ans += 1; } if (flag != 0) { while (m2 != 1) { if (m2 % 5 == 0) m2 /= 5; else if (m2 % 3 == 0) m2 /= 3; else if (m2 % 2 == 0) m2 /= 2; else { flag = 0; break; } ans += 1; } } if (!flag) puts("-1"); else printf("%d\n", ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; void ayushmehta651() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int32_t main() { ayushmehta651(); long long a, b; cin >> a >> b; long long a2 = 0, a3 = 0, a5 = 0; long long b2 = 0, b3 = 0, b5 = 0; while (a and a % 2 == 0) { ++a2; a /= 2; } while (a and a % 3 == 0) { ++a3; a /= 3; } while (a and a % 5 == 0) { ++a5; a /= 5; } while (b and b % 2 == 0) { ++b2; b /= 2; } while (b and b % 3 == 0) { ++b3; b /= 3; } while (b and b % 5 == 0) { ++b5; b /= 5; } if (a == b) { cout << abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5) << endl; } else { cout << -1 << endl; } return 0; } bool compare(string &s1, string &s2) { return s1.size() < s2.size(); }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a, b, tmp, ansa = 0, ansb = 0; cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } tmp = gcd(a, b); int tmpa, tmpb; tmpa = a / tmp; tmpb = b / tmp; if (tmpa != 1) { while (tmpa % 2 == 0) { ++ansa; tmpa /= 2; } } if (tmpa != 1) { while (tmpa % 3 == 0) { ++ansa; tmpa /= 3; } } if (tmpa != 1) { while (tmpa % 5 == 0) { ++ansa; tmpa /= 5; } } if (tmpa != 1) { cout << -1 << endl; return 0; } if (tmpb != 1) { while (tmpb % 2 == 0) { ++ansb; tmpb /= 2; } } if (tmpb != 1) { while (tmpb % 3 == 0) { ++ansb; tmpb /= 3; } } if (tmpb != 1) { while (tmpb % 5 == 0) { ++ansb; tmpb /= 5; } } if (tmpb != 1) { cout << -1 << endl; return 0; } cout << ansa + ansb << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void Solve(int a, int b) { int a1, a2, b1, b2, c1, c2; a1 = a2 = b1 = b2 = c1 = c2 = 0; while (a % 2 == 0) { a /= 2; a1++; } while (a % 3 == 0) { a /= 3; b1++; } while (a % 5 == 0) { a /= 5; c1++; } while (b % 2 == 0) { b /= 2; a2++; } while (b % 3 == 0) { b /= 3; b2++; } while (b % 5 == 0) { b /= 5; c2++; } if (a != b) { printf("-1\n"); return; } printf("%d\n", abs(a1 - a2) + abs(b1 - b2) + abs(c1 - c2)); } int main(int argc, char* argv[]) { int a, b; cin >> a >> b; Solve(a, b); return 0; }
#include <bits/stdc++.h> using namespace std; void calculateDivisorCount(int& num, int divisorsCount[], int divisors[], int divSize) { int div = 0; for (int i = 0; i < divSize; ++i) { div = divisors[i]; while (num % div == 0) { num /= div; divisorsCount[div] += 1; } } } int main() { int a, b; cin >> a >> b; int const divisorsCount = 3; int divisiors[divisorsCount] = {2, 3, 5}; int divisorCountA[6] = {}, divisorCountB[6] = {}; calculateDivisorCount(a, divisorCountA, divisiors, divisorsCount); calculateDivisorCount(b, divisorCountB, divisiors, divisorsCount); if ((a != 1 || b != 1) && a != b) { cout << -1; } else { cout << abs(divisorCountA[2] - divisorCountB[2]) + abs(divisorCountA[3] - divisorCountB[3]) + abs(divisorCountA[5] - divisorCountB[5]); } return 0; }
#include <bits/stdc++.h> using namespace std; int Gcd(int a, int b) { if (!b) return a; return Gcd(b, a % b); } int main() { int a[2]; while (scanf("%d%d", &a[0], &a[1]) != EOF) { int b = Gcd(a[0], a[1]); a[0] /= b, a[1] /= b; bool flag = true; int ans = 0; for (int i = 0; i < 2; i++) { while (a[i] % 2 == 0) ++ans, a[i] /= 2; while (a[i] % 3 == 0) ++ans, a[i] /= 3; while (a[i] % 5 == 0) ++ans, a[i] /= 5; if (a[i] != 1) { cout << -1 << endl; flag = false; break; } } if (flag) cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, a2 = 0, b2 = 0, a3 = 0, b3 = 0, a5 = 0, b5 = 0; cin >> a >> b; if (a == b) { cout << 0 << endl; return 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 << endl; else cout << abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5); }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, ans = 0, flag = 0; int a2 = 0, b2 = 0, a3 = 0, b3 = 0, a5 = 0, b5 = 0; cin >> a >> b; if (a == b) printf("0\n"); else { while (a != b) { if (a % 5 == 0) { a = a / 5; a5++; } else if (a % 3 == 0) { a = a / 3; a3++; } else if (a % 2 == 0) { a = a / 2; a2++; } if (b % 5 == 0) { b = b / 5; b5++; } else if (b % 3 == 0) { b = b / 3; b3++; } else if (b % 2 == 0) { b = b / 2; b2++; } if (a % 2 != 0 && a % 3 != 0 && a % 5 != 0 && b % 2 != 0 && b % 3 != 0 && b % 5 != 0 && a != b) { flag = 1; break; } } if (flag == 1) printf("-1\n"); else printf("%d\n", abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5)); } }
#include <bits/stdc++.h> using namespace std; int a, b, x, ans, a2, a3, a5, b2, b3, b5; int main(int argc, char **argv) { scanf("%d%d", &a, &b); x = a; while (x % 2 == 0) x /= 2, ++a2; x = a; while (x % 3 == 0) x /= 3, ++a3; x = a; while (x % 5 == 0) x /= 5, ++a5; x = b; while (x % 2 == 0) x /= 2, ++b2; x = b; while (x % 3 == 0) x /= 3, ++b3; x = b; while (x % 5 == 0) x /= 5, ++b5; ans = abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5); while (a2--) a /= 2; while (a3--) a /= 3; while (a5--) a /= 5; while (b2--) b /= 2; while (b3--) b /= 3; while (b5--) b /= 5; if (a != b) puts("-1"); else printf("%d", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 4e3 + 10; int main() { int a, b; cin >> a >> b; function<int(int, int)> gcd = [&](int n, int m) -> int { return m == 0 ? n : gcd(m, n % m); }; auto check = [](int x) -> int { int a[3] = {2, 3, 5}; int cnt = 0; for (int i = 0; i < 3; i++) while (x % a[i] == 0) x /= a[i], cnt++; if (x != 1) cnt = -INF; return cnt; }; int g = gcd(a, b); int ans = check(a / g) + check(b / g); if (ans < 0) ans = -1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> void quit(); using namespace std; const long double PI = acos((double)-1.0); const long double EPS = 1e-11; double __t; int a, b; int va[3], vb[3]; int main() { cin >> a >> b; while (a % 2 == 0) va[0]++, a /= 2; while (a % 3 == 0) va[1]++, a /= 3; while (a % 5 == 0) va[2]++, a /= 5; while (b % 2 == 0) vb[0]++, b /= 2; while (b % 3 == 0) vb[1]++, b /= 3; while (b % 5 == 0) vb[2]++, b /= 5; if (a != b) { cout << -1; return 0; } int ans = 0; for (int i = 0; i < (int)3; ++i) ans += abs(va[i] - vb[i]); cout << ans; quit(); } void quit() { exit(0); }
#include <bits/stdc++.h> using namespace std; void print(vector<long long int> v) { for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; } long long int abss(long long int a, long long int b) { if (a > b) return a - b; else return b - a; } long long int minn(long long int a, long long int b) { return a < b ? a : b; } long long int maxx(long long int a, long long int b) { return a > b ? a : b; } long long int cill(long long int a, long long int b) { if (a % b == 0) return a / b; return a / b + 1; } long long int gcd(long long int a, long long int b) { if (a % b == 0) return b; else return gcd(b, a % b); } long long int mod_exp(long long int a, long long int n, long long int mod) { if (n == 0) return 1; if (n % 2 == 0) { long long int temp = mod_exp(a, n / 2, mod); return (temp * temp) % mod; } else { long long int temp1 = a; long long int temp2 = mod_exp(a, n - 1, mod); return (temp1 * temp2) % mod; } } vector<long long int> facts; void preprocess(int n) { long long int temp = 1; facts.push_back(temp); for (int i = 1; i <= n; i++) { temp *= i; temp %= 1000003; facts.push_back(temp); } } void solve() { long long int a, b; cin >> a >> b; long long int twosa = 0; long long int threesa = 0; long long int fivesa = 0; while (a % 2 == 0) { twosa++; a /= 2; } while (a % 3 == 0) { threesa++; a /= 3; } while (a % 5 == 0) { fivesa++; a /= 5; } long long int twosb = 0; long long int threesb = 0; long long int fivesb = 0; while (b % 2 == 0) { twosb++; b /= 2; } while (b % 5 == 0) { fivesb++; b /= 5; } while (b % 3 == 0) { threesb++; b /= 3; } if (a != b) cout << "-1" << endl; else cout << abss(twosa, twosb) + abss(threesa, threesb) + abss(fivesa, fivesb) << endl; } int main() { solve(); }
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int cnt[2][10]; int main() { int a, b; cin >> a >> b; while (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) { if (a % 2 == 0) { a /= 2; cnt[0][2]++; } if (a % 3 == 0) { a /= 3; cnt[0][3]++; } if (a % 5 == 0) { a /= 5; cnt[0][5]++; } } while (b % 2 == 0 || b % 3 == 0 || b % 5 == 0) { if (b % 2 == 0) { b /= 2; cnt[1][2]++; } if (b % 3 == 0) { b /= 3; cnt[1][3]++; } if (b % 5 == 0) { b /= 5; cnt[1][5]++; } } if (a != b) puts("-1"); else { cout << abs(cnt[0][2] - cnt[1][2]) + abs(cnt[0][3] - cnt[1][3]) + abs(cnt[0][5] - cnt[1][5]) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long gcd(long a, long b) { long r, i; while (b != 0) { r = a % b; a = b; b = r; } return a; } int main(void) { long long int a, b, gd, count1 = 0, count2 = 0, ans; cin >> a >> b; gd = gcd(a, b); a = a / gd; b = b / gd; int e = 0; while (a > 1 && e == 0) { if (a % 2 == 0) { a /= 2; count1++; } else if (a % 3 == 0) { a /= 3; count1++; } else if (a % 5 == 0) { a /= 5; count1++; } else e = 1; } if (e == 1) ans = -1; else { e = 0; while (b > 1 && e == 0) { if (b % 2 == 0) { b /= 2; count2++; } else if (b % 3 == 0) { b /= 3; count2++; } else if (b % 5 == 0) { b /= 5; count2++; } else e = 1; } if (e == 1) ans = -1; else ans = count1 + count2; } cout << ans; return 0; }
#include <bits/stdc++.h> int main() { int a, b, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0; scanf("%d%d", &a, &b); while (a % 2 == 0) { a /= 2; c++; } while (a % 3 == 0) { a /= 3; d++; } while (a % 5 == 0) { a /= 5; e++; } while (b % 2 == 0) { b /= 2; f++; } while (b % 3 == 0) { b /= 3; g++; } while (b % 5 == 0) { b /= 5; h++; } if (a != b) printf("-1"); else printf("%d", abs(c - f) + abs(d - g) + abs(e - h)); }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int cal(int n) { if (n == 1) return 0; int ans = 0; while (n % 2 == 0) { n /= 2; ans++; } while (n % 3 == 0) { n /= 3; ans++; } while (n % 5 == 0) { n /= 5; ans++; } if (n == 1) return ans; else return -1; } int main() { int a, b; cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } int g = gcd(a, b); int c1 = cal(a / g); int c2 = cal(b / g); if (c1 == -1 || c2 == -1) cout << -1 << endl; else cout << c1 + c2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, a2 = 0, b2 = 0, a3 = 0, b3 = 0, a5 = 0, b5 = 0; cin >> a >> b; 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 << abs(a2 - b2) + abs(a3 - b3) + abs(a5 - b5) << endl; } else { cout << -1 << endl; } }
#include <bits/stdc++.h> int n = 2, a, b, D[6], s; int main() { for (std::cin >> a >> b; n < 6; s += abs(D[n++])) { while (a % n == 0) a /= n, D[n]++; while (b % n == 0) b /= n, D[n]--; } std::cout << (a - b ? -1 : s); }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, la[10] = {0}, lb[10] = {0}, ans = 0; scanf("%d%d", &a, &b); while (a % 2 == 0) { a /= 2; la[2]++; } while (a % 3 == 0) { a /= 3; la[3]++; } while (a % 5 == 0) { a /= 5; la[5]++; } while (b % 2 == 0) { b /= 2; lb[2]++; } while (b % 3 == 0) { b /= 3; lb[3]++; } while (b % 5 == 0) { b /= 5; lb[5]++; } if (a != b) puts("-1"); else { ans += la[2] - lb[2] > 0 ? la[2] - lb[2] : lb[2] - la[2]; ans += la[3] - lb[3] > 0 ? la[3] - lb[3] : lb[3] - la[3]; ans += la[5] - lb[5] > 0 ? la[5] - lb[5] : lb[5] - la[5]; printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int parent(int i, vector<long long> &a) { if (a[i] == i) return i; return parent(a[i], a); } void make_par(vector<long long> &a, int x, int y) { int para = parent(x, a); int parb = parent(y, a); a[parb] = para; } long long jjjj(long long j) { long long c = 0; while (j != 0) { j /= 2; ++c; } return c; } long long maxi(long long a, long long b) { return (a > b) ? a : b; } int rminutil(vector<long long> &a, int si, int sj, int i, int j, int k) { if (si >= i && sj <= j) { return a[k]; } if (sj < i || si > j) { return INT_MAX; } int mid = si + (sj - si) / 2; return min(rminutil(a, si, mid, i, j, 2 * k), rminutil(a, mid + 1, sj, i, j, 2 * k + 1)); } int rmin(vector<long long> &a, int n, int i, int j) { if (i < 0 || j > n - 1 || i > j) { cout << "invalid"; return -1; } return rminutil(a, 0, n - 1, i, j, 1); } void RMQ(vector<long long> &a, int b[], int i, int j, int n, int k) { if (i == j) { a[k] = b[i]; } int mid = i + (j - i) / 2; if (a[k] == -1) { if (a[2 * k] == -1) { RMQ(a, b, i, mid, n, 2 * k); } if (a[2 * k + 1] == -1) { RMQ(a, b, mid + 1, j, n, 2 * k + 1); } a[k] = min(a[2 * k], a[2 * k + 1]); } } void minupdate(vector<long long> &a, int si, int sj, int i, int old, int ne, int k) { if (i < si || i > sj) { return; } if (si == sj) { a[k] = ne; } if (si != sj) { int mid = si + (sj - si) / 2; if (i <= mid) { minupdate(a, si, mid, i, old, ne, 2 * k); } else { minupdate(a, mid + 1, sj, i, old, ne, 2 * k + 1); } a[k] = min(a[2 * k], a[2 * k + 1]); } } void sumupdate(vector<long long> &a, int si, int sj, int i, int old, int ne, int k) { if (i < si || i > sj) { return; } a[k] = a[k] + ne - old; if (si != sj) { int mid = si + (sj - si) / 2; sumupdate(a, si, mid, i, old, ne, 2 * k); sumupdate(a, mid + 1, sj, i, old, ne, 2 * k + 1); } } long long rsumutil(vector<long long> &a, int si, int sj, int i, int j, int k) { if (si >= i && sj <= j) { return a[k]; } if (sj < i || si > j) { return 0; } int mid = si + (sj - si) / 2; return rsumutil(a, si, mid, i, min(j, mid), 2 * k) + rsumutil(a, mid + 1, sj, max(i, mid + 1), j, 2 * k + 1); } long long rsum(vector<long long> &a, int n, int i, int j) { if (i < 0 || j >= n || i > j) { cout << "invalid"; return -1; } return rsumutil(a, 0, n - 1, i, j, 1); } void RSQ(vector<long long> &a, int b[], int i, int j, int n, int k) { if (i == j) { a[k] = b[i]; } int mid = i + (j - i) / 2; if (a[k] == -1) { if (a[2 * k] == -1) { RSQ(a, b, i, mid, n, 2 * k); } if (a[2 * k + 1] == -1) { RSQ(a, b, mid + 1, j, n, 2 * k + 1); } a[k] = a[2 * k] + a[2 * k + 1]; } } long long binaryexp(long long a, long long b, long long i) { if (b == 0) return 1LL; if (b == 1) return a; long long k = binaryexp(a, b / 2, i); if (b & 1) { return (((k * k) % i) * a) % i; } else return (k * k) % i; } long long mini(long long a, long long b) { return (a < b) ? a : b; } void diff(int a[], int h, vector<long long> &v) { if (h == 0) { v[0] = a[0]; v[1] += a[0]; } diff(a, h - 1, v); } bool prime(long long n) { int c = 0; int k = sqrt(n); for (long long i = 1; i < sqrt(n); i++) { if (n % i == 0) ++c; } if (k * k == n) { ++c; } return c == 2; } int main() { int t; t = 1; while (t--) { long long a, b; cin >> a >> b; long long k; k = 0; int va[6]; int vb[6]; memset(va, 0, sizeof va); memset(vb, 0, sizeof vb); while (a % 3 == 0) { a /= 3; ++k; } va[3] = k; k = 0; while (a % 5 == 0) { a /= 5; ++k; } va[5] = k; k = 0; while (a % 2 == 0) { a /= 2; ++k; } va[2] = k; k = 0; while (b % 2 == 0) { b /= 2; ++k; } vb[2] = k; k = 0; while (b % 3 == 0) { b /= 3; ++k; } vb[3] = k; k = 0; while (b % 5 == 0) { b /= 5; ++k; } vb[5] = k; if (b != a) { cout << -1; } else { cout << maxi(va[2], vb[2]) - mini(va[2], vb[2]) + maxi(va[3], vb[3]) - mini(va[3], vb[3]) + maxi(va[5], vb[5]) - mini(va[5], vb[5]); } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; void changeVal(ll arr[], ll pos, ll changeTo) { if (arr[pos] == -1) { arr[pos] = changeTo; return; } arr[pos] = min(arr[pos], changeTo); } int gcd(int a, int b) { if (b == 0) return a; a %= b; return gcd(b, a); } int main() { ll a, b; cin >> a >> b; ll d = gcd(a, b); while (d != 1) { a /= d; b /= d; d = gcd(a, b); } ll 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 == 1 && b == 1) { cout << ans; } else { cout << "-1\n"; } }