Cod sursa(job #2554213)

Utilizator BogdanTicuTicu Bogdan Valeriu BogdanTicu Data 22 februarie 2020 17:50:32
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <cmath>
using namespace std;

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



int zeros(int n)
{
    int ans=0;
    while(n)
    {
        ans+=(n/5);
        n/=5;
    }
    return ans;
}
int main()
{
    int p,ok=1,n;
    f>>p;
    n=5*p;
    if(p==0)
        g<<1;
    else
    {
        int st=5,dr=n,sol=-1,mij;
        while(st<=dr)
        {
            mij=(st+dr)/2;
            if(p<=zeros(mij))
            {
                sol=mij;
                dr=mij-1;
            }
            else
                st=mij+1;
        }
        if(sol==-1)
            g<<sol;
        else
        {
            sol-=sol%5;
            if(zeros(sol)==p)
                g<<sol;
            else
                g<<-1;
        }

    }
    return 0;
}