Length fact spoj problem

LENGFACT

#include <stdio.h>
#include <math.h>

long long solve(long long n){
    if(n==0 || n==1) return 1;
    if(n <= 10) return (long long)log10(tgamma(n+1)) + 1;
    return (long long)((double)n*log10((double)n/M_E) + 0.5*log10(2*M_PI*n)) + 1;
}

int main(){
    long long n;
    int T;
    for(scanf("%d",&T);T--;){
        scanf("%lld",&n);
        printf("%lld\n",solve(n));
    }
    return 0;
}

No comments:

Post a Comment