码迷,mamicode.com
首页 > 其他好文 > 详细

ACM--大数阶乘位数--HDOJ 1018--Big Number--水

时间:2016-08-02 13:44:30      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:


HDOJ题目地址:传送门


Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34843    Accepted Submission(s): 16536


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



题意:给你一个m,求m!的位数。

数学证明:

假设m! = a * 10 ^ b ,求m! 的位数(10>a>=1,b∈R)。

对公式两边取log10();

得到: log10(a * 10 ^ b) = log10(m!);

化解公式:log10(a) + log10(10 ^ b) = log10(1 * 2 *3 * ....... * m);

-------->>  log10(a) + b = log10(1)+ log10( 2)  + log10(3) + . . . . . .  + log10(m);

所以m!的位数为: b + 1 = log10(1)+ log10( 2)  + log10(3) + . . . . . .  + log10(m) - log10(a) + 1。

因此可以近似的写成 b + 1 <= log10(1)+ log10( 2)  + log10(3) + . . . . . .  + log10(m)  + 1 

(去掉 log10(a) < 1,毕竟a不好求,且10>a>=1  )。


测试例子:

1! = 1 = 1.0 * 10 ^ 0------------------------------------------------------------------- 1位 ->log10(1) = 0.0

2! = 2 = 2.0 * 10 ^ 0------------------------------------------------------------------- 1位 ->log10(2) = 0.3010299956639812

3! = 6 = 6.0 * 10 ^ 0------------------------------------------------------------------- 1位 ->log10(3) = 0.47712125471966244

4! = 24 = 2.4 * 10 ^ 1----------------------------------------------------------------- 2位 ->log10(4) = 0.6020599913279624

5! = 120 =  1.2 * 10 ^ 2--------------------------------------------------------------- 3位 ->log10(5) = 0.6989700043360189

6! = 720 = 7.2 * 10 ^ 2---------------------------------------------------------------- 3位 ->log10(6) = 0.7781512503836436

7! = 5040 =  5.04 * 10 ^ 3------------------------------------------------------------ 4位 ->log10(7) = 0.8450980400142568

8! = 40320 = 4.032 * 10 ^ 4--------------------------------------------------------- 5位 ->log10(8) = 0.9030899869919435

9! = 362880 = 3.6288 * 10 ^ 5;--------------------------------------------------- 6位 ->log10(9) = 0.9542425094393249

10!= 3628800 = 3.6288 * 10 ^ 6;---------------------------------------------- 7位 ->log10(10) = 1.0

log10(3.6288 * 10 ^ 6) = log10(3.6288) + log10(10*6) = 0.5597630328767937 + 6;

所以  位数( m! ) = b + 1;





import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
	   Scanner sc = new Scanner(System.in);  
       int n = sc.nextInt();  
       int m = 0, answer;  
       while (n-- > 0) {  
           m = sc.nextInt();  
           double sum = 0.0;  
           for (int i = 1; i <= m; i++) {  
               sum += Math.log10(i * 1.0);  
           }  
           answer = (int) sum + 1;  
           System.out.println(answer);  
	   }
   }
}

   
   


ACM--大数阶乘位数--HDOJ 1018--Big Number--水

标签:

原文地址:http://blog.csdn.net/qq_26891045/article/details/52093066

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!