Cod sursa(job #1759739)

Utilizator PondorastiAlex Turcanu Pondorasti Data 19 septembrie 2016 19:09:34
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;
int five0(int n);
int main()
{
    ifstream in("fact.in");
    ofstream out("fact.out");
    int p,st=1,dr=2000000000,mij;
    in>>p;
    if(p==0)
    {
        out<<"1";
        return 0;
    }
    while(dr>=st)
    {
        mij=(dr+st)/2;
        if(five0(mij)<p)
        {
            st=mij+1;
        }
        else
        {
            dr=mij-1;
        }
    }
    if(five0(mij)==p)
        out<<mij;
    else
        out<<"-1";
    return 0;
}
int five0(int n)
{
    int n5=5,nr=0;
    while(n5<=n)
    {
        nr=nr+n/n5;
        n5=n5*5;
    }
    return nr;
}