Cod sursa(job #1331799)

Utilizator bogdanciurezubogdan ciurezu bogdanciurezu Data 1 februarie 2015 11:14:32
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
#include <cmath>
#define nmax 400000020
using namespace std;
ifstream f("fact.in");
ofstream g("fact.out");
int numar, P;
typedef long long tip;
tip nrZero(tip c){

    tip p = 0;

     while(c){

            p += c / 5;
            c /= 5;
     }

     return p;
}
int main()
{tip a = 0, b = 2e10, S, i;

    f >> P;

    if(P == 0) g<<1<<'\n';
    else{

        while(a <= b){

            S = (a + b) / 2;
            i = nrZero(S);
            //g<<a<<' '<<b<<' '<<S<<' '<<i<<'\n';

            if(i < P){
                a = S + 1;
            }
            else if(i > P){
                b = S - 1;
            }
            else if(i == P){
                b = -5;
                while(S % 5 != 0) --S;
                g << S << '\n';
            }
        }
        if(b != -5) g<< -1 << '\n';
    }

    return 0;
}