Cod sursa(job #1384836)

Utilizator apostolandreiApostol Andrei Laurentiu apostolandrei Data 11 martie 2015 14:34:47
Problema Factorial Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <iostream>
#include <fstream>

using namespace std;

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

long long zero (long long n){
  int nr = 0;
  while (n >= 5){
    nr += n/5;
    n /= 5;
  }
  if (n == 0) return 0;
  return nr;
}

int solve(int p){
  int i = 0;
  long long pas = 1 << 15;
  while (pas != 0){
    if (zero(i+pas) <= p)
      i += pas;
      pas /= 2;
  }
  if (zero(i) == p) return i;
  if (zero(i+1) == p) return i + 1;
  return -1;
}

int main()
{
    int p;
    in >> p;
    int x = solve (p);
    if (x == -1) out << -1;
    else out << x - 4;
    return 0;
}