题目链接:http://lightoj.com/volume_showproblem.php?problem=1138
题意:问 N! 末尾 0 的个数为 Q 个的数是什么?
解法:二分枚举N,因为0是由5×2 出现的,2的个数比5多故计算5的个数即可。
代码:
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
long long count_num(long long n)
{
long long num = 0;
while (n)
{
num += n / 5;
n /= 5;
}
return num;
}
int main()
{
int t;
scanf("%d",&t);
int cases = 1;
while (t--)
{
int n;
scanf("%d",&n);
printf("Case %d: ",cases++);
long long left = 1;
long long right = 1000000000000;
long long mid;
while (left <= right)
{
mid = (right + left) / 2;
long long tmp = count_num(mid);
if (tmp >= n)
right = mid - 1;
else
left = mid + 1;
}
if (count_num(left) != n)
puts("impossible");
else
printf("%lld\n",left);
}
return 0;
}