Cod sursa(job #2594402)

Utilizator r_moraruRadu-Andrei Moraru r_moraru Data 5 aprilie 2020 21:52:27
Problema Factorial Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <iostream>
#include <fstream>

using namespace std;

long long nr_zerouri(long long numar)
{
    long long solutie = 0;
    for(long long i = 5; i <= numar; i*=5)
    {
        solutie += numar/i;
    }   //face suma de numar impartit la 5^1, ^2, ^k....
    return solutie;
}

int main()
{
	fstream fin("fact.in", ios::in);
	fstream fout("fact.out", ios::out);

    long long minim = 1, maxim = 400000015, mijloc, numar; //limitele lui n
    bool gasit = false;
	int p;
	fin >> p;

	if (p == 0)
    {
        fout << 1;
    }
    else
    {
        while(minim < maxim)
        {
            //cautare binara
            mijloc = (minim + maxim)/2;
            cout << "Minim: " << minim << endl;
            cout << "Mijloc: " << mijloc << endl;
            cout << "Max: " << maxim << endl;
            numar = nr_zerouri(mijloc);
            if(numar == p)
            {
                fout << mijloc - mijloc%5;
                gasit = true;
                break;
            }
            else if(numar < p)
            {
                minim = mijloc + 1;//cauta la dreapta
            }
            else
            {
                maxim = mijloc - 1;//cauta la stanga
            }

        }
        if (gasit == false)
        {
            fout << -1;
        }
    }
}