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)