Cod sursa(job #2585646)

Utilizator ancestralsymphonyEmanuel Muja ancestralsymphony Data 19 martie 2020 11:37:16
Problema Factorial Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <iostream>
#include <climits>
#include <fstream>
using namespace std;
bool check(int p, int n)
{
  int c = 0, i;
  for (int i = 5; i <= p; i *= 5) {
    c += p / i;
    if (c == n)
      return 1;
  }
  return 0;
}

int findNum(int n)
{
  if (n == 0)
    return 1;
  else if (n == 1)
    return 5;

  int low = 0, high = 5 * n;
  while (low < high) {
    int mid = (low + high) / 2;
    if (check(mid, n))
      high = mid;
    else
      low = mid + 1;
  }

  switch(check(low, n)) {
    case 1: return low;
    break;

    default: return -1;
    break;
  }
}

int main()
{
  int n;
  ifstream fin("fact.in");
  fin >> n;

  ofstream fout("fact.out");
  fout << findNum(n);

  return 0;
}