1. 程式人生 > >Factorial(階乘最後0的個數)

Factorial(階乘最後0的個數)

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

6
3
60
100
1024
23456
8735373

Sample Output

0
14
24
253
5861
2183837

Source


這道題挺經典的,這個演算法理解起來挺簡單的。

先說一下原理:後面的0都是由2和5相乘得到的,而2每兩個就有一個,所以個數足夠,不考慮。

所以只需要考慮因數5的個數即可。

我覺得個人參照程式碼比較容易學會吧,如果給出n,小於等於n的5的倍數有 n/5 個,這些數都包括了因數5。

然後把這些數縮小為原來的 1/5 (記得高中語文老師說,小於1的倍數的說法不能說縮小几倍,瞎扯幾句,繼續說),如果剩下的數仍然大於5,則還是存在5的倍數,再次迴圈上面的過程即可。不懂的話可以看看程式碼。

程式碼如下:

#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;
}


相關推薦

Factorial最後0個數

Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submissions: 15475 Accepted: 9533 Description The most important part of

154 Factorial素因子次數的逆問題

題目: You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N!

的質因數分解算組合數】【TOJ4111】【Binomial efficient】

n<=10^6 m<=10^6 p=2^32 用unsigned int 可以避免取模 我寫的SB超時 階乘分解程式碼 #include <cstdio> #include <cstdlib> #include <cmath&

末尾0個數java

末尾0的個數 題目描述 輸入一個正整數,求n!末尾有多少個0?比如n = 10; n! = 3628800 ,所以答案為2 輸入描述 輸入為一行,n (1 <= n <= 1000)//輸入量範圍限制不能用先求階乘的方法,會產生溢位

poj1401--Factorial--末尾0個數

cnblogs sin factorial int amp div factor color sum Description   求出n!的末尾有多少個0(連續的)。   每組測試點有t個測試數據,輸入格式為第一行一個t,後面2~t+1行每行一個n,輸出其結果。 Sampl

1003 後面0的數量51nod 解題報告

基準時間限制:1 秒 空間限制:131072 KB 分值: 5 難度:1級演算法題  收藏  關注 n的階乘後面有多少個0? 6的階乘 = 1*2*3*4*5*6 = 720,720後面有1

javaSE (三十四File類和遞迴練習統計資料夾大小、拷貝資料夾、層級列印資料夾、斐波拉契數列、獲取1000全部0和尾部0數目、約瑟夫環

1、統計資料夾大小: 思路: 套用之前已經做過的,鍵入一個路徑,若有效則封裝成File類 初始化計數器len, 若資料夾下是檔案,則記錄檔案.length() 若資料夾下是資料夾,遞迴 輸出len 注:遞迴也可以刪除資料夾,但是一定要先刪除裡

Factorial Trailing Zeroes(OJ) 求其尾數0個數[1808548329]

問題描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 問題

【LeetCode】Factorial Trailing Zeroes 尾部0個數

題目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time c

《資料庫技巧》資料庫兩個表求笛卡爾積

最近遇到了一個需求:使用者在客戶端頁面上進行資料錄入,下拉列表的選擇,然後使用者對頁面資料進行提交。後臺要根據客戶端傳來的資料進行分析,並且生成一串數字,將該數字串進行儲存。 介紹之前,我們要了解本文的一個名詞【笛卡爾積】,同俗的來講,就是數學中的排列組合。

Java中的組織形式、類與物件、靜態的static關鍵字、最終的final關鍵字、方法傳參方式、遞迴、斐波那契數列、漢諾塔

Java程式的組織形式 Java程式需要把程式碼以類的形式組織起來,然後被Java編譯器編譯,再被JVM執行。Java程式是以類的結構為基礎的。 Java程式的基本要素 識別符號 識別符號命名規範 關鍵字(保留字) 關鍵字(保留字)具有專門的意義和用途

求一個數0個數

求一個數的階乘中0的個數 連續幾天產出為負,再不寫點東西沒法交代了。。。 public class The_number_of_0_in_FactorialN { public static void main(String[] args) { int n=5; Syst

末尾 0個數.c

// 階乘尾數零的個數 // 100!的末尾有多少個0? /* 問題分析和演算法設計: 首先分析在100!結果值的末尾產生0的條件。不難看出:一個整數 若含有一個因子5則必然會在求100!時產生一個0。因此問題轉化為 求1到100這100個整數中包含了多少個因子5。若整數N

for迴圈語句運算

#include<stdio.h> main() { int i,j,n; long term, sum=0; printf("Input n:"); while(scanf("%d",&n)!=EOF){

五十道程式設計小題目 --- 11 排列與組合 java

package wn.comeOn.java.test.arithmetic50; import java.util.ArrayList; public class Arrangement { // 階乘 public static int factorial(int n) { if (n

HDU 1261 字串數大數 除法 組合 java

生平第一次用java A題 這個題目明顯的一個全排列除以相同元素的排列數 import java.util.*; import java.math.*; public class Main { public static void main(St

java 求n的尾部0個數

題目描述 求n的階乘尾部0的個數 樣例輸入 11 樣例輸出 2 java程式碼 import java.util.Scanner; import java.util.Arrays; p

n後面0個數+組合數學結果0個數舉例+公式推導

先掛上一題,用作例子。 (n!%(10^k))==0. 已知n,求能使上式成立的k的最大值。 例如 5!= 120  有1個0,10!= 3628800 ,有2個0。 很明顯,階乘中所有數的因

計算N的0個數

首先我們要知道0是怎麼來的 兩個個位數相乘只有2*5=10會出現一個0 所以我們只要找出因子2和5能匹配多少對 因為在N的階乘中因子為2的個數遠遠大於因子為5的個數 比如5!=120 是因為2*5 = 10能匹配出一對來而4還可以分成2*2所以2的數目遠遠大於5 所以下一

GCC和求餘,很重要

GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 4162    Accepted Submissi