题意:
求最小的n的阶乘使得阶乘的末尾有q个0
思路:
阶乘末尾0的个数,即含有的5的倍数,求一次是log的复杂度,再二分一下
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <bitset>
#define max_ 200010
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
#define mod 364875103
using namespace std;
int casnum=1;
int n;
int cal(int x)
{
int ans=0;
while(x)
{
ans+=x/5;
x/=5;
}
return ans;
}
int find(int x)
{
int l=1;int r=500000000;
int ans=-1;
while(l<=r)
{
int mid=(l+r)>>1;
int d=cal(mid);
if(d<x)
l=mid+1;
else if(d==x)
{
ans=mid;
r=mid-1;
}
else
r=mid-1;
}
return ans;
}
int main(int argc, char const *argv[]) {
int t;
scanf("%d",&t);
while(t--)
{
int q;
scanf("%d",&q);
int ans=find(q);
if(ans==-1)
printf("Case %d: impossible\n",casnum++ );
else
printf("Case %d: %d\n",casnum++,ans );
}
return 0;
}