HDU -- 1018 -- water question-use Sterling ..

Source: Internet
Author: User

Very watery question...

Calculates the number of digits of a factorial ..

Touch me

Sterling formula:

Or more precisely or I didn't know the formula at the beginning, so I used my own method to simply use Log (x * Y) = logX + logy ================= log (1*2*3 .... * n) = log1 + log2 + log3 + .... + logn shows a good formula for this introduction.
/*************************************** * ************ This question requires the number of digits of the N factorial, if n is large, the factorial of N must be a large number. 1 <= n <10000000, when N = 10000000, it can be said that the factorial of N will be a very huge number. For the problem of dealing with large numbers, we generally use strings. When n gets the maximum value, it is the product of multiplying 10 million numbers. It is too large. Even if it is difficult to store it in a string, and multiplying 10 million numbers involves multiplication of large numbers. Multiplication of large numbers is time-consuming, the calculation result usually times out. This makes us have to discard this direct method. Think about it again. This is the number of digits of the factorial of N, and the factorial of N is the product of the number of N, so we can just break down this problem. Before that, we must know that the number of digits of any positive integer a is equal to (INT) log10 (A) + 1. Why? Let's deduce the following: for any given positive integer a, suppose 10 ^ (x-1) <= A <10 ^ X, then the number of digits of a is obviously X, because log10 (10 ^ (x-1) <= log10 (a) <(log10 (10 ^ X) is the X-1 <= log10 (a) <X then (INT) log10 (A) = X-1, that is, (INT) log10 (A) + 1 = X that is, the number of digits of a is (INT) log10 () + 1 we know that the number of digits of a positive integer is equal to (INT) log10 (A) + 1. Now we want to calculate the number of digits of the factorial of N: Suppose A = n! = 1*2*3 *...... * n, so what we need is (INT) log10 (A) + 1, and: log10 (A) = log10 (1*2*3 *...... n) (according to log10 (A * B) = log10 (A) + log10 (B) = log10 (1) + log10 (2) + log10 (3) + ...... + log10 (n) Now we have finally found a solution and solved the problem. We break down the number of digits of the factorial of N into the sum of the logarithm of the number of N to 10, and any number is in the normal numeric range. To sum up, the number of factorial digits of N is equal to (INT) (log10 (1) + log10 (2) + log10 (3) + ...... + log10 (n )) + 1 based on this idea, we can easily write out the program ****************************** **********************/
--- "A little long but detailed
 1 #include <iostream> 2 #include<math.h> 3 using namespace std; 4  5 #define PI 3.141592657 6 #define E 2.71828182845904523536028747135266250 7  8 int main() 9 {10     int t , n;11     double cnt;12     while( cin >> t )13     {14         while( t-- )15         {16             cin>>n;17             cnt = log10( sqrt( 2*PI*n ) ) + n * log10(n/E);18             cout << (int)(cnt+1) << endl;19         }20     }21     return 0;22 }
View code
 1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4  5 int main() 6 { 7     cin.sync_with_stdio(false); 8     int t , n; 9     double cnt;10     while( cin >> t )11     {12         while( t-- )13         {14             cnt = 0;15             cin >> n;16             for( int i = 1 ; i<=n ; i++ )17             {18                 cnt += log10(i*1.0);19             }20             cout << (int)(cnt+1) << endl;21         }22     }23     return 0;24 }
View code

 

Today:

Different Flowers

Different sorrows

To ask about Acacia

When flowers bloom

 

 

 

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.