Cod sursa(job #881455)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 17 februarie 2013 23:28:54
Problema Factorial Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>

using namespace std;

int P;

int Legendre(int numar, int fact){

    int s = 0;

    while( numar >= fact )
        s += numar / fact,
        fact *= 5;

    return s;
}

int cautare_binara(int st, int dr){

    int mij = (st + dr) / 2;

    if(st > dr)
        return -1;

    if( Legendre(mij, 5) == P ){

        for(int i = st; i <= mij; i++)
             if( Legendre(mij, 5) == P )
                return i;
    }
    else
        if( Legendre(mij, 5) < P )
            return cautare_binara(mij+1, dr);
        else
            return cautare_binara(st, mij-1);
}

int main(){

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

    f >> P;

    if(P == 0)
        g << 1;
    else
        g << cautare_binara(1, 5*P);

    return 0;
}