Trailing Zeroes (III) -;lightoj 1138

Trailing Zeroes (III)
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,所以可以用二分快速求解。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 long long slove(long long n)
 8 {
 9     long long ans = 0;
10     while(n)
11     {
12         ans += n/5;
13         n /= 5;
14     }
15     return ans;
16 }
17 
18 int main()
19 {
20     int t, l = 1;
21 
22     scanf("%d", &t);
23     while(t--)
24     {
25         long long n;
26         scanf("%lld", &n);
27         long long mid, low = 4, high = 500000000;
28 
29         while(low <= high)    // 二分查找快~
30         {
31             mid = (low+high)/2;
32             long long num = slove(mid);
33             if(num >= n)
34                 high = mid-1;
35             else
36                 low = mid+1;
37         }
38         if(slove(low) == n)
39             printf("Case %d: %lld
", l++, low);
40         else
41             printf("Case %d: impossible
", l++);
42     }
43     return 0;
44 }
让未来到来 让过去过去
原文地址:https://www.cnblogs.com/Tinamei/p/4746828.html