您的位置:首页 > 其它

light oj 1138

2015-09-20 11:32 225 查看
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1138

Description

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,使其阶乘的末尾有Q个0.

思路:只有2和5相乘时末尾才会出现0,在阶乘过程中,因子2的个数足够多,所以每遇到一个5,末尾就会出现一个0,因此问题转化为求1到N这N个整数中包含了多少个因子5。


#include<stdio.h>
#include<string.h>
#define MAX 0x3f3f3f
#define LL long long
LL fun(LL x)
{
LL ans=0;
while(x)
{
ans+=x/5;
x=x/5;
}
return ans;
}
int main()
{
int n,k=1;
LL m;
scanf("%d",&n);
while(n--)
{
scanf("%lld",&m);
LL left=0;
LL right=400000010;
LL mid;
while(left<=right)
{
mid=(left+right)>>1;
if(fun(mid)>=m)
right=mid-1;
else
left=mid+1;
}
printf("Case %d: ",k++);
if(fun(left)!=m)
printf("impossible\n");
else
printf("%lld\n",left);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: