Cod sursa(job #3003346)

Utilizator RolandPetreanPetrean Roland RolandPetrean Data 15 martie 2023 17:55:11
Problema GFact Scor 35
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
// https://www.infoarena.ro/problema/gfact
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

ifstream fin("gfact.in");
ofstream fout("gfact.out");

#define int long long

signed main() {
  int p, q;
  fin>>p>>q;

  vector<pair<int,int>> f{};
  for (int d=2; d*d<=p; ++d) {
    if (p%d==0) {
      int e=0;
      while (p%d==0) p /= d;
      f.push_back({d, e*q});
    }
  }
  if (p>1) f.push_back({p, q});

  int l=1, r=1e18, res=0;
  while (l <= r) {
    int mid=(l+r)/2;

    bool ok=true;
    for (int i=0; i<(int)f.size() && ok; ++i) {
      int a=f[i].first, x=a;
      
      int j=0;
      while (x <= mid) {
        j += mid/a;
        x *= a;
      }
      ok = (j>=f[i].second);
    }

    if (ok) {
      res = mid;
      r = mid-1;
    } else l = mid+1;
  }

  fout<<res;
}