Cod sursa(job #2744320)

Utilizator PatruMihaiPatru Mihai PatruMihai Data 24 aprilie 2021 13:34:33
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

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

int nrzero(int x)
{
   int total = 0;
   while(x > 0)
   {
       total += x / 5;
       x /= 5;
   }

    return total;
}

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

    int l = 1; 
    int r = 1e9;
    int mid;

    while (l <= r)
    {
        mid = (l + r) / 2;
        
        if(nrzero(mid) >= p)
        {
            r = mid - 1;
        }
        else
        {
            l = mid + 1;
        }
    }

    if(nrzero(r + 1) == p)
    {
        fout << r + 1;
    }
    else
        fout << -1;
    return 0;
}