Cod sursa(job #2156317)

Utilizator YetoAdrian Tonica Yeto Data 8 martie 2018 17:23:55
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <fstream>
using namespace std;
int p, n, i, j, cnt, x, k, pmax, mult;

int main () {
    ifstream fin ("fact.in");
    ofstream fout ("fact.out");
    fin>>p;
    if (p==0) {
        fout<<1;
        return 0;
    }
    n=5*p;
    k=1;
    while (k<=n) {
        k*=5;
        pmax++;
    }
    pmax--;
    if (pmax==1) {
        fout<<5*p;
        return 0;
    }

    for (i=pmax;i>=2;i--) {
        x=1;
        cnt=0;
        for (j=1;j<=i;j++)
            x*=5;
        ///fout<<x<<" ";
        mult=x;
        do {
            cnt++;
            x+=mult;
        } while (x<=n);

        cnt--;
        n-=cnt*5;
    }

    fout<<n;
    return 0;
}