Cod sursa(job #2218338)

Utilizator alexsandulescuSandulescu Alexandru alexsandulescu Data 4 iulie 2018 11:56:21
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <bits/stdc++.h>

using namespace std;

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

long long P, dr, step, i;

long long zero(long long n) {
    int sum = 0;
    for(int put = 5; put <= n; put *= 5)
        sum += n / put;
    return sum;
}

int main()
{
    f >> P;
    dr = 5 * P;
    for(step = 1; step < dr; step <<= 1);
    for(i = 1; step; step >>= 1) {
        if(i + step <= dr && zero(i + step) <= P) {
            i += step;
        }
    }
    while(zero(i) == P && i >= 1) i--;
    i++;
    if(zero(i) == P)
        g << i << "\n";
    else
        g << "-1\n";
    return 0;
}