题目概述
给定数字N,求N!中所含数字的位数
时限
1000ms/2000ms
输入
第一行正整数times,其后times行,每行一个正整数N
限制
1<=N<=1e7
输出
每行一个数,如题目要求所示
样例输入
3
10
20
100000
样例输出
7
19
456574
讨论
如果你不知道斯特林(stirling)公式是什么,这个题八成是做不出来的
这便是斯特林公式,直接对其两部分求两个对数,加和即可
题解状态
0MS,1720K,581 B,C++
题解代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 22
#define memset0(a) memset(a,0,sizeof(a))
const double pi = atan(1.0) * 4;
const double e = 2.718281828459;//要么背过 要么去百度然后背过 库里没有这个东西
int fun(int N)
{
double a = 0.5*log10(2 * pi*N);
double b = N*log10(N / e);
return ceil(a + b);//向上取整 原因不必多言
}
int main(void)
{
//freopen("vs_cin.txt", "r", stdin);
//freopen("vs_cout.txt", "w", stdout);
int times;
scanf("%d", ×);//input
while (times--) {
int N;
scanf("%d", &N);//input
printf("%d\n", fun(N));//output
}
}
EOF