poj1401(计算n!中0的个数)

来源:互联网 发布:柴犬为什么会笑 知乎 编辑:程序博客网 时间:2024/06/07 14:14

http://poj.org/problem?id=1401

Factorial
Time Limit: 1500MSMemory Limit: 65536KTotal Submissions: 12332Accepted: 7682

Description

The most important part of a GSMnetwork is so called Base Transceiver Station (BTS). Thesetransceivers form the areas called cells (this term gave the nameto the cellular phone) and every phone connects to the BTS with thestrongest signal (in a little simplified view). Of course, BTSesneed some attention and technicians need to check their functionperiodically.

ACM technicians faced a very interesting problem recently. Given aset of BTSes to visit, they needed to find the shortest path tovisit all of the given points and return back to the centralcompany building. Programmers have spent several months studyingthis problem but with no results. They were unable to find thesolution fast enough. After a long time, one of the programmersfound this problem in a conference article. Unfortunately, he foundthat the problem is so called "Travelling Salesman Problem" and itis very hard to solve. If we have N BTSes to be visited, we canvisit them in any order, giving us N! possibilities to examine. Thefunction expressing that number is called factorial and can becomputed as a product 1.2.3.4....N. The number is very high evenfor 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 thegovernment, they needed to continue with their studies and produceat least some results. So they started to study behaviour of thefactorial function.

For example, they defined the function Z. For any positive integerN, Z(N) is the number of zeros at the end of the decimal form ofnumber N!. They noticed that this function never decreases. If wehave two numbers N1 < N2, then Z(N1) <= Z(N2). It is becausewe can never "lose" any trailing zero by multiplying by anypositive number. We can only get new and new zeros. The function Zis very interesting, so we need a computer program that candetermine its value efficiently.

Input

There is a single positive integer Ton the first line of input. It stands for the number of numbers tofollow. Then there is T lines, each containing exactly one positiveinteger number N, 1 <= N <= 1000000000.

Output

For every number N, output a singleline containing the single non-negative integer Z(N).

Sample Input

63601001024234568735373

Sample Output

0142425358612183837

Source

Central Europe 2000

题意:求一个阶乘数末尾连续的0的个数。

分析:首先要知道,末尾出现0的话,一定是某个偶数和5相乘得到的,所以整个数含有5(包含拆开,即25=5*5,出现两次)的5的个数就是所求末尾0的个数。

思路分析(转)

每次只计算最多含有5,5^2,5^3……的数字个数
每次含有5的[n/5]
........25.[n/25]

注意,当统计最多含有5^2的因子的时候,5的个数应该为[n/25]而不是[n/25]*2因为,在含有[n/5]的时候已经统计过一次了
所以,只需要把[n/5],[n/25]...加起来就可以了

例如1~100中
(1)含有5的:5,10,15....总计100/5=20个,每个之中含有1个5,总5因子数为20
(2)含有25的:25,50,75,100,总计100/25=4个,每个之中含有2个5,但是因为在(1)中含5的数字已经统计过一次因子,所以这里仍然记录因子个数1*5个
。。。。所以1~100中含有5的因子一共100/5+100/25=24个

1~N中的所有数字相乘,能拆分成的最简形式为素数的相乘(1忽略),其中2的个数多于5的个数(2的倍数多于5的倍数),所以,N!中,因子5有多少个,最后N!的末尾的0就有多少个。

 

这题在前面知识点的时候已经提到具体算法了。。。

#include
#include
using namespace std;
int solve(int n)
{
    intcount=0;
   while(n>=5)
    {
       n=n/5;
       count+=n;
    }
    returncount;
}
int main()
{
    int t;
   scanf("%d",&t);
   while(t--)
    {
       int n;
       scanf("%d",&n);
       //sum=sovle(n);
       printf("%d\n",solve(n));
    }
    return0;
}

原创粉丝点击