Cod sursa(job #1259764)

Utilizator adnionutCojocaru Ionut adnionut Data 10 noiembrie 2014 16:07:48
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
/*
Punctaj final pe sursa curenta: 100p
*/
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

ifstream fin("fact.in");
ofstream fout("fact.out");

int nr0(int z)
{
    int cnt = 0;
    while( z >= 5 )
    {
        cnt += z/5;
        z /= 5;
    }
    return cnt;
}

int main()
{
    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;
}