Cod sursa(job #1334124)

Utilizator geaninmihaiBurcea Geanin-Mihai geaninmihai Data 3 februarie 2015 21:54:27
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include<fstream>
using namespace std;
long long st,dr,p,nr;
long long f(long long n)
{
    long long s,x;
    x=n;
    s=0;
    while(x!=0)
    {
        x=x/5;
        s=s+x;
    }
    return s;
}
int main()
{
     ifstream fin("fact.in");
    ofstream fout("fact.out");
    fin>>p;
    if(p==0)
    {
        fout<<1;
    }
    else
    {
        st=1;
        dr=2000000000;
        while(st<=dr)
        {
            nr=st+(dr-st)/2;
            if(f(nr)<p)
            {
                st=nr+1;
            }
            else
            {
                dr=nr-1;
            }
        }
        if(f(st)!=p && f(dr)!=p)
        {
            fout<<"-1";
        }
        else
        {
            if(f(dr)==p)
            {
                fout<<dr;
            }
            else
            {
                fout<<st;
            }
        }
    }
 
    return 0;
}