Cod sursa(job #881461)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 17 februarie 2013 23:41:44
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 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 = 1, dr = 5 * P, mij;

    while( st <= dr ){

        mij = (st + dr) / 2;

        if( Legendre(mij, 5) < P )
            st = mij + 1;
        else
            dr = mij - 1;
    }

    if(Legendre(st, 5) == P)
        return st;
    else
        return -1;
}

int main(){

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

    f >> P;

    if(P == 0)
        g << -1;

    else
        g << cautare_binara();

    return 0;
}