[Codechef] factorial (n! Number of 0 at the end)

Source: Internet
Author: User

The most important part of a GSM network is so calledBase transceiver station(BTS). These transceivers form the areas calledCells(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.

The 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 "Traveling 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 provided ed the research grant from the government, they needed to continue with their studies and produce at leastSomeResults. So they started to study behavior 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 (equal to about 100000 ). it stands for the number of numbers to follow. then there are 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 ).

Question: The question is very long. In fact, it is a sentence: calculate n! The number of zeros at the end.

The main idea is n! If there are 5, there will be 0 at the end.

First, by decomposing N into the formula, we know that the above adequacy is true, because 0 at the end of all can be regarded as factor 10, and 10 can be divided into 2*5, therefore, a 0 at the end must correspond to this 5. Each 0 must also come from a factor of 5. So there is a problem, that is, n! Is there enough 2 to change all 5 to 10? The answer is yes: For any n> = 5, there are n = (5*10*15 *...... * 5 (k-1) * 5 k) * a, where A is an integer that cannot be divisible by 5. So for the above sequence 5, 10, 15 ,......, 5 (k-1), 5 k in each 5, in the range (5 (I-1), 5I] must exist in an even number, 2 In this even number can change this 5 to 0 at the end.

So for n! Any Factor 5 in corresponds to a end 0, so we only need to find n! The number of factors 5 indicates the number of zeros at the end.

Assume n! F (N !) Five, then f (N !) = (N! /5) + f (n! /5); so we can use recursive methods to solve n! The number is 5.

The code for this question is as follows:

 1 import java.util.Scanner; 2  3 public class Main { 4     private static int end_zeros(int num) { 5         if(num <= 4) 6             return 0; 7         else 8             return num/5 + end_zeros(num/5); 9     }10     public static void main(String[] args) {11         // TODO Auto-generated method stub12         Scanner scanner = new Scanner(System.in);13         int t = scanner.nextInt();14         while(t-- >0 )15         {16             int num = scanner.nextInt();17             System.out.println(end_zeros(num));18         }19     }20 21 }

 

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.