Cod sursa(job #1754376)

Utilizator xSliveSergiu xSlive Data 8 septembrie 2016 00:16:53
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f("fact.in");
ofstream g("fact.out");

long long func(long long n){
    long long p=1,s=0;
    while(p <= n){
        p*=5;
        s+=n/p;
    }
    return s;
}

int main()
{
    long long st=1,dr = 100000000;
    long long mij;
    bool gasit = false;
    long long n0,p;
    f >> p;
    if(p == 0){
            g << 1;
            return 0;
    }
    while(st < dr && !gasit){
            mij = (st + dr) / 2;
            n0 = func(mij);
            if(n0 == p) gasit = true;
            else if(n0 < p) st = mij + 1;
            else dr = mij - 1;
    }
    if(!gasit)  g << -1;
    else{
        while(mij%5)    mij--;
        g << mij;
    }
    return 0;
}