Cod sursa(job #2477396)

Utilizator Rares5000Baciu Rares Rares5000 Data 20 octombrie 2019 11:23:52
Problema Factorial Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <iostream>
#include <fstream>

using namespace std;

int fact(int n)
{
    int putere, s = 0;
    putere = 5;
    while(n / putere != 0)
    {
        s += n / putere;
        putere = putere * 5;

    }
    return s;
}

int main()
{
    ifstream fin("fact.in");
    ofstream fout("fact.out");
    int p, n, i, st, dr, mij, c, factorial;
    fin >> p;
    st = 1;
    dr = 500000000;
    c = 0;
    while(st <= dr)
    {
        mij = st + (dr - st) / 2;
        factorial = fact(mij);
        if(factorial == p)
        {
            n = mij;
            c = 1;
            break;
        }
        if(factorial < p)
            st = mij + 1;
        else
            dr = mij - 1;
    }
    if(c == 1)
        {
            n = n - (n % 5);
            fout << n;
        }
    else
        fout << -1;
    return 0;
}