您的位置:首页 > 大数据 > 人工智能

【lightoj 1138】Trailing Zeroes (III) (二分)

2017-08-22 16:58 561 查看


Trailing Zeroes (III)

 

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
3

1

2

5

Sample Output
Case 1: 5

Case 2: 10

Case 3: impossible

还不太懂,先存一下


求一个数n的阶乘中所存在的0的个数:
当0<n<5时,f(n!)=0;
当n>=5时,f(n!)=k+f(k!),其中k=n/5;
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define Max 0x7fffffff
using namespace std;
int Cal(int x)      //计算x后面有几个0
{
int ans=0;
while(x)
{
ans+=x/5;
x/=5;
}
return ans;
}
int main()
{
int t;
int cs=1;
scanf("%d",&t);
while(t--)
{
int q;
scanf("%d",&q);
int left=0,right=Max;
int mid;
while(left<=right)
{
mid=(left+right)>>1;
if(Cal(mid)>=q)
right=mid-1;
else
left=mid+1;
}
if(Cal(left)==q) //如果最后一次逼近时满足了条件,则此时left==mid(无论left与right相差1还是相等)
printf("Case %d: %d\n",cs++,left);
else
printf("Case %d: impossible\n",cs++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: