Cod sursa(job #3132381)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 22 mai 2023 16:30:42
Problema Factorial Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>
using namespace std;
int fact(int n, int k)
{
    int s = 0;
    while(n > 1)
    {
        s = s + n / k;
        n = n / k;
    }
    return s;
}
int p, st, dr, mid, ok;
ifstream f("fact.in");
ofstream g("fact.out");
int main()
{
    ios_base::sync_with_stdio(0);
    f.tie(0);
    g.tie(0);
    f >> p;
    st = 0, dr = 1e8, mid;
    if(p == 0)
    {
        g << 1;
        return 0;
    }
    while(st <= dr)
    {
        mid = st + (dr - st) / 2;
        if(fact(mid, 5) == p)
        {
            g << (mid / 5) * 5;
            ok = 1;
            return 0;
        }
        else
        {
            if(fact(mid, 5) < p)
            {
                st = mid + 1;
            }
            else
            {
                dr = mid - 1;
            }
        }
    }
    if(!ok)
    {
        g << -1;
    }
    return 0;
}