hdu 1018 Big Number

题意:求阶乘的位数

解法一:利用斯特灵公式

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const double inf=1e20;
const int maxn=1e5+10;

const double e = 2.7182818284590452354;
const double pi = 3.1415926535897932385;

ll f(ll n){
    if(n==1)return 1;
    return (ll )ceil(0.5*log10(2*pi*n)+n*log10(n/e));
}

ll n;

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%lld",&n);
        printf("%lld\n",f(n));
    }
    return 0;
}

 

解法二:位数公式为log10(n)+1现在变成了log10(n!)+1,log内部乘等于外部加,所以直接相加就好了

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
const double inf=1e20;
const int maxn=1e5+10;

const double e = 2.7182818284590452354;
const double pi = 3.1415926535897932385;

ll f(ll n){
    if(n==1)return 1;
    return (ll )ceil(0.5*log10(2*pi*n)+n*log10(n/e));
}

ll n;

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        double num;
        scanf("%d",&n);
        num=0;
        for(int i=1;i<=n;i++)
            num+=(log10(i));
        printf("%lld\n",(ll)num+1);
    }
    return 0;
}

 

posted @ 2020-02-27 21:19  wz-Archer  阅读(...)  评论(...编辑  收藏