Cod sursa(job #1284149)

Utilizator Johnny07Savu Ioan-Daniel Johnny07 Data 6 decembrie 2014 12:00:18
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
ifstream f("fact.in");
ofstream g("fact.out");
long long p,pow,sum,ld,ls,mid,tot,k;

int solve (int q)
{
    int sump=0;
    int pow2=1;
    while (pow2<q)
    {
        pow2*=5;
        sump=sump+q/pow2;
    }
    return sump;
}


int main()
{
f>>p;
if (p!=0)
{pow=1;
sum=1;
while (sum<p)
{
    pow*=5;
    sum=(pow*5-1)/4;
}
pow*=5;
ld=pow;
ls=pow/5;
while (ls<ld)
{

mid=(ls+ld)/2;
tot=solve(mid);
if (tot<p) ls=mid+1;
if (tot>=p) ld=mid-1;
if (ls==ld) break;
}
k=(ls+ld)/2;
tot=solve((ls+ld)/2);
if (tot<p) k++;
tot=solve(k);
if (tot==p) g<<k;
else g<<"-1";
}
else g<<1;

    return 0;
}