Description
求最小的整数n使得n!的值后面有q个0
Input
一个整数q(0<=q<=10^8)
Output
满足条件的最小的正整数n,如果不存在则输出No solution
Sample Input
2
Sample Output
10
Solution
n!后面0的个数等于n!的质因子分解形式中5的幂指数,即为[n/5]+[[n/5]/5]+…,所以n最多为5*10^8,二分n即可
Code
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 11111
#define INF 0x3f3f3f3f
int count(int n)
{
int ans=0;
while(n)
{
ans+=n/5;
n/=5;
}
return ans;
}
void solve(int n)
{
int l=0,r=INF;
while(l<=r)
{
int mid=(l+r)>>1;
int temp=count(mid);
if(temp>=n)r=mid-1;
else if(temp<n)l=mid+1;
}
if(count(l)==n)printf("%d\n",l);
else printf("No solution\n");
}
int main()
{
int n;
scanf("%d",&n);
if(n==0)printf("1\n");
else solve(n);
return 0;
}