Cod sursa(job #2186027)

Utilizator sauron275Andrei Radu sauron275 Data 25 martie 2018 11:47:48
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int legendre(int n, int p)
{
    int ex = 0;
    int pp = p;
    while(n >= pp)
    {
        ex += n / pp;
        pp *= p;
    }
    return ex;
}

int cautbin(int a)
{
    int p = 1, u = 500000000, N = -1;
    while(p <= u)
    {
        int m = (p + u) / 2;
        int nrz = legendre(m, 5);
        if(a == nrz)
            N = m;
        if(a <= nrz)
            u = m - 1;
        else
            p = m + 1;
    }
    return N;
}

int main()
{
    int p;
    f >> p;
    g << cautbin(p);
    return 0;
}