Cod sursa(job #1750889)

Utilizator giotoPopescu Ioan gioto Data 31 august 2016 13:39:58
Problema Factorial Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.64 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;
    while(1==1){
        int mij=(st+dr)/2;
        int j=0,legendre=0;
        while(p[j]<=mij&&j<=10){
            legendre=legendre+(mij/p[j]);
            ++j;
        }
        if(legendre==n){printf("%d",mij-mij%5);return 0;}
        else if(legendre<n) st=mij+1;
                       else dr=mij-1;
    }
    return 0;
}