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

题目链接:http://lightoj.com/volume_showproblem.php?problem=1138

题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0;如果没有输出impossible

可以用二分求结果,重点是求一个数的阶乘中末尾含有0的个数,一定和因子5和2的个数有关,因子为2的明显比5多,所以我们只需要求一个数的阶乘的因子中一共有多少个5即可;

LL Find(LL x)
{
LL ans = ;
while(x)
{
ans += x/;
x /= ;
}
return ans;
}

例如125! 125/5 = 25;所以125!中含5的项为25*5+24*5+23*5+...+3*5+2*5+1*5 = 25!*5 

所以125!末尾有25+5+1 = 31 个0;

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef long long LL;
#define N 1000001
using namespace std;
const double eps = 1e-; LL Find(LL x)
{
LL ans = ;
while(x)
{
ans += x/;
x /= ;
}
return ans;
} int main()
{
int T, t = , n;
scanf("%d", &T);
while(T --)
{
scanf("%d", &n);
LL ans = , L = , R = ;
while(L <= R)
{
LL Mid = (L+R)/;
LL ret = Find(Mid); if(ret == n)
ans = Mid;
if(ret >= n)
R = Mid - ;
else
L = Mid + ;
}
if(ans == )
printf("Case %d: impossible\n", t++);
else
printf("Case %d: %lld\n", t++, ans);
}
return ;
}
上一篇:N的阶乘末尾0的个数和其二进制表示中最后位1的位置


下一篇:Apache Spark Tachyon的简介