Cod sursa(job #880272)

Utilizator paul.chPaul Chelarescu paul.ch Data 16 februarie 2013 16:07:00
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
//#include <fstream>
//using namespace std;
//ifstream fin ("fact.in");
//ofstream fout ("fact.out");
//long long p, m, mijloc, dreapta, stanga, solutie, temp;
//inline long long cati_cinci(long long numar)
//{
//    long long nr = 0;
//    while (numar >= 5 )
//    {
//        nr += numar / 5;
//        numar /= 5;
//    }
//    return nr;
//}
//int main()
//{
//    fin >> p;
//    stanga = 1;
//    dreapta = 100010000;
//    if(p == 0)
//    {
//        fout << "1";
//    }
//    else
//    {
//        while(dreapta >= stanga)
//        {
//            mijloc = (dreapta + stanga)/ 2;
//            temp = cati_cinci(mijloc);
//            if(temp > p) dreapta = mijloc - 1;
//            if(temp < p) stanga = mijloc + 1;
//            if(temp == p)
//            {
//                solutie = mijloc;
//                dreapta = mijloc - 1;
//            }
//        }
//        fout << solutie;
//    }
//    return 0;
//}
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("fact.in");
ofstream g("fact.out");


long long st,dr,mij,p,sol;

long long vrf(int x)
{
    long long nr=0;
    while(x>=5)
    {
        nr+=x/5;
        x/=5;
    }
    return nr;
}
int main()
{
    f>>p;
    if(p==0)g<<1;
        else
        {
            st=1;
            dr=500000000;
            sol=-1;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(vrf(mij)>p)dr=mij-1;
                    else if(vrf(mij)<p)st=mij+1;
                    else {sol=mij;dr=mij-1;}

        }
        g<<sol;
}
return 0;
}