Cod sursa(job #1663150)

Utilizator AlexTheDAlexandru Donc AlexTheD Data 25 martie 2016 16:12:43
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <cstdio>

using namespace std;
int legendre(int n)
{
    int s=0;
    long long numitor;
    numitor=5;
    while(numitor<=n)
    {
        s=s+n/numitor;
        numitor*=5;
    }
    return s;
}
int main()
{
    int P,st,dr,med,last;
    scanf("%d",&P);
    st=1;
    dr=(1LL<<31)-1;
    while(st<=dr)
    {
        med=dr-(dr-st)/2;
        if(legendre(med)>=P)
        {
            dr=med-1;
            last=med;
        }
        else
            st=med+1;
    }
    if(legendre(last)==P)
        printf("%d\n",last);
    else
        printf("-1");
    return 0;
}