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 starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
For each case, print the case number and N. If no solution is found then print 'impossible'.
3
1
2
5
Case 1: 5
Case 2: 10
Case 3: impossible
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll find_num(ll n)
{
ll cnt =0;
while (n)
{
cnt+=n/5;
n/=5;
}
return cnt ;
}
ll q;
ll binary_search_()
{
// cout<<q<<endl;
ll l=1,r=1000000000000000000;
ll ans =0;
while (l<=r)
{
ll mid =(l+r)>>1;
// cout<<mid<<endl;
ans=find_num(mid);
// cout<<"ans :: "<<ans<<endl;
if (ans>=q)
r=mid-1;
else
l=mid+1;
// cout<<l<<"\t"<<r<<endl;
}
ans=find_num(l);
if(ans==q)
return l;
else
return -1;
}
int main ()
{ int t,cnt =1;
cin>>t;
ll ans;
while (t--)
{
scanf("%lld",&q);
ans =binary_search_();
if(ans!=-1)
printf("Case %d: %lld\n",cnt++,ans);
else
printf("Case %d: impossible\n",cnt++);
}
return 0;
}