Cod sursa(job #1587350)
Utilizator | Neghina Dragos dragos231456 | Data | 1 februarie 2016 22:42:54 |
---|---|---|---|
Problema | Factorial | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 0.92 kb |
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int c,lt,rt,p,n,s,x;
bool t=false;
ifstream f("fact.in");
ofstream g("fact.out");
f>>p;
if(p==0) g<<"1";
else{
rt=p*5;
lt=0;
while(rt-lt!=5)
{
c=(lt+(rt-lt)/2)/5;
c*=5;
x=1;
s=0;
n=0;
while(x*5<=c)
{
s++;
x*=5;
}
x=1;
for(int i=1;i<=s;++i)
{
x*=5;
n+=(c/x);
}
if(n<p) lt=c;
else if(n>p) rt=c;
else {t=true; break;}
}
if(!t)
{
c=rt;
x=1;
s=0;
n=0;
while(x*5<=c)
{
s++;
x*=5;
}
x=1;
for(int i=1;i<=s;++i)
{
x*=5;
n+=(c/x);
}
if(n==p) t=true;
}
if(t) g<<c;
else g<<"-1";
}
}