Cod sursa(job #1008250)

Utilizator Athena99Anghel Anca Athena99 Data 10 octombrie 2013 18:07:22
Problema Factorial Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>

using namespace std;

ifstream fin("fact.in");
ofstream fout("fact.out");

const int pmax= 100000000;
const int sol_max= 5*pmax;
const int n5max= 14; //smallest x such as x>=sol_max and x power of 5

int f[n5max];

int check ( int x )
{
    int sol= 0;
    for ( int i= 1; i<n5max; ++i ) {
        sol+= x/f[i];
    }

    return sol;
}

int main()
{
    int p;
    fin>>p;
    
    f[0]=1;
    for ( int i= 1; i<n5max; ++i ) {
        f[i]= f[i-1]*5;
    }

    int step;
    for ( step= 1; step<p*5; step*= 2 ) {
    }
    int sol;
    for ( sol= max(p*5, 1); step>0; step/=2 ) {
        if ( sol-step>0 && check(sol-step)>=p ) {
            sol-= step;
        }
    }

    fout<<sol<<"\n";

    return 0;
}