1. 程式人生 > >light oj 1138 - Trailing Zeroes (III)(階乘末尾0)

light oj 1138 - Trailing Zeroes (III)(階乘末尾0)

num stdio.h star adding 什麽 fin std sin each

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

水題一發,直接二分查找。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define
LL long long using namespace std; LL n; LL erfen() { LL ans=-1, l=5, r=400000020;//想想r的值為什麽這樣定(因為此時r!等於10^8+1) while(l<=r) { LL mid=(l+r)/2; LL sum=0, s=mid; while(s>0) sum+=s/5,s/=5; if(sum==n) { r=mid-1; ans=mid; }
else if(sum>n) r=mid-1; else l=mid+1; } return ans; } int main() { int T, t=1; scanf("%d", &T); while(T--) { scanf("%lld", &n); LL s=erfen(); if(s!=-1) printf("Case %d: %lld\n", t++, s); else printf("Case %d: impossible\n", t++); } }

light oj 1138 - Trailing Zeroes (III)(階乘末尾0)