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!后面0的个数。输出N最小值。
参考理解:
https://blog.csdn.net/ee8736199/article/details/48105017
注:计算N阶乘后面有几个0:
有算数基本定理可知 N!可划分为 质因数相乘的形式 N!=2^a*3^b*5^c*7^d........
因为只有2*5 才会出现 0 又因为2的数量肯定比5的多 所以计算阶乘中5的数量就可以得到该阶乘后有几个0。
50/5=10 10/5=2 所以50!后有10+2=12个0
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int res=0;
while(n)
{
res+=n/5;
n/=5;
}
printf("%d\n",res);
}
return 0;
}
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll slove(ll n)
{
ll num=0;
while(n)
{
num+=n/5;
n/=5;
}
return num;
}
ll er(ll n)//二分查找
{
ll x=1;
ll y=500000000;
ll mid;
ll res=-1;
while(y>=x)
{
mid=(x+y)/2;
ll ans=slove(mid);
if(ans==n)
{
res=mid;
y=mid-1;//找到 最小 的数,其阶层末尾的0为N个
}
else if(ans>n)
y=mid-1;
else if(ans<n)
x=mid+1;
}
return res;
}
int main()
{
int t;
scanf("%d",&t);
int x=1;
while(t--)
{
ll n;
scanf("%lld",&n);
ll ans=er(n);
if(ans==-1)
printf("Case %d: impossible\n",x++);
else
printf("Case %d: %lld\n",x++,ans);
}
return 0;
}