Cod sursa(job #2120385)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 2 februarie 2018 13:25:16
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>

using namespace std;
ifstream f("fact.in");
ofstream g("fact.out");

int n, i, j, ok=0;
int fact( int x)
{
    int rez = 0, p = 5;
    while( p < x )
    {
        rez += x/p;
        p*=5;
    }
    if (!rez)
        return 1;
    return rez;
}

int cautbin( long long n)
{
    long long mj;
    long long st = 0;
    long long dr = 100000000;
    while(st < dr)
        {
            mj = (st+dr)/2;
            if( fact(mj) > n )
                dr = mj;
            else
                st = mj+1;

        }
        return st;
}
int main()
{
    f >> n;
    if ( n == 0 )
        g << 1;
    else
    if( fact(cautbin(n)-5) != n )
        g << -1;
    else
        g << cautbin(n)-5;
    return 0;
}