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
注意以下是超时的代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
int main(){
int t;
scanf("%d",&t);int ccase=0;
while(t--){
int n;
scanf("%d",&n);int ans=0;
int res=n*5;int tem=5; bool flag=1;int ress;
while(1){
ress=res;
ans=0;
while(res){
ans+=res/tem;
res/=tem;
}
// cout<<ans<<endl;
if(ans>n){
res=ress-5;
}
else if(ans==n){
break;
}
else{
flag=0;
break;
}
}
printf("Case %d: ",++ccase);
if(!flag) cout<<"impossible"<<endl;
else printf("%d\n",ress);
}
return 0;
}
首先偶数的个数显然大于5的个数,所以看5的个数就可以了。超时的话,也就是1e8还是挺大的(我准备丰富一下自己的博客,写的太丑会很丢人的,尽量写的清晰易懂,不过不知道什么样的该写博客,由于我自己的水平比较低,基本上都会写的
所以二分,二分还是需要多写写的,边界的问题总是出错
我的思维正常是解方程组的思想,但是计算机是可以一个个试的,所以二分我不是很习惯用。需要注意一下范围,保证1e8个0的话,二分的范围要大一些
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
const int maxn=5e8;//要适当的大点保证范围
int judge(int n){
int res=0;
while(n){
res+=n/5;
n/=5;
}
return res;
}
int erfen(int n){
int l=0; int r=maxn;
while(l<=r){
int mid=(l+r)>>1;
if(judge(mid)<n){
l=mid+1;
}
else r=mid-1;
}
if(judge(l)==n) return l;
return 0;
}
int main(){
int t;
scanf("%d",&t);int ccase=0;
while(t--){
int n;
scanf("%d",&n);int ans=0;
//if(erfen(n)==0)
printf("Case %d: ",++ccase);
if(erfen(n)==0) cout<<"impossible"<<endl;
else printf("%d\n",erfen(n));
}
return 0;
}