题意:
N!后面有Q个0,给你Q,求最小的N
Input
starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
For each case, print the case number and N. If no solution is found then print 'impossible'.
3
1
2
5
Case 1: 5
Case 2: 10
Case 3: impossible
求5的个数的方法:例如求25!,25/5=5,5/5=1,故有5+1=6个5。但是本题给0个数求n,故要在茫茫中寻找,果断二分。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 15006
#define LL long long
using namespace std;
int f(int n)//求n的阶乘末尾0的个数
{
int ans=0;
while(n)
{
ans+=n/5;
n=n/5;
}
return ans;
}
int main()
{
int t,n,cat=0,mid;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int l=0,r=1e9;
while(l<=r)
{
mid=(l+r)>>1;
if(f(mid)>=n)
r=mid-1;
else
l=mid+1;
}
if(f(l)==n)
printf("Case %d: %d\n",++cat,l);
else
printf("Case %d: impossible\n",++cat);
}
return 0;
}
如果求最大的n,则二分要这样写:
while(l<=r) //输出最大值
{
mid=(l+r)>>1;
if(f(mid)>n)
r=mid-1;
else if(f(mid)<=n)
l=mid+1;
}
if(f(r)==n)
printf("Case %d: %d\n",++cat,r);
else
printf("Case %d: impossible\n",++cat);