Cod sursa(job #1900851)

Utilizator cella.florescuCella Florescu cella.florescu Data 3 martie 2017 16:58:13
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

long long leg(long long n, int p) {
  long long ans = 0LL;
  while (n) {
    n /= p;
    ans += n;
  }
  return ans;
}

int main()
{
    long long lim, ans, step;
    int p, q, pd, ex;
    ifstream fin("gfact.in");
    fin >> p >> q;
    fin.close();
    for (int d = 2; d * d <= p; ++d)
      if (p % d == 0) {
        pd = d; ex = 0;
        while (p % d == 0) {
          p /= d;
          ++ex;
        }
      }
    if (p > 1) {
      pd = p;
      ex = 1;
    }
    lim = 1LL * ex * q;
    for (ans = 1LL, step = (1LL << 60); step; step >>= 1LL)
      if (leg(ans + step, pd) < lim)
        ans += step;
    ofstream fout("gfact.out");
    fout << ans + 1 << '\n';
    fout.close();
    return 0;
}