Poj 1401 Factorial

Source: Internet
Author: User

This question is N! The number of zeros that follow. Note that N may be up to 9 times of 10. Haha, how many 2 and 5 factors can be enumerated directly for 1-N, and then a small value will definitely time out.
However, I tried again and time out.
Then we can only draw a mathematical conclusion, and think of N/2 numbers that can be divisible by 2 in 1-N. Haha, think about it later and find that there are N/4 numbers that can be divisible by 4 in 1-N.
That is, N/8, until a certain time of N divided by 2 is 0, then all the values are the number of two factors. This is also the method for finding the number of factors in step 5.
Obviously, the number of 5 factors must be smaller than or equal to the number of 2 factors. Then, you can directly calculate the number of five factors. Because, N/5 is used to take down an integer,
Therefore, we cannot use the Sum Formula of the proportional series. We do not know how to make the answer into a formula.
PS: Actually, my idea is inspired by the method of filtering prime numbers.

The Code is as follows:
# Include <stdio. h>
# Include <algorithm>
# Include <math. h>
Using namespace std;

Int GetAns (int nN)
{
Int nAns = 0;
While (nN)
{
NAns + = nN/5;
NN/= 5;
}
Return nAns;
}

Int main ()
{
Int nT;

Scanf ("% d", & nT );
While (nT --)
{
Int nN;
Scanf ("% d", & nN );
Printf ("% d \ n", GetAns (nN ));
}

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.