Cod sursa(job #2337700)

Utilizator RedXtreme45Catalin RedXtreme45 Data 6 februarie 2019 17:21:35
Problema Factorial Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>

using namespace std;

int main()
{
    ifstream fin("fact.in");
    ofstream fout("fact.out");
    int n;
    fin>>n;
    int u=n*5;
    int p=1;
    int i=5;
    int f=1;
    int OK=0;
    while (p<=u&& OK==0)
    {
        int m=(p+u)/2;
        int r=5;
        int S=0;
        while (r<m)
        {
            S+=m/r;
            r*=5;
        }
        if (S<n)
        {
            p=m+1;
        }
        if (S>n)
        {
            u=m-1;
        }
        if (S==n)
        {
            OK=m;
        }
    }
    if (OK>0)
        fout<<OK;
    else
        fout<<-1;
    return 0;
}