问题:N! 结果零的个数
分析:
求零的个数即10的因子个数,而10=5*2
即求5和2的对数
记N! 的因子x个数为ans(x)
显然在N! 中ans(2)>ans(5)
则ans(10)=min(ans(2),ans(5))=ans(5)
现在问题变为求ans(5)了
那么还是继续分析N!成分
N!数字 1 2 3 4 5 6 7 8 9 10 11 12 15 14 15 16 17 18 19 20 21 22 23 24 25 26 ……N
包含因子 5 10 15 20 25……N/(5^1) N/(5^1)个
提出个5 1 2 3 4 5……N/(5^2)
包含因子 5……N/(5^2) N/(5^2)个
……
最后
提出个5 1
包含因子 无 0个
由此可见ans(5)=(从i=0 到N/(5^i)=0 的N/(5^i)的和)
上题
描述
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! = 1*2*…*N. For example, 5! = 120, 120 contains one zero on the trail.
输入
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
输出
For each case, print the case number and N. If no solution is found then print ‘impossible’.
样例输入
3
1
2
5
样例输出
Case 1: 5
Case 2: 10
Case 3: impossible
#include<stdio.h>
int a[13];
int ans(int x)
{
int sum=0;
for(int i=0;i<13;i++)
sum+=x/a[i];
return sum;
}
int main()
{
int T,t,i,q,low,top;
for(a[0]=5,i=1;i<13;i++)
{
a[i]=5*a[i-1];
}
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d",&q);
low=5,top=1000000000;
while(top-low>1)
{
if(ans((low+top)/2)>=q)
top=(low+top)/2;
else
low=(low+top)/2;
}
if(ans(low)==q) printf("Case %d: %d\n",t,low);
else if(ans(top)==q) printf("Case %d: %d\n",t,top);
else printf("Case %d: impossible\n",t);
}
return 0;
}