Cod sursa(job #1693888)

Utilizator Alexandru_DanielAlexandru Alexandru_Daniel Data 24 aprilie 2016 04:39:22
Problema Factorial Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 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 = 400000016;
    int m;

    while(a!=b){
        m = (a+b)/2;
        if(trailingZeros(m)>p){
            b=m;
        }
        else if(trailingZeros(m)<p){
            a=m;
        }
        else {
            goto found;
        }
    }
    fout<<-1;
    goto exit;

    found:
    while(trailingZeros(m)==p) m--;
    fout<<m+1;

    exit:

    fout.close();

    return 0;
}