首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【POJ】1401 - Factorial(阶乘最后0的个数)

【POJ】1401 - Factorial(阶乘最后0的个数)

作者头像
FishWang
发布2025-08-26 19:45:57
发布2025-08-26 19:45:57
1600
举报

Factorial

Time Limit: 1500MS

Memory Limit: 65536K

Total Submissions: 15475

Accepted: 9533

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

代码语言:javascript
复制
6
3
60
100
1024
23456
8735373

Sample Output

代码语言:javascript
复制
0
14
24
253
5861
2183837

Source

Central Europe 2000

这道题挺经典的,这个算法理解起来挺简单的。

先说一下原理:后面的0都是由2和5相乘得到的,而2每两个就有一个,所以个数足够,不考虑。

所以只需要考虑因数5的个数即可。

我觉得个人参照代码比较容易学会吧,如果给出n,小于等于n的5的倍数有 n/5 个,这些数都包括了因数5。

然后把这些数缩小为原来的 1/5 (记得高中语文老师说,小于1的倍数的说法不能说缩小几倍,瞎扯几句,继续说),如果剩下的数仍然大于5,则还是存在5的倍数,再次循环上面的过程即可。不懂的话可以看看代码。

代码如下:

代码语言:javascript
复制
#include <cstdio>
int main()
{
	int u;
	int ans,n;
	scanf ("%d",&u);
	while (u--)
	{
		ans = 0;
		scanf ("%d",&n);
		while (n)
		{
			ans += n/5;
			n /= 5;
		}
		printf ("%d\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档