Cod sursa(job #606848)

Utilizator 0pt1musOptimus 0pt1mus Data 10 august 2011 12:30:37
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>

using namespace std;

ifstream in;
ofstream out;

inline int f(int x)
{
    int p=0;

    for(int pow=5;pow<=x;pow*=5)
        p+=(x/pow);

    return p;
}

inline int BS(int P,int L,int R)
{
    if(L<R)
    {
        int M=L+(R-L)/2;
        int p=f(M);

        if(p<P) return BS(P,M+1,R);
        else
        if(p>P) return BS(P,L,M-1);
        else return M;
    }
    else return -1;
}

int main()
{
    int N,P,p;

    in.open("fact.in");
    in>>P;
    in.close();

    if(P==0) N=1;
    else
    {
        N=BS(P,1,500000000);
        while(1)
        {
            p=f(N-1);
            if(p==P) --N;
            else break;
        }
    }

    out.open("fact.out");
    out<<N<<'\n';
    out.close();

    return 0;
}