POJ 1423:big Number Math

Source: Internet
Author: User
Tags acos integer numbers
Big number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27537 Accepted: 8787

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

Input input consists of several lines of the integer numbers. The first line contains a integer n, which is the number of cases to being tested, followed by n lines, one integer 1 <= M <= 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
20

Sample Output

7
19
Ideas:
Seek log10 (n!) for the number of digits of the factorial of N. =LOG10 (1) +log10 (2) +......log10 (n)

another formula is given in the art of computer programming.

n! = sqrt (2*π*n) * ((n/e) ^n) * (1 + 1/(12*n) + 1/(288*n*n) + O (1/n^3))

π= ACOs ( -1)

e = exp (1)

10 logarithm on both sides

Ignore log10 (1 + 1/(12*n) + 1/(288*n*n) + O (1/n^3)) ≈log10 (1) = 0

Get the formula

log10 (n!) = log10 (sqrt (2 * pi * n)) + N * log10 (n/e) . Baidu to the ... Forgive me for not being good at maths ~ ~ ~ Ac-code:

#include <iostream>
#include <cmath>
using namespace std;
Double E=exp (1.0), Pi=acos ( -1.0);
int main ()
{
	int T;
	Long long N,ans;
	cin>>t;
	while (t--)
	{
		cin>>n;
		Ans=int (log10 (sqrt (2.0*pi*n)) +n*log10 (n/e)) +1;
		cout<<ans<<endl;
	 } 
	 return 0;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.