码迷,mamicode.com
首页 > 其他好文 > 详细

【HDU】 1018 Big Number

时间:2014-10-12 13:46:48      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   数据   

大意就是求 :

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;
}

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


bubuko.com,布布扣


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

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

这里就不多说了。

【HDU】 1018 Big Number

标签:style   blog   http   color   io   os   ar   for   数据   

原文地址:http://blog.csdn.net/u013451221/article/details/40016195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!