Light OJ 1138 - Trailing Zeroes (III)
2014-08-12 12:25
363 查看
采用二分法。
n!中因子2的个数比5多,所以因子5的个数等于末尾零的个数,只需要算出因子5的个数即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1000000000;
ll fac(int n,int x){
ll ans = 0;
while(n){
ans += n/x;
n /= x;
}
return ans;
}
int main(){
int t,q;
scanf("%d",&t);
for(int cas = 1;cas <= t;cas++){
scanf("%d",&q);
int l = 4,r = maxn;
while(l <= r){
int mid = (l+r)>>1;
if(fac(mid, 5)>=q)
r = mid - 1;
else
l = mid + 1;
}
if(fac(l, 5) != q)
printf("Case %d: impossible\n",cas);
else
printf("Case %d: %d\n",cas,l);
}
return 0;
}
n!中因子2的个数比5多,所以因子5的个数等于末尾零的个数,只需要算出因子5的个数即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1000000000;
ll fac(int n,int x){
ll ans = 0;
while(n){
ans += n/x;
n /= x;
}
return ans;
}
int main(){
int t,q;
scanf("%d",&t);
for(int cas = 1;cas <= t;cas++){
scanf("%d",&q);
int l = 4,r = maxn;
while(l <= r){
int mid = (l+r)>>1;
if(fac(mid, 5)>=q)
r = mid - 1;
else
l = mid + 1;
}
if(fac(l, 5) != q)
printf("Case %d: impossible\n",cas);
else
printf("Case %d: %d\n",cas,l);
}
return 0;
}
相关文章推荐
- light oj 1138 - Trailing Zeroes (III)《《二分》》
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
- Light oj1138 - Trailing Zeroes (III)
- Light OJ 1138:Trailing Zeroes (III)【二分+求阶乘中某质因子的幂】
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
- Light OJ 1138 Trailing Zeroes (III)(n!中素数p的幂问题)
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
- Light oj 1138 - Trailing Zeroes (III)
- Light oj 1138 - Trailing Zeroes (III)
- LIGHT OJ 1138 - Trailing Zeroes (III)【N!后0的个数&&二分(好题)】
- 【Light OJ 1138 】Trailing Zeroes (III) 【二分+数学】
- light oj 1138 - Trailing Zeroes (III)【规律&&二分】
- Light oj 1138 - Trailing Zeroes (III) (二分)
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
- 【Light oj 1138 】- Trailing Zeroes (III)(二分,思维)
- Light oj 1138 - Trailing Zeroes (III)
- Light oj 1138 -Trailing Zeroes (III)
- Light OJ:1138 Trailing Zeroes (III)(二分)
- LightOJ 1138 - Trailing Zeroes (III) 二分
- LightOJ - 1138 - Trailing Zeroes (III) (二分)