阶乘因式分解问题

问题: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’.

样例输入




5

样例输出

Case 1: 5 
Case 2: 10 
Case 3: impossible



知道N!零的个数逆向求N,枚举绝对超时,用二分搜索,因为N!零的个数是单调的
#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值