Cod sursa(job #2495927)

Utilizator pro119Manea Dumitru pro119 Data 19 noiembrie 2019 23:35:15
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll p;
int fact(int x)
{
    int sum=0,j=5;
    while(j<=x)
    {
        sum+=(x/j);
        j*=5;
    }
    return sum;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ifstream cin("fact.in");
    ofstream cout("fact.out");
    cin>>p;
    if(p==0) cout<<1;
    ll l=1,r=10e8;
    bool aux=0;
    while(l<=r)
    {
        ll mid=l+(r-l)/2;
        ll temp=fact(mid);
        ll temp2=fact(mid-1);
        if (temp==p && temp2<p)
        {
            cout<<mid;
            aux=1;
            break;
        }
        else if (temp<p) l=mid+1;
        else r=mid-1;
    }
    if(!aux)
        cout<<-1;
    return 0;
}