Mai intai trebuie sa te autentifici.
Cod sursa(job #1183514)
Utilizator | Data | 9 mai 2014 15:16:21 | |
---|---|---|---|
Problema | Factorial | Scor | 55 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 0.8 kb |
#include <iostream>
#include <fstream>
using namespace std;
long long fact(long long n){
int x = 5,rez=0;
while(x <= n){
rez += n / x;
x *=5;
}
return rez;
}
int main()
{
ifstream f("fact.in");
ofstream g("fact.out");
long long n,p;
f>>p;
long long st = 1, dr = 100000000, ok = 0, m;
while(st < dr){
m = (st + dr) / 2;
if(p > fact(m)){
st = m + 1;
}
if(p < fact(m)){
dr = m - 1;
}
if(p == fact(m)){
ok = 1;
break;
}
}
while(fact(m) >= p){
if(fact(m) >= p)
--m;
}
if(ok)
g<<m+1;
else
g<<"-1";
return 0;
}