Cod sursa(job #1577564)

Utilizator dsergiu05Sergiu Druga dsergiu05 Data 23 ianuarie 2016 16:03:45
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <fstream>

using namespace std;

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

const int nmax=1000000;
int v[5*nmax+1];

int main () {
    int n;
    fin>>n;

    int n2;
    for (n2=1; n2<=5*nmax; n2*=2) {
    }
    n2/=2;

    for (int i=1; i<=5*nmax; i++) {
        int j=i;
        while (j/5!=0) {
            v[i]=v[i]+j/5;
            j=j/5;
        }
    }

    int sol=5*nmax;
    for (int step=n2; step>0; step/=2) {
        if (sol-step>=1 && v[sol-step]>=n) {
            sol=sol-step;
        }
    }

    if (v[sol]==n) {
        fout<<sol<<"\n";
    } else {
        fout<<"-1\n";
    }

    return 0;
}