题目链接: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. iOS UIImage的解码时机

    在看博客 UITableView优化技巧 时想到列表的优化主要还是对图片的优化处理. 博文中介绍了按需加载.快速滑动时不加载.异步刷新等等技巧. 这里有个问题, 当我们实例化一个UIImage对象并为 ...

  2. ZBrush中Blob点滴笔刷介绍

    对于ZBrush®来说,笔刷的使用时至关重要的,ZBrush中给我们提供了越来越多的笔刷的同时,我们也要根据经验来合理选择笔刷.本文内容小编将分享Blob点滴笔刷的简单介绍,该笔刷在使用时笔头犹如一股 ...

  3. Eclipse中合并GIT分支

    合并GIT分支: 1.  切换到主分支: 2.  右击项目——Team——Merge…: 3.  在弹出的Merge框中选择要合并的分支——Merge: 4.  合并后如果出现冲突,右击项目——Tea ...

  4. CF894E Ralph and Mushrooms_强连通分量_记忆化搜索_缩点

    Code: #include<cstdio> #include<stack> #include<cstring> using namespace std; cons ...

  5. AIM Tech Round 5 1028cf(A-E)

    AIM Tech Round 5 (codeforces上题目编号是1028)(A-E) ---完全被这次比赛打击,自己真的很渣--- 战况 依旧3题选手 被构造题坑得好惨 稍稍涨了rating,希望 ...

  6. Jquery复习总结

    1.选择器: $(".class") $("#id") $("div") $("a p") $(div:first).c ...

  7. django-10-中间件和上下文管理器

    <<<中间件的引入>>> 用户<->中间件<->url->视图  在app目录里面 middleware.py  (1)中间件就是一个 ...

  8. Java基础学习总结(33)——Java8 十大新特性详解

    Java8 十大新特性详解 本教程将Java8的新特新逐一列出,并将使用简单的代码示例来指导你如何使用默认接口方法,lambda表达式,方法引用以及多重Annotation,之后你将会学到最新的API ...

  9. ASP.NET-技巧01

    ==符号的写法 ViewBag.StatusMessage = message == ManageMessageId.ChangePasswordSuccess ? "你的密码已更改.&qu ...

  10. [React] Understanding setState in componentDidMount to Measure Elements Without Transient UI State

    In this lesson we'll explore using setState to synchronously update in componentDidMount. This allow ...