Cod sursa(job #1889236)

Utilizator Andrei.CosteaAndrei Costea Andrei.Costea Data 22 februarie 2017 17:16:57
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

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

int p,x,ok,dr,st,mij,nr;

int main()
{
    f>>p;
    ok=0;
    st=1; dr=p*5;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        nr=0;  x=mij;
        while(x>0)
        {
            nr+=x/5;
            x/=5;
        }
        if(nr==p && mij/5!=0){
            while(mij%5!=0)
            mij--;
        }
        if(nr==p && mij%5==0){
                g<<mij;
        ok=1;
        break; }
        else if(nr>p)dr=mij-1;

        else if(nr<p)st=mij+1;
    }
    if(p==0)g<<"1";
    else
    if(ok==0)g<<"-1";
    return 0;
}