Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,z,ans,i;
	int mid,l,r,x;
	scanf("%d",&t);
{
		for(i=1;i<=t;i++)
	{
		cin>>z;
		l=1; r=5e8; mid=(l+r)/2;
		while(l!=mid)
		{
			ans=0;
			x=mid;
			while(x)
			{
				ans+=x/5;
				x/=5;
			}
			if(ans>=z)
			r=mid;
			else 
			l=mid;
			mid=(l+r)/2;
		}
		ans=0;
		x=mid+1;
		while(x)
		{
			ans+=x/5;
			x/=5;
		}
		printf("Case %d: ",i);
		if(ans==z)
		printf("%d\n",mid+1);
		else printf("impossible\n");
	}
	
}
	
	
	
	
	
	
	
	return 0;
}

f(n)=n/5+f(n/5)  阶乘后0的个数 n>5

猜你喜欢

转载自blog.csdn.net/cgmm2333/article/details/81265513