POJ 1401---ask N! The number of the end 0, 2 of the number must be more than 5, observed, 0 of the production is 2*5, to find this factorial line 5 of the number can be

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h>int main () {    int t,n;    while (scanf ("%d", &t)!=eof)    {        int i;        for (i=0;i<t;i++)        {            int sum=0;            scanf ("%d", &n);            while (N)            {                n/=5;                sum+=n;            }            printf ("%d\n", sum);        }    }    return 0;}

If there are 4 numbers, 5*1 5*5*5 5*5*3 5*9

1. Become 5*n1 5*n2 5*n3 5*n4 There are 4 numbers is a multiple of 5

2. Become 5*n1 5*5*n5 5*5*n6 5*n4 There are 2 numbers is a multiple of 25

3. Become 5*n1 5*5*5 5*5*n6 5*n4 There are 1 numbers is a multiple of 125

The above is to avoid repetition, only 1 layers at a time (one-off)

The number of 5 per addition is actually the number on the right

Should be the theorem of number theory: for factorial n! , the number of 1 to N is a multiple of 5 for a given N,N/5

Also for n/25,n/125

The aim is to n/5+n/25+n/125 ...

If there is a 0, the sum is

Right

 while (N) {       sum+=n/5;       N/=5;}

Interpretation: Multiple additions, and each time to see a number divided by 5,sum overlay

N/5+n/25+n/125 ... can be seen as N/5+N1/5+N2/5

N1=n/5,n2=n1/5

Loop body: Each value is a number divided by 5, and the number of each number compared to the previous count except 5

while (N) {       sum+=n/5;       n/=5; }

For a logical relationship:

A number if it is a multiple of 25, then he must be a multiple of 5, on the contrary, it is not right, so this is the pillar he can layer

POJ 1401---ask N! The number of the end 0, 2 of the number must be more than 5, observed, 0 of the production is 2*5, to find this factorial line 5 of the number can be

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.