Cod sursa(job #1750897)

Utilizator giotoPopescu Ioan gioto Data 31 august 2016 13:54:55
Problema Factorial Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <cstdio>
using namespace std;

int n;
int p[]={5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125};
int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    scanf("%d", &n);
    if(n==0) {printf("1");return 0;}
    int st=1,dr=100000000/5;
    while(st<=dr){
        int mij=(st+dr)/2;mij=mij*5;
        long long j=0,legendre=0;
        while(p[j]<=mij&&j<=10){legendre=legendre+(mij/p[j]);++j;}
        mij=mij/5;
        if(legendre==n){printf("%d",mij*5);return 0;}
        else if(legendre<n) st=mij+1;
                       else dr=mij-1;
    }printf("-1");
    return 0;
}