Cod sursa(job #2857649)

Utilizator toma_ariciuAriciu Toma toma_ariciu Data 25 februarie 2022 23:59:19
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("fact.in");
ofstream fout("fact.out");

int ans, pow2 = (1 << 30);

int p5(int x)
{
    int sol = 0, i = 5;
    while(i <= x)
    {
        sol += x / i;
        i = i * 5;
    }
    return sol;
}

void solve(int n)
{
    if(n == 0)
    {
        fout << 1 << '\n';
        return;
    }
    for(int i = pow2; i > 0; (i >>= 1))
    {
        if(p5(ans + i) < n)
            ans += i;
    }
    if(p5(ans + 1) == n)
        fout << ans + 1 << '\n';
    else
        fout << -1 << '\n';
}

int main()
{
    int x;
    fin >> x;
    //for(int i = 1; i <= 10000; i++)
        solve(x);
    return 0;
}