Cod sursa(job #1312981)

Utilizator KOzarmOvidiu Badea KOzarm Data 10 ianuarie 2015 11:06:59
Problema Factorial Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.53 kb
#include <fstream>

using namespace std;
int t,p,dr,stg,n,pu;
ifstream fin("fact.in");
ofstream fout("fact.out");
int main()
{
    fin>>p;
    stg=1;
    dr=2147483647;
    while((t!=p)&&(stg<dr))
    {
        n=(stg+dr)/2;
        t=0;
        pu=5;
        while(pu<=n)
        {
            t=t+n/pu;
            pu=pu*5;
        }
        if(t>p)
            dr=n;
        else
        if(t<p)
            stg=n;
    }
    n=n-n%5;
    if(t==p)
        fout<<n;
    else
        fout<<"-1";
    return 0;
}