题目链接:https://cn.vjudge.net/problem/

题意

找一个最小的正整数n

使得n!有a个零

思路

就是有几个因数10呗

考虑到10==2*5,也就是说找n!因数5有几个

数据量略大(N<=1e8),打表之类的O(N)算法是直接不可以

分析到这里,可能的算法也就是二分了

找了找很久规律,发现可以有O(log5(n))的方法确定n!的因数5的个数

于是有二分

代码

// binary search
// [f(m)<n, f(m)=n, f(m)>n]
// [l, r, r] (find min r)
// [l, l, r] (find max l)
//
// Attention:
// 1. make sure the initial value of l and r.
// 2. whlie (l<r-1).
// 3. l <= mid < r.
// 4. check if r(l) is the answer. #include <cstdio>
long long find(long long n){
long long ans=0;
while (n){
ans+=n/5; n/=5;
}return ans;
} long long search(long long n){
long long l=1, r=5e8;
while (l<r-1){
long long mid=(r+l)/2, m=find(mid);
if (m>=n) r=mid;
else l=mid;
}
if (find(r)==n) return r;
else return -1;
} int main(void){
int T; long long n; scanf("%d", &T);
for (int cnt=1; cnt<=T; cnt++){
scanf("%lld", &n);
long long ans=search(n);
if (ans>0) printf("Case %d: %lld\n", cnt, ans);
else printf("Case %d: impossible\n", cnt);
} return 0;
}
Time Memory Length Lang Submitted
20ms 1088kB 896 C++ 2018-05-17 18:18:26

LightOJ-1138 Trailing Zeroes (III) 唯一分解定理 算n!的某个因数个数的更多相关文章

  1. LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)

    http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS     M ...

  2. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...

  3. lightoj 1138 - Trailing Zeroes (III)【二分】

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...

  4. LightOj 1138 Trailing Zeroes (III)

    题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...

  5. LightOJ 1138 Trailing Zeroes (III) 打表

    就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...

  6. light oj 1138 - Trailing Zeroes (III)【规律&&二分】

    1138 - Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit:  ...

  7. Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

    1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...

  8. Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

    1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  9. 1138 - Trailing Zeroes (III) 二分

    1138 - Trailing Zeroes (III)   You task is to find minimal natural number N, so that N! contains exa ...

随机推荐

  1. 优动漫结合Photoshop怎么画草地?

    今天继续技法教学~草地的技法,PS教学~但是很简单,都是默认工具,而且是常用工具VS常用设置.你肯定会学会的! 草地教程,就到这里啦!有兴趣的可以尝试画一画哦,想要Get到更多有关优动漫的信息包括软件 ...

  2. Pyhton学习——Day5

    # s=set('hello')# print(s)## s=set(['alex','alex','sb'])# print(s) # s={1,2,3,4,5,6} #添加# s.add('s') ...

  3. FaceBook SDK登录功能实现(Eclipse)

    由于公司游戏要进行海外推广,所以要我们接入FBSDK 实现登录,分享,投放,所以写这篇文章,也算是个工作总结.1.资料 (1).FB SDK github源码地址为 (2): [FB SDK中文接入文 ...

  4. VUE使用中踩过的坑

    前言 vue如今可谓是一匹黑马,github star数已居第一位!前端开发对于vue的使用已经越来越多,它的优点就不做介绍了,本篇是我对vue使用过程中以及对一些社区朋友提问我的问题中做的一些总结, ...

  5. Shiro:整合swagger2时需要放行的资源

    filterMap.put("/swagger-ui.html", "anon"); filterMap.put("/swagger-resource ...

  6. VUE:条件渲染和列表渲染

    条件渲染 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  7. %02x与%2x之间的区别

    输出最小宽度用十进制整数来表示输出的最少位数.若实际位数多于定义的宽度,则按实际位数输出,若实际位数少于定义的宽度则补以空格或0(当最小宽度数值以0开头时). X 表示以十六进制形式输出02表示不足两 ...

  8. 数组实例的 entries(),keys() 和 values()

    数组实例的 entries(),keys() 和 values() entries(),keys()和values(),用于遍历数组.它们都返回一个遍历器对象,可以用for...of循环进行遍历,唯一 ...

  9. POJ 2230 Watchcow

    Watchcow Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 2 ...

  10. 电子签章盖章之jQuery插件jquery.zsign

           简介:  使用jquery.zsign可以实现电子签章盖章效果,使用方便,只需提供自己的章图片.效果图如下:        页面引用:        <link href=&quo ...