Cod sursa(job #1327013)

Utilizator Vali_DeaconuVali Deaconu Vali_Deaconu Data 26 ianuarie 2015 12:11:20
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
long long p, n, nmin, nmax, i, j, k, c, s;
int main()
{
    fin >> p;
    if (p==0)
    {
        fout<<1;
        return 0;
    }
    if(p==1)
    {
        fout<<5;
        return 0;
    }
    n = 5;
    c = 1;
    while (c < p)
    {
        n = n*5;
        c = c*5 + 1;
    }
    nmax = n/5;
    nmin = n/5;
    c = (c-1)/5;
    while (c < p)
    {
        nmax *= 2;
        c *= 2;
    }
    nmin = nmax/2;
    c = 0;
    while (nmin <= nmax)
    {
        c = 0;
        i = (nmin+nmax) / 2;
        j = i;
        while(j)
        {
            c += j/5;
            j /= 5;
        }
        if(c > p)
            nmax = (nmin + nmax)/2 - 1;
        if(c < p)
            nmin = (nmin+nmax)/2+1;
        if(c == p)
        {
            for(j=i; c == p; j--)
            {
                c = 0;
                k=j;
                while(k)
                {
                    c=c+k/5;
                    k/=5;
                }
            }
            fout << j+2;
            return 0;
        }
    }
    fout<<-1;
    return 0;
}