Cod sursa(job #673322)

Utilizator al.mocanuAlexandru Mocanu al.mocanu Data 4 februarie 2012 12:12:01
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>
#include <math.h>
using namespace std;

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

long long P,x,i,y,j,z,st,dr,a,b;

int main()
{f>>P;
if(P==0)
g<<'1';

else
{j=0;
i=1;
while(j==0)
{x=floor(pow(5,i));
z=0;
while(x>0)
{
    x=x/5;
    z=z+x;
}
if(P<z)
j=i;

else
i++;
}

st=floor(pow(5,j-1));
dr=floor(pow(5,j));
a=st;
b=dr;
while(a>0)
{
    a=a/5;
    x=x+a;
}
if(x==P)
g<<st;
a=st;
while((x!=P)&&(st<b-4)&&(dr>a+4))
{y=(st+dr)/2;
z=y;
x=0;
while(z>0)
{
    z=z/5;
    x=x+z;
}

if(x==P)
{
    while(y%5!=0)
    y--;
    g<<y;
}
else
{
    if(x>P)
    dr=y;

    else
    st=y;
}
}

if(x!=P)
g<<"-1";}

    return 0;
}