Cod sursa(job #1257193)

Utilizator teo2mirceFMI Popescu Mircea teo2mirce Data 7 noiembrie 2014 13:34:04
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int nr0(int z)
{
    int cnt = 0;
    while( z >= 5 )
    {
        cnt += z/5;
        z /= 5;
    }
    return cnt;
}
int main()
{
    ifstream fin("fact.in");
    ofstream fout("fact.out");
    unsigned long long p;
    fin>>p;
    if(p==0){fout<<1;return 0;}
    long long x = 100000000000000000/2,n=0;
    long mi=1,ma=x*2;
        while(mi<=ma)
    {
        x=(mi+ma)/2;
        if(nr0(x)==p)
        {
            n=x;
            break;
        }
        else if(nr0(x)<p)
            mi=x+1;
        else
            ma=x-1;
    }
    if(n!=0)
    {
        while(n>0 && nr0(n)==p)
            n--;
        fout<<n+1;
    }
    else
        fout<<-1;
}