Cod sursa(job #1736757)

Utilizator CrystyAngelDinu Cristian CrystyAngel Data 2 august 2016 16:22:03
Problema Factorial Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define nmax 8000101

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)
        g<<x*5;
    else
        g<<-1;
}