Cod sursa(job #3322177)

Utilizator hrib_the_slothAndreea Pasca hrib_the_sloth Data 13 noiembrie 2025 01:51:56
Problema GFact Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long i8;
vector<pair<i8, i8>> v;

bool chek(i8 x) {
  for (i8 i = 0; i < v.size(); i++) {
    i8 y = v[i].first;
    i8 res = 0;
    i8 ct = 1;
    while (y <= x) {
      res += x / y;
      y *= v[i].first;
    }
    if (res < v[i].second) {
      return false;
    }
  }
  return true;
}
int main() {
  ifstream cin("gfact.in");
  ofstream cout("gfact.out");
  i8 p, q;
  cin >> p >> q;

  i8 n = 2;
  i8 tmp = p;
  while (p > 1) {
    i8 pow = 0;
    while (p % n == 0) {
      pow++;
      p /= n;
    }
    if (pow != 0) {
      v.push_back({n, pow * q});
    }
    n++;
    if (p > 1 && n * n > p) {
      n = p;
    }
  }
  p = tmp;
  i8 l = 1;
  i8 r = p * q;
  i8 res = -1;
  while (l <= r) {
    i8 mid = l + (r - l) / 2;
    if (chek(mid)) {
      r = mid - 1;
      res = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << res << "\n";

  return 0;
}