Cod sursa(job #1737933)

Utilizator CrystyAngelDinu Cristian CrystyAngel Data 5 august 2016 12:50:10
Problema Factorial Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define nmax 8100101

int v[nmax];
int i,x,p;

int bs(int s,int d,int x)
{
    if(s>=d)
        return s;
    else
    {
        int a=(s+d)/2;
        if(v[a]==x)
            return a;
        if(v[a]>x)
            return bs(s,a-1,x);
        return bs(a+1,d,x);
    }
}

int main()
{
    for(i=1; i<nmax; ++i)
    {
        x=i;
        v[i]=v[i-1]+1;
        while(x-x/5*5==0)
        {
            x/=5;
            ++v[i];
        }
    }

    f>>p;
    x=bs(0,nmax-1,p);
    if(v[x]==p)
    {
        x=x*5;
        while(x%5!=0 && x>1)
            --x;
        g<<x;
    }
    else
        g<<-1;
}