Cod sursa(job #2612364)

Utilizator dianapingu1Diana Vasiliu dianapingu1 Data 8 mai 2020 21:32:41
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <fstream>

using namespace std;

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

    int p;
    fin>>p;
    if (p==0)
        fout<<"1";
    else {
        int mij,copie,st,dr,ok,putere,s;
        ok=1; st=1; dr=2000000000;

        while(st <= dr && ok) {
            mij = (st+dr)/2;
            copie = mij;
            s=0; putere=5;

            while(copie/putere > 0) {
                s = s + copie/putere;
                putere*=5;
            }

            if (s == p)
                ok=0;
            else if (s > p)
                dr = mij-1;
            else
                st = mij+1;
        }

        if (ok) {
            mij = -1;
        }
        else {
            mij = mij - mij%5;
        }

        fout<<mij;
    }

    fin.close();
    fout.close();
    return 0;
}