题目描述
夫夫有一天对一个数有多少位数感兴趣,但是他又不想跟凡夫俗子一样,
所以他想知道给一个整数n,求n!的在8进制下的位数是多少位。
输入描述:
第一行是一个整数t(0
输出描述:
输出n!在8进制下的位数。
示例1
输入
3
4
2
5
输出
2
1
3
题意: 略
分析: 之前做过求 n!
在十进制下的都知道,斯特林公式的妙用,今天又到了,首先这个公式呢在n越大的时候越精确,我们只需要做个小小的变形即可,将以10为低取对数,变成以8 即可,具体公式的推导请参考这里
参考代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1.0)
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll s[100];
int main(){
int T;cin>>T;
s[0] = 1;
for(int i = 1;i <= 20;i++) {
s[i] = s[i - 1] * i;
}
while(T--) {
int x;x = read();
if(x <= 20) {
printf("%lld\n",(ll)(log(s[x])/log(8)) + 1);
} else {
ll res = (ll)((0.5*log(2*PI*x) + x*log(x) - x)/log(8));
printf("%lld\n",res + 1);
}
}
return 0;
}
- 如有错误或遗漏,请私聊下UP,thx