hdu 1124 Factorial 数论,就是求一个数的阶乘的结果末尾有多少0.

Factorial

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2990    Accepted Submission(s): 1921

Problem 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

6
3
60
100
1024
23456
8735373

Sample Output

0
14
24
253
5861
2183837

12!=479001600,末尾有2个0.

20!=2432902008176640000 ,末尾有4个0.

题目的意思就是这个意思。

解题思路:

末尾的0是什么导致的?

显然是5。5这个数比较特殊,任何非5的数乘以5末尾多会多出一个0。,那我们只要算出1—n个数相乘中,共可以分解成多少个5就可以。

代码:

#include <stdio.h>

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n ;
		scanf("%d",&n) ;
		int ans = 0 ;
		while(n)
		{
			ans += n/5 ;
			n /= 5 ;
		}
		printf("%d\n",ans) ;
	}
	return 0 ;
}

与君共勉

时间: 2024-11-10 11:11:15

hdu 1124 Factorial 数论,就是求一个数的阶乘的结果末尾有多少0.的相关文章

10.输入一个数,求这个数的阶乘

(1)运用循环: #include<iostream>using namespace std;int JieCheng(int); int main(){    int n;    cout<<"please input an number: "<<endl;    cin>>n;    cout<<JieCheng(n);} int JieCheng(int n){    int m=1;    for(int i=1;i&

HDU 1124 Factorial(简单数论)

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4237    Accepted Submission(s): 2805 Problem Description The most important part of a GSM network is so called Base Transceiver Station

HDU 11124 Factorial (数论)

Factorial Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2807    Accepted Submission(s): 1804 Problem Description The most important part of a GSM network is so called Base Transceiver Station

HDU 1124 Factorial (数论)

http://acm.hdu.edu.cn/showproblem.php? pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長.我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是非常禽獸,嘻嘻~~~ 題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0. 解析: 一看就是簡單數論啦.跟數因子有關.最小素因子并且相乘能得到10的(就是後面有0的)就是2*5啦.因為一個數的階乘2的因子明顯比5的因子要

HDU 1124 Factorial (數論)

http://acm.hdu.edu.cn/showproblem.php?pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長,我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙下~~~哈哈,所以我就猥瑣的叫 旁邊的小師妹幫我翻譯了,我是不是很禽獸,嘻嘻~~~ 題目大意呢,就是給一個數,要你求出它的階乘的得到的結果後面有幾個0: 解析: 一看就是簡單數論啦,跟數因子有關,最小素因子而且相乘能得到10的(就是後面有0的)就是2*5啦,因為一個數的階乘2的因子明顯比5的因子要多得

HDU 1124 Factorial (阶乘后缀0)

题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将十进制质因数分解得2*5=10.将n!质因数分解,那么分解后,其中应含有min(2个数,5个数)个后缀0. 为何这么说?例如n=15,那么{1 2 3 4 5 6 7 8 9  10 11 12 13 14 15},那么可以产生2的数字有{2,4,6,8,10,12,14},可以产生5的只有{5,10,15},质数中只有2乘以5才能形成10,因为素数只有2是偶数!!!那么min(2个数,5个数)就决定了可以产生10的个数,也就决定了

求一个数的阶乘

用while循环进行求解,代码如下: int ContinuousMultiplication(int num) { int tmp = 1; while (num) { tmp= tmp*num; num--; } return tmp; } int main() { int i = 0,allnum=0; for (i = 1; i <= 20; i++) { allnum +=ContinuousMultiplication(i); } cout << allnum <<

[LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 这道题并没有什么难度,是让求一个数的阶乘末尾0的个数,也就是要找乘数中10的个数,

求N!末尾的0的个数--找规律+递归

0\'s Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 计算整数n!(n的阶乘)末尾有多少个0. 输入 第一行输入一个数T代表测试数据个数(T<=20).接下来T行每行1个数代表n(0<=n< 2^31). 输出 对于每个测试数据输n!末尾有多少个0,每行输出一个结果. 示例输入 3 1 5 10 示例输出 0 1 2 提示 中国海洋大学第三届"朗讯杯"编程比赛高级组试题 声明(摘抄至某前辈)--