Time Limit: 2 second(s) | Memory Limit: 32 MB |
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.
Input
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.
Output
For each case, print the case number and N. If no solution is found then print 'impossible'.
Sample Input | Output for Sample Input |
3 1 2 5 | Case 1: 5 Case 2: 10 Case 3: impossible |
题解:和之前做的一道题一样就是看5有多少个因为0的形成是由10或者是2*5产生的呀!
开始很纠结不明白为什么5个0是不可能的,以为25!就有5个0了,后来才想到25!会形成6个0,因为25=5*5呀!!!不要忽视啊!!!
打表会超内存的,所以这里二分,简单二分,看看就懂啦!
最后要记得再检查一下,从样例就可以发现第三组输出了25,在检查一次就ok啦!
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(a,b) memset(a,b,sizeof(a))
#define LL long long
LL n;
LL check(LL x)
{
LL sum=0;
while(x)
{
sum+=x/5;
x/=5;
}
return sum; //求 x!会有多 0
}
int main()
{
LL u,ca=1;
scanf("%d",&u);
while(u--)
{
scanf("%lld",&n);
LL L=1,R=10000000000;
while(R>=L)
{
LL mid=(L+R)>>1;
if(check(mid)>=n)
R=mid-1;
else
L=mid+1;
}
printf("Case %d: ",ca++);
LL ant=0;
R=L;
while(L)
{
ant+=L/5;
L/=5;
}
if(ant==n)
printf("%lld\n",R);
else
printf("impossible\n");
}
return 0;
}