Cod sursa(job #2373855)

Utilizator alexghitaAlexandru Ghita alexghita Data 7 martie 2019 15:31:12
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>

using namespace std;

const long long NMAX = 500000000;

int P;

int get_num_of_zeros(long long num) {
  int power = 5;
  int num_of_zeros = 0;

  while (power <= num) {
    num_of_zeros += num / power;
    power *= 5;
  }

  return num_of_zeros;
}

void binary_search(long long l, long long r) {
  if (l > r) {
    cout << "-1";
    return;
  }

  long long mid = (l + r) >> 1;
  int num_of_zeros = get_num_of_zeros(mid);

  if (num_of_zeros == P) {
    while (mid % 5) mid--;
    cout << mid;
  } else if (num_of_zeros < P) {
    binary_search(mid + 1, r);
  } else {
    binary_search(l, mid - 1);
  }
}

int main() {
  freopen("fact.in", "r", stdin);
  freopen("fact.out", "w", stdout);

  cin >> P;
  if (P == 0) {
    cout << "1";
  } else {
    binary_search(1, NMAX);
  }
}