LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)

来源:互联网 发布:知乎关注最多的问题 编辑:程序博客网 时间:2024/06/04 22:23

大意:给定一个数的阶乘的零的个数,输出最小这个数(不是这个数的阶乘)

思路:二分枚举这个数的范围判断这个数结成后零的个数。

#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-8const 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;}
0 0
原创粉丝点击