Cod sursa(job #1170561)

Utilizator silvatheviprersilviu catioiu silvatheviprer Data 13 aprilie 2014 20:24:39
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <fstream>
#include <math.h>
using namespace std;
ifstream f("fact.in");
ofstream g("fact.out");
double cinci=5,c[14]= {0,1,6,31,156,781,3906,19531,97656,488281,2441406,12207031,61035156,305175781};
long n;
int c_b(int l,int h)
{
    if (h-l<2)
        return 0;
    int mid=(l+h)/2,x=mid;
    long nr=0;
    while (x>0)
    {
        nr+=x/5;
        x/=5;
    }
    int ok=0;
    while (nr==n)
    {
        ok=1;
        mid--;
        nr=0;
        x=mid;
        while (x>0)
        {
            nr+=x/5;
            x/=5;
        }
    }
    if (ok)
    {
        g<<(mid+1)<<"\n";
        return 1;
    }
    if (nr>n)
        c_b(l,mid);
    else
        c_b(mid,h);
}
int main()
{
    f>>n;
    if (n==0)
        g<<1<<"\n";
    else
    {
        int i=0;
        while (n>c[i])
            i++;
        if (n==c[i])
        {
            double y=i;
            g<<pow(cinci,y)<<"\n";
        }
        else if (!c_b(pow(cinci,i-1),pow(cinci,i)))
            g<<"-1\n";
    }
    return 0;
}