Cod sursa(job #2165226)

Utilizator AlexPascu007Pascu Ionut Alexandru AlexPascu007 Data 13 martie 2018 11:32:04
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int n,m,p,st,dr,mid,r,s;
int main() {
    fin>>p;
    if (p==0)
        fout<<1;
    else {
        st=1;
        dr=1000000000;
        while (st<=dr) {
            mid=(st+dr)/2; m=mid; r=5; s=0;
            while (m/r!=0) {
                s+=m/r;
                r*=5;
            }
            if (s>=p)
                dr=mid-1;
            else
                st=mid+1;

        }
        r=5; s=0;
        while (st/r!=0) {
            s+=st/r;
            r*=5;
        }
        if (s==p)
            fout<<st;
        else
            fout<<-1;
    }
    return 0;
}