地址点击打开链接
C - Trailing Zeroes (III)
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
题意:给你一个数q,求一个数n,使得n!末尾有q个0,如果不存在输出impossible。
解题思路:阶乘的式子中,每含一个5,结果就会在末尾多增加一个0,用二分扫一个大区间,最后确定一个数的阶乘末位为q个0,。而计算一个数的阶乘末尾0的个数,
#include<cstdio>
#include<algorithm>
using namespace std;
long long judge(long long x) //求5的个数 阶乘里有一个5 结尾一个0
{
long long sum=0;
while(x)
{
sum=sum+x/5;
x/=5;
}
return sum;
}
int main()
{
int t,i;
scanf("%d",&t);
i=1;
while(t--)
{
long long q,a,l=0,r=2000000000,mid;
scanf("%lld",&q);
a=0;
while(l<=r) //标准的二分法模板
{
mid=(l+r)>>1;
if(judge(mid)>=q)
{
a=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
if(judge(a)==q)
printf("Case %d: %lld\n",i,a);
else
printf("Case %d: impossible\n",i);
i++;
}
return 0;
}
/*long long n,i,s=1;
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
s=s*i;
}
printf("%lld\n",s);*/