poj 1401 Factorial

来源:互联网 发布:质量效应3防御矩阵 编辑:程序博客网 时间:2024/05/16 17:18

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 problem 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 received 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

 

这道题的大意是求n!中末尾0的个数~

开始时我的想法是每次循环时用所得s=i!除以10~若余数为0~则count++~s=s/10~

后来发现不对~求出来的数据时错的~

看完discuss之后~我明白数字阶乘中末尾0的个数就是将该数字数字分解后10的个数~也就是全分解为质数后2和5的对数~

假如你把1×2×3×4×……×N中每一个因数分解质因数,结果就像: 1 × 2 × 3 × (2 × 2) × 5 × (2 × 3) × 7 × (2 × 2 ×2) ×……

10进制数结尾的每一个0都表示有一个因数10存在——任何进制都一样,对于一个M进制的数,让结尾多一个0就等价于乘以M。

 10可以分解为2 × 5——因此只有质数2和5相乘能产生0,别的任何两个质数相乘都不能产生0,而且2,5相乘只产生一个0。 所以,分解后的整个因数式中有多少对(2, 5),结果中就有多少个0,而分解的结果中,2的个数显然是多于5的,因此,有多少个5,就有多少个(2, 5)对~

因此~此题就成了求1到N因式分解后中包含多少个5的问题既求N中包含多少个5及5^k的个数的问题~

 代码如下~

#include"stdio.h"int main(){ int i; int count; int n; int cases;// freopen("01in.txt","r",stdin);// freopen("01out.txt","w",stdout); scanf("%d",&cases); while(cases--) {  scanf("%d",&n);  for(count=0,i=5;i<=n;i*=5)   count+=n/i;  printf("%d\n",count); } return 0;}


 

原创粉丝点击