NYOJ 84 factorial 0 Number Theory

Source: Internet
Author: User

0 of factorial
Time Limit: 3000 MS | memory limit: 65535 KB
Difficulty: 3
Description
Calculate n! In decimal format.
Input
Enter an integer N in the first line to indicate the number of groups of test data (1 <= N <= 100)
Each group of test data occupies one row, with only one integer M (0 <= M <= 10000000)
Output
Number of the last 0 in the decimal number of the factorial output M

For example, 5! = 120 then the number of the last 0 is 1


Sample Input
6
3
60
100
1024
23456
8735373 sample output
0
14
24
253
5861
2183837 solution: because in the prime number, only 2 and 5 are multiplied to produce a "0" at the end. Then, as long as m is decomposed into the prime factor, and then the number of 2 and 5 is counted, the smaller one is the answer.
Further, after m decomposition, the number of 2 is definitely more than 5, so the problem is further simplified, as long as the number of 5 of all prime factors is counted.

For example:


5! = 5*4*3*2*1 = 120, where 1 is 5 and 1 is 2, so the last one is 0.

10! = 10*9*8*7*6*5*4*3*2*1 = 3628800. The product can be written as (5*2) * (3*3) * (2*2*2) * 7 * (3*2) * 5 * (2*2) * 3*2*1, there are 2 5 and 8 2, and the number of Factor 5 is the number of the last 0, so the last two 0

Perform the following in a stupid way:


1-> the multiples of 5 in 3000 are:
5, 10, 15, 25 ,...... 95,100,105,110,115,125 ,...... 195,200 ,...... 2995,3000
Then the number of five is 3000 bytes 5 = 600 (count)

However, as long as the number is a multiple of 5 ^ 2 = 25, it can be divided into 2 5, for example:
25, 50, 75,100,125,150,175,200,225 ,...... 3000, these numbers are counted as two five, so the number of five must be added to the number of these numbers once,
The number of 25 in the above heap is 3000 bytes 25 = 120 (count)

However, as long as the number is a multiple of 5 ^ 3 = 125, it can be divided into 3 5, for example:
125,250,375 ,...... 3000, these numbers must be three five, so the number of five will need to be added one more time.
The number of 125 in the above heap is 3000 bytes 125 = 24 (count)

However, as long as the number is a multiple of 5 ^ 4 = 625, it can be divided into 4 5, for example:
, The four numbers are counted as four five, so the number of five is to be added one more time.
So the number of 125 in the above heap is 3000 bytes 625 = 4.8. Here, there are only four multiples of 625.

So the number of five is actually 600 + 120 + 24 + 4 = 748 (s)


[Cpp]
# Include <stdio. h>
Int main ()
{
Int t, sum, n;
Scanf ("% d", & t );
While (t --)
{
Sum = 0;
Scanf ("% d", & n );
While (n)
{
N/= 5;/* decomposition prime factor, count the number of 5 */
Sum + = n;
}
Printf ("% d \ n", sum );
}
Return 0;
}

# Include <stdio. h>
Int main ()
{
Int t, sum, n;
Scanf ("% d", & t );
While (t --)
{
Sum = 0;
Scanf ("% d", & n );
While (n)
{
N/= 5;/* decomposition prime factor, count the number of 5 */
Sum + = n;
}
Printf ("% d \ n", sum );
}
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.