Reprinted please indicate the source: http://blog.csdn.net/lttreeFactorial
| Time limit:1500 Ms |
|
Memory limit:65536 K |
| Total submissions:13993 |
|
Accepted:8678 |
Description
The most important part of a GSM network is so called base transceiver station (BTS ). these transceivers form the areas called cells (this term gave the name to the cellular phone) and every phone connects to the BTS with the strongest signal (in a little simplified view ). of course, btses need some attention and technicians need to check their function periodically.
ACM technicians faced a very interesting problem recently. given a set of btses to visit, they needed to find the shortest path to visit all of the given points and return back to the central company building. programmers have spent several months studying this problem but with no results. they were unable to find the solution fast enough. after a long time, one of the programmers found this proble M in a conference Article. unfortunately, he found that the problem is so called "travelling Salesman Problem" and it is very hard to solve. if we have n btses to be visited, we can visit them in any order, giving us n! Possibilities to examine. The function expressing that number is called factorial and can be computed as a product 1.2.3.4... n. The number is very high even for a relatively small N.
The programmers understood they had no chance to solve the problem. but because they have already stored ed the research grant from the government, they needed to continue with their studies and produce at least some results. so they started to study behaviour of the factorial function.
For example, they defined the Function Z. For any positive integer N, Z (n) is the number of zeros at the end of the decimal form of number N !. They noticed that this function never decreases. if we have two numbers N1 <N2, then z (N1) <= z (N2 ). it is because we can never "lose" any trailing zero by multiplying by any positive number. we can only get new and new Zeros. the Function Z is very interesting, so we need a computer program that can determine its value efficiently.
Input
There is a single positive integer T on the first line of input. it stands for the number of numbers to follow. then there is t lines, each containing exactly one positive integer number N, 1 <= n <= 1000000000.
Output
For every number N, output a single line containing the single non-negative integer Z (n ).
Sample Input
63601001024234568735373
Sample output
0142425358612183837
Question: http://poj.org/problem? Id = 1401
The question is n! The number of zeros at the end.
This question was made in the first example of a typical example. However, I found that my understanding was wrong. (Thanks to a netizen For reminding me !)
So I did it again and found this question on poj, and I was able to explain whether it was correct.
Obviously, it is impossible to calculate the number of factorial and then 0.
Therefore, we need to change our thinking.
Why does it generate 0? It is derived from 2X5, so we can infer how many (2, 5) factor pairs there are through the number of 0.
We can also find that the number of five is always greater than the number of two.
And can simplify the number of 5.
At that time, I was still very young, and the practice made me look a bit tangled ..
At that time, we did a loop from 1 to n (the number entered) to see if it could be divisible by 5. If it could be divisible, we would continue to divide 5 until it could not be divisible.
The answer to this practice is obviously correct, but it is required by TLE in this question...
# Include <stdio. h> int main () {int I, n, k, T; double num; bool prime; scanf ("% d", & T); While (t --) {scanf ("% d", & N); k = 0; for (I = 1; I <= N; I ++) // check the number of 5 {num = I/5.0; If (Num-int (Num) = 0) prime = true; else Prime = false; while (Num> = 1 & prime = true) {num/= 5.0; If (Num-int (Num) = 0) prime = true; else Prime = false; K + = 1 ;}} printf ("% d \ n", k) ;}return 0 ;}
Now, let's think about it. This question does not need to be so troublesome.
The number is a multiple of 5.
Take 698 as an example:
698/5 = 139.6 → integer → 139 (indicating that 139 of the numbers are multiples of 5) sum = sum + 139 (sum is initialized to 0)
139/5 = 27.8 → integer → 27 (indicating that there are 27 multiples of 25) sum = sum + 27
(Why should I add 27*2 in multiples of 25 only once? 25 indicates two five?
Because there was a 5 before 25, it was counted once in the first 139, so you do not need to add it twice .)
27/5 = 5.4 → integer → 5 (indicating that there are 5 multiples of 125) sum = sum + 5
5/5 = 1 → integer → 1 (indicating that there is a number multiple of 625) sum = sum + 1
1/5 = 0.
So the answer is 139 + 27 + 5 + 1 = 172
Well, the program is very easy:
/*************************************** ************************************ Author: tree ** from: http://blog.csdn.net/lttree ** title: factorial ** Source: poj 1401 ** hint: n! Calculate the number of zeros ************************************ **************************************** ** // scanf 125 Ms, cin422 MS # include <stdio. h> int main () {// sum is the answer, storing the number (accumulative) int t, n, sum; scanf ("% d ", & T); While (t --) {sum = 0; scanf ("% d", & N); While (n) {n/= 5; sum + = N;} printf ("% d \ n", sum) ;}return 0 ;}
ACM-factorial -- poj1401