Cod sursa(job #2281006)

Utilizator alexnigaNiga Alexandru alexniga Data 11 noiembrie 2018 15:14:16
Problema Factorial Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const long long maxx = 100000000000;

int check(long long mij, long long n)
{
    long long a = 5, s=0;

    while(mij / a > 0)
    {
        s = s + mij / a;
        a *= 5;
    }


    return s;

}

int nr = 1, poz = -1;
void cautbinar(long long st, long long dr, long long n)
{
    if(st <= dr)
    {
        long long mij = (st + dr) / 2;

        int verf = check(mij, n);

        if (verf == n && (check(mij - 1, n) < n))
        {
            poz = mij;
            return;
        }
        if (verf >= n)
            cautbinar(st, mij - 1, n);
        else
            cautbinar(mij + 1, dr, n);
    }
}

int main()
{
    unsigned long long p;

    f >> p;

    if(p == 0)
        g << 1;
    else
        if(p == 1)
            g << 5;
        else
            {
                cautbinar(1, maxx, p);
                if(poz != -1)
                    g<< poz;
                else
                    g << poz;
            }

    return 0;
}