Nefu 118 n! How many 0 at the back (n! Power factor problem)

Source: Internet
Author: User

How many 0 in the back of n!?
problem:118 Time limit:1000ms Memory limit:65536k
Description
Read a number n from the input to find out the n! The number of the end 0.
Input
The input has several lines. The first line has an integer m, indicating the number of digits that follow. Then the M-line, each row contains a positive integer n,1<=n<=1000000000 that is determined.
Output
For each data in the input row, n, the output line, whose content is n! The number of the end 0.
Sample_input
331001024
Sample_output
024253




Ideas:

The power of p in prime factor decomposition of n! is: [n/p]+[n/p^2]+[n/p^3]+ ...

Proof in http://www.cnblogs.com/openorz/archive/2011/11/14/2248992.html detail

From the formula can be easily seen n! The power of the factor p decreases with the increase of P


For this problem, you can know that at the end of the 0 well-known factor 2*5 generation, and the power of 2 must be greater than 5, so the number of 0 is equal to (2,5) the number of matches, that is, 5 of the number of vegetarian factors

The conclusion formula can be solved by O (logn).

Accepted 820k 2ms C + + (g++ 3.4.3) 331  #include <cstdio> #include <iostream> #include <cstring># Include<algorithm>using namespace Std;int n;int main () {    int T;    scanf ("%d", &t);    while (t--)    {        scanf ("%d", &n);        int ans=0;        int p=5;        while (n/p) ans+=n/p,p*=5;        printf ("%d\n", ans);    }    return 0;}


Nefu 118 n! How many 0 at the back (n! Power factor problem)

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.