Cod sursa(job #2170595)

Utilizator YetoAdrian Tonica Yeto Data 15 martie 2018 08:50:21
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <fstream>
using namespace std;
int n, p, st, dr;

int nr0 (int a)
{
    int sol=0;
    int r=1;
    while (r<=a) {
        r*=5;
        sol+=a/r;
    }

    return sol;
}

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

    if (p==0) {
        fout<<1;
        return 0;
    }

    st=p;
    dr=5*p;
    while (st<=dr) {
        int mid = (st+dr)/2;
        if (nr0(mid)<p)
            st=mid+1;
        else
            dr=mid-1;
    }

    if (nr0(st)==p)
        fout<<st;
    else
        fout<<-1;

    return 0;
}