Cod sursa(job #1730090)

Utilizator Dragne.Andrei11Dragne Andrei Dragne.Andrei11 Data 16 iulie 2016 12:43:42
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <cstdio>

using namespace std;

inline int fct(int n)
{
    long long x=0;
    for(register int i=5;i<=n;i*=5)
        x=x+n/i;
    return x;
}
int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    int p;

    scanf("%d", &p);
    if(p==0)
    {
        printf("1\n");
        return 0;
    }
    int stg=1;
    int dr=1000000000;
    while(stg<=dr)
    {
        int med=stg+(dr-stg)/2;
        if(fct(med)<p)
            stg=med+1;
        else
            dr=med-1;
    }
    int med=stg+(dr-stg)/2;
    if(fct(med)>p)
        med--;
    if(fct(med)==p)
        printf("%d\n", med);
    else
        printf("-1\n");

    return 0;
}