Cod sursa(job #2934401)

Utilizator RolandPetreanPetrean Roland RolandPetrean Data 5 noiembrie 2022 22:25:47
Problema Factorial Scor 85
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.5 kb
// https://www.infoarena.ro/problema/fact
#include <bits/stdc++.h>
using namespace std;

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

int cntZeros(long long n) {
  long long f=5;
  int z=0;
  while (f<n) {
    z += n/f;
    f *= 5;
  }
  return z;
}

int main() {
  int p;
  fin>>p;

  long long l=0, r=10e9, mid, res=-1;
  while (l<=r) {
    mid = (l+r)/2;
    int z=cntZeros(mid);
    if (z<p) l=mid+1;
    else if (z>p) r=mid-1;
    else {
      r=mid-1;
      res = mid;
    }
  }
  fout<<res;
}