(hdu step 2.3.3)Big Number(求N!的位数)

在写题解之前给自己打一下广告哈~。。抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下:

http://edu.csdn.net/course/detail/209

题目:

Big Number

Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 951 Accepted Submission(s): 640
 

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

 

Source

Asia 2002, Dhaka (Bengal)


Recommend

JGShining

题目大意:

这个题的意思是:给你一个数,让你求出N!由多少位数构成,比如输出10,它的阶乖是3628800 由7位数构成,这时你要输出7;

题目分析:

这道题对于C/C++的写法来说,n太大阶乘出来的结果已经超出了所能表示的整数的范围。这时候主要有以下两种思考:

1)用科学计数法a*10^n来表示N!。那么这时候n+1就是N!的位数。(其实这种想法就是直接暴力,只不过是在暴力之前做了以下处理。因为N!的范围太大,C/C++的整数范围内存不下)

N的阶乖的位数等于LOG10(N!)=LOG10(1)+.....LOG10(N);

2)用斯特林公式获得N!的近似值。然后计算这个近似值的位数。

需要注意的是:

用第一种方法同样是暴力,他只不过使用对数来处理一下数字太大的问题。既然这样的话,那么是不是使用java来直接求N!就行了呢?我试了一下,是会TLE的。因为java会比C/C++慢3倍以上。而这道题的时间却没有同等程度的放宽。

代码如下:

/*
 * c.cpp
 *
 *  Created on: 2015年2月3日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		int i;
		double ans = 0;
		for(i = 1 ; i <= n ; ++i){
			ans += log10(i);
		}

		printf("%d\n",(int)ceil(ans));//ceil()函数用于向上取整.
	}

	return 0;
}
时间: 2025-01-10 15:36:21

(hdu step 2.3.3)Big Number(求N!的位数)的相关文章

(hdu step 3.1.5)Tiling_easy version(求用2*1、2*2两种骨格铺满2*n的网格的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Tiling_easy version Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 515 Accepted Submission(s): 447   Prob

(hdu step 4.2.3)Knight Moves(求从起点是否能够到达终点的最小步数)

题目: Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 453 Accepted Submission(s): 326   Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where

(hdu step 5.1.5)Dragon Balls(求并查集的根节点、节点数和个结点的移动次数)

题目: Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 562 Accepted Submission(s): 239   Problem Description Five hundred years later, the number of dragon balls will increase unexpected

(hdu step 7.1.5)Maple trees(求凸包的最小覆盖圆的半径)

题目: Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 177 Accepted Submission(s): 63   Problem Description There are a lot of trees in HDU. Kiki want to surround all the trees with the m

(hdu step 5.1.3)Segment set(求与一条线段相交的线段集合中的线段的数量)

题目: Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 177 Accepted Submission(s): 82   Problem Description A segment and all segments which are connected with it compose a segment set. T

(hdu step 1.3.6)Wooden Sticks(求长度和重量都严格递增的数列的个数)

题目: Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2374 Accepted Submission(s): 921   Problem Description There is a pile of n wooden sticks. The length and weight of each stick are

(hdu step 6.3.1)Strategic Game(求用最少顶点数把所有边都覆盖,使用的是邻接表)

题目: Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 110 Accepted Submission(s): 75   Problem Description Bob enjoys playing computer games, especially strategic games, but sometim

(hdu step 3.1.6)统计问题(求不断地左右走、向上走n步的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: 统计问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 732 Accepted Submission(s): 466   Problem Description

(hdu step 6.1.3)畅通工程(求需要添加多少条边才能让n个点连通)

题目: 畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 163 Accepted Submission(s): 132   Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道