HDU 1018 Big Number (log函数求数的位数)

Problem Description

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 ≤ 107 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

123456=1.23456*10^5;

log10(123456)=5.09151;

log10(1.23456*10^5)=log10(1.23456)+log10(10^5)=0.09151+5;

故int(log10(n))+1 就是n的位数

1、x的位数=(int)log10(x)+1;

2、斯特林近似公式:n!≈sqrt(2*π*n)*(n/e)^n。

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
	int i,t,n;
	double ans;
	cin>>t;
	while(t--){
		cin>>n;
		ans=0;
		for(i=1;i<=n;i++) {
			ans+=log10(double(i));
		}
		printf("%d\n",int(ans)+1);
	}
	return 0;
}
时间: 2024-08-27 15:27:02

HDU 1018 Big Number (log函数求数的位数)的相关文章

HDU 1018 -- Big Number (Stirling公式求n!的位数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 40551    Accepted Submission(s): 19817 Problem Description In many applications ver

HDU 1018 Big Number 数学题解

Problem Description 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

HDU 1018 Big Number (简单数学)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25649    Accepted Submission(s): 11635 Problem Description In many applications very large integers numbers are required. Some of these

HDU 1018 Big Number (数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x) + 1; 所以n!的位数 = (int)log10(1*2*3*.......n) + 1; = (int)(log10(1) + log10(2) + log10(3) + ........ log10(n)) + 1; 1 #include<cstdio> 2 #include<cs

HDU 1018 Big Number 数学题

Problem Description 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

nefu26(求数的位数)

Description 根据密码学需要,要计算某些数的阶乘的位数. Input 第一行为整数n ,接下来 n 行, 每行1个数m (1 ≤ m ≤ 10^7) . Output 输出m的阶乘的位数. Sample Input 2 10 20 Sample Output 7 19 思路:求n的位数:(int)log10(n)+1, log(a*b)=log(a)+log(b) #include <cstdio> #include <cmath> using namespace std

hdu 1018 Big Number 两种方法 log方法(300+ms)+斯特林公式(0+ms)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 28178    Accepted Submission(s): 12819 Problem Description In many applications very large integers numbers are required. Some of thes

HDU 1018 Big Number 斯特林数近似n!

题目链接:点击打开链接 斯特林数:点击打开链接 题意是计算n! 的位数 即ans = log10(n!) = log10(sqrt(2πn)) + n*log10(n/e) #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <

HDU 1394- Minimum Inversion Number(线段树求逆序数)

Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1394 Appoint description:  System Crawler  (2015-04-13) Description The inversion number of a given number sequence a1, a