Problem description
Mr. Santa asks all the great programmers of the world to solve a trivial problem. He gives them an integer m and asks for the number of positive integers n, such that the factorial of n ends with exactly m zeroes. Are you among those great programmers who can solve this problem?
Input
The only line of input contains an integer m (1 ≤ m ≤ 100 000) — the required number of trailing zeroes in factorial.
Output
First print k — the number of values of n such that the factorial of n ends with mzeroes. Then print these k integers in increasing order.
Examples
Input
1
Output
5
5 6 7 8 9
Input
5
Output
0
Note
The factorial of n is equal to the product of all integers from 1 to n inclusive, that is n! = 1·2·3·...·n.
In the first sample, 5! = 120, 6! = 720, 7! = 5040, 8! = 40320 and 9! = 362880.
解题思路:题目的意思就是要输出自然数x!的尾数刚好有m个0的所有x。做法:暴力打表找规律,代码如下:
1 import java.math.BigInteger; 2 public class Main{ 3 public static void main(String[] args) { 4 for(int i=1;i<=100;++i){ 5 BigInteger a=new BigInteger("1"); 6 for(int j=1;j<=i;++j){ 7 BigInteger num = new BigInteger(String.valueOf(j)); 8 a=a.multiply(num);// 调用自乘方法 9 } 10 System.out.println(i+" "+a); 11 } 12 } 13 }
通过打表可以发现:当m=1时,x∈[5,9];(共有5个元素)
当m=2时,x∈[10,14];(共有5个元素)
当m=3时,x∈[15,19];(共有5个元素)
当m=4时,x∈[20,24];(共有5个元素)
当m=5时,无x;
当m=6时,x∈[25,29];(共有5个元素)
......
因此,我们只需对5的倍数进行枚举,只要位数n<=m,则当n==m时,必有5个元素满足条件,否则输出"0"。
AC代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 int main(){ 4 int n=0,t=5,m,tmp;bool flag=false; 5 cin>>m; 6 while(n<=m){ 7 tmp=t; 8 while(tmp%5==0){n++;tmp/=5;}//n表示的个数,累加因子5的个数 9 if(n==m){ 10 puts("5"); 11 for(int i=0;i<5;i++) 12 cout<<t+i<<(i==4?" ":" "); 13 flag=true;break; 14 } 15 t+=5; 16 } 17 if(!flag)puts("0"); 18 return 0; 19 }