Factorial
Time Limit: 1500MS | Memory Limit: 65536K | |
Total Submissions: 6744 | Accepted: 4225 |
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.
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
此题的解题思路为:每5个数增加一个0;每25个数多增加一个0;每125个数再多增加一个0;以此类推
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int num = Integer.valueOf(cin.nextLine()).intValue(); for(int i = 0; i < num; i++) { int raw = Integer.valueOf(cin.nextLine()).intValue(); int znum = getZNum(raw); System.out.println(znum); } } private static int getZNum(int input) { int output = 0; int power = 1; while(input >= Math.pow(5, power)) { output += input / (int)(Math.pow(5, power)); power++; } return output; } }
发表评论
-
POJ ACM习题【No.2328】
2009-07-05 19:43 892Guessing Game Time ... -
POJ ACM习题【No.3157】
2009-04-26 23:54 1626Java vs C++ Time Lim ... -
POJ ACM习题【No.2924】
2009-04-26 11:52 915Gauß in Elementary School ... -
POJ ACM习题【No.3176】
2009-04-26 10:53 1038Cow Bowling Time Lim ... -
POJ ACM习题【No.3173】
2009-04-25 23:30 893Parkside's Triangle ... -
POJ ACM习题【No.2845】
2009-04-25 22:25 120501000001 Time Limit: ... -
POJ ACM习题【No.2140】
2009-04-25 21:26 929Herd Sums Time Limit ... -
POJ ACM习题【No.1969】
2009-04-25 20:59 851Count on Canton Time ... -
POJ ACM习题【No.2840】
2009-04-25 19:24 996Big Clock Time Limit ... -
POJ ACM习题【No.2521】
2009-04-24 22:41 839How much did the businessman l ... -
POJ ACM习题【No.1326】
2009-04-24 22:14 1001Mileage Bank Time Li ... -
POJ ACM习题【No.3325】
2009-04-24 21:15 1082ICPC Score Totalizer Software ... -
POJ ACM习题【No.2756】
2009-04-24 20:28 765Autumn is a Genius T ... -
POJ ACM习题【No.3062】
2009-04-24 20:10 801Celebrity jeopardy ... -
POJ ACM习题【No.1547】
2009-04-23 20:03 765Clay Bully Time Limi ... -
POJ ACM习题【No.1552】
2009-04-23 19:39 743Doubles Time Limit: ... -
POJ ACM习题【No.1007】
2009-04-23 19:24 843DNA Sorting Time Lim ... -
POJ ACM习题【No.1565】
2009-04-22 22:40 833Skew Binary Time Lim ... -
POJ ACM习题【No.2403】
2009-04-22 22:18 852Hay Points Time Limi ... -
POJ ACM习题【No.1862】
2009-04-22 20:12 692Stripies Time Limit: ...
相关推荐
方便大家有针对性地联系 祝大家AC愉快~
pojACM题目分类,便于各类型同学分别做题有所参考
相信大家在做poj上的题目的时候如果没有分类的话很迷茫吧....这里有一份目前比较全面的poj题目分类..
02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ题库使用指南.docx02.北大POJ...
acm数据结构总结.doc acm数据结构总结.doc
poj acm题解,包括绝大部分poj题目的题解,可以供acm爱好者学习研究
本文件是ACM里的一些题目的源码、原题和习题的分析及详细解答。欢迎各位下载
http://acm.pku.edu.cn/JudgeOnline/ acm的AC解题报告
POJ ACM 1015 Jury Compromise 两种解法 解题报告
西工大C语言POJ习题答案.docx
PKU 、POJ ACM/ICPC300多题的代码,还有各种典型问题的分类代码
poj 2007 Scrambled Polygon.md
poj 1984 Navigation Nightmare.md
poj 2900 Griddy Hobby.md
poj 2430 Lazy Cows.md
poj 3435 Sudoku Checker.md
poj 3183 Stump Removal.md
poj 2386 Lake Counting.md
poj 2201 Cartesian Tree.md
poj 3376 Finding Palindromes.md