【HDU - 1018】【Big Number 】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/82114146

题目:

In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number. 

Input

Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 10 7 on each line. 

Output

The output contains the number of digits in the factorial of the integers appearing in the input. 

Sample Input

2
10
20

Sample Output

7
19

题意:求解n的阶乘有多少位,咳咳咳,这个题暴力也是可以过的,主要是数据出水了,所以一定会有捷径的,组合数学的公式

log10(n!) = log10(sqrt(2 * pi * n)) + n * log10(n / e) (不要问我咋咋知道的,百度n的阶乘的位数就会有)

只要对它变一下形就可以得到位数。

ac代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define PI 3.1415926
int num;
int n;

//log10(n!) = log10(sqrt(2 * pi * n)) + n * log10(n / e)

int solve()
{
	double t;
    int i;
    t = (num*log(num) - num + 0.5*log(2*num*PI))/log(10);
    //这步就是将它转换为上边的求位数的公式。
    int  ans = t+1;
   	return ans;
}
int main()
{
	cin>>n;
	while(n--)
	{
		cin>>num;
		cout<<solve()<<endl; 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/82114146