154. Factorial
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard input
output: standard output
output: standard output
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
One number Q written in the input (0<=Q<=10^8).
Output
Write "No solution", if there is no such number N, and N otherwise.
Sample test(s)
Input
2
Output
10
问是否存在整数n,使得n!的末尾恰有Q个0.有的话输出最小满足条件的n
n!尾数0的个数就是 [n/5] + [n/25] + [n/5^3]+.....
我们可以二分枚举n ,然后做判断,不断缩小范围。。
知道AC。
贴个代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>
#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)
using namespace std;
int const nMax = 1010;
int const base = 10;
typedef long long LL;
typedef pair<LL,LL> pij;
// std::ios::sync_with_stdio(false);
long long n;
LL f(LL a){
LL ans=0;
LL t=5;
while(a/t){
ans+=(a/t);
t*=5;
}
return ans;
}
int main(){
LL l,r;
l=0,r=450000000;
cin>>n;
LL ans=r;
while(l<=r){
LL mid=(l+r)>>1;
LL p=f(mid);
// printf("%lld %lld\n",mid,p);
if(p==n){
if(ans>mid)
ans=mid;
r=mid-1;
}else if(p>n){
r=mid-1;
}else {
l=mid+1;
}
}
if(ans==0)ans=1;
if(ans==450000000){
printf("No solution\n");
}else
printf("%I64d\n",ans);
return 0;
}