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;
}