思路:对于有几个0,10一定会是5的整数倍,2的因子数一定比5的多,所以只要算5的个数即可,
30%,每个n都去算
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll check(ll n) { //计算n!末尾有多少个0
ll cnt = 0;
while (n) cnt += (n /= 5);
return cnt;
}
int main(){
ll k; cin >> k;
for(ll n=5;;n+=5){ //n是5的倍数,它含有因子5
ll cnt = check(n); //cnt是n!的尾零数量
if(cnt==k){ cout << n; break;}
if(cnt>k) { cout <<-1; break;}
}
return 0;
}
100%,利用二分思想
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll check(ll n) { //计算n!末尾有多少个0
ll cnt = 0;
while (n) cnt += (n /= 5);
return cnt;
}
int main() {
ll k; cin >> k;
ll L = 0, R = 1e19; //R的初值为一个极大的数
while (L < R) {
ll mid = (L + R) / 2;
if (check(mid) >= k) R = mid; // mid!的尾零数量超过了k,说明mid大了
else L = mid + 1; // mid小了
}
if (check(R) == k) cout << R ;
else cout << -1;
return 0;
}
标签:二分,cnt,cout,ll,mid,long,蓝桥,阶乘,check
From: https://blog.csdn.net/zengxuan151168/article/details/137523175