大意:给定一个数的阶乘的零的个数,输出最小这个数(不是这个数的阶乘)
思路:二分枚举这个数的范围判断这个数结成后零的个数。
#include<map>
#include<queue>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
#define eps 1e-8
const double PI=acos(-1.0);
using namespace std;
LL so(LL x){
LL ans=0;
while(x){
ans+= (x / 5);
x/=5;
}
return ans;
}
int main(){
LL n,m,i,j,k,cla;
scanf("%lld",&cla);
for(int zu = 1;zu <= cla;++ zu ){
scanf("%lld",&n);
LL l = 1,r = inf,mid;
bool vis = false;
while(r >= l){
mid = (r + l) / 2 ;
if(so(mid) == n ){
vis=true;
}
if(so(mid) < n){
l = mid + 1;
}
else{
r = mid - 1;
}
}
printf("Case %d: ",zu);
if(vis)
printf("%lld\n",l);
else{
puts("impossible");
}
}
return 0;
}
本文介绍了一种通过二分查找算法解决特定数学问题的方法:已知一个数的阶乘尾部零的数量,如何找出能产生该数量尾零的最小正整数。文章详细解释了核心算法思想,并提供了完整的C++实现代码。
2008

被折叠的 条评论
为什么被折叠?



