Cod sursa(job #1694273)

Utilizator RobertLISARURobert Lisaru RobertLISARU Data 25 aprilie 2016 03:25:28
Problema Factorial Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>

using namespace std;

int trailingZeros(int n){
    int cnt = 0;
    while(n!=0){
        n = n/5;
        cnt += n;
    }
    return cnt;
}

int main(){
    int p;

    ifstream fin("fact.in");
    fin>>p;
    fin.close();

    ofstream fout("fact.out");

    int a = 0; int b = 5*p;
    int mid;
    int midZeros;

    if(p==0){
        fout<<1;
        goto exit;
    }

    while(a!=b){
        mid = (a+b)/2;
        midZeros = trailingZeros(mid);
        if(midZeros>p){
            b=mid;
            continue;
        }
        if(midZeros<p){
            a=mid;
            continue;
        }
        goto found;
    }

    fout<<-1;
    goto exit;

    found:
    fout<<mid-mid%5;

    exit:
    fout.close();

    return 0;
}