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

Trailing Zeroes (III) -;lightoj 1138

2015-08-21 09:12 330 查看
Trailing Zeroes (III)

PDF (English)StatisticsForum
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

PROBLEM SETTER: JANE ALAM JAN
题意:如果某个数的阶乘有n个0,问这个数最小可以是多少。

题目描述:

  假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少?

解题思路:

  由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n=2^x=5^y,可知x<<y。

所以我们可以直接根据因子5的个数,算阶乘末尾的零的个数。1<=Q<=10^8,所以可以用二分快速求解。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

long long slove(long long n)
{
long long ans = 0;
while(n)
{
ans += n/5;
n /= 5;
}
return ans;
}

int main()
{
int t, l = 1;

scanf("%d", &t);
while(t--)
{
long long n;
scanf("%lld", &n);
long long mid, low = 4, high = 500000000;

while(low <= high)    // 二分查找快~
{
mid = (low+high)/2;
long long num = slove(mid);
if(num >= n)
high = mid-1;
else
low = mid+1;
}
if(slove(low) == n)
printf("Case %d: %lld\n", l++, low);
else
printf("Case %d: impossible\n", l++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: