您的位置:首页 > 其它

【HDU】 1018 Big Number

2014-10-12 10:58 281 查看
大意就是求 :

log10(n!) = log10(1 * 2 * 3 * .......*n) = log10(1) + log10(2) + ........+log10(n);

打表的话会MLE,直接递推就行了,后台数据不会很刁钻。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 10000000;
//double dp[maxn + 1];
//void List(){
//    dp[1] = log10(1.0);
//    for(int i = 2; i < maxn; i++)
//        dp[i] = dp[i - 1] + log10(1.0 * i);
//    return;
//}
int main(){
    int T;
    //List();
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        double ret = 0;
        for(int i = 1; i <= n; i++)
            ret += log10(1.0 * i);
        printf("%.f\n",ceil(ret));
    }
    return 0;
}


还有一种方法,就是斯特林公式



也就是log10(n!) = log(n!) / log(10) = ( n*log(n) - n + 0.5*log(2*π*n))/log(n);

直接公式就出来了,更快捷。

这里就不多说了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: