LightOJ-1138 Trailing Zeroes (III) 唯一分解定理 算n!的某个因数个数
题目链接: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!的某个因数个数的更多相关文章
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- LightOj 1138 Trailing Zeroes (III)
题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...
- light oj 1138 - Trailing Zeroes (III)【规律&&二分】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...
- 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 ...
- 1138 - Trailing Zeroes (III) 二分
1138 - Trailing Zeroes (III) You task is to find minimal natural number N, so that N! contains exa ...
随机推荐
- Unity 围绕X、Y、Z旋转图例
绿色:绕X 红色:绕Y 蓝色:绕Z PS:这是右手坐标系,Unity为左手坐标系 不知道啥叫左手右手?参见我的另一篇文章http://www.cnblogs.com/36bian/p/7571727. ...
- Guitar Pro 的双十一特惠活动,正在如火如荼进行中...
11月11日这个令人兴奋的日子又来了.没错,“双十一”所有网购达人狂欢的日子.同时期待已久的Guitar Pro 也将在“双十一”当天,把福利分享与你我.11月11日Guitar Pro 将在麦软商城 ...
- Python多线程一学就会!
免费Python课程:阿里云大学——开发者课堂 Python中使用线程有两种方式:函数或者用类来包装线程对象. 函数式:调用thread模块中的start_new_thread()函数来产生新线程.语 ...
- Java开发就业形势和面试技巧
如果从软件编程的就业来讲,如果你现在不懂架构,那么找到一份好工作还是比较难的,但是这里面有两点需要注意: 传统软件公司,这类公司还会使用最为原始的开发技术(SSH),但是这样的传统软件公司的招聘量已经 ...
- Pyhton学习——Day39
# CSS的常用属性# 1 颜色属性# <div style="color:rgb(255,0,0)">ppppp</div># 2 字体属性# font- ...
- POST和GET详解
GET和POST Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上 ...
- github删除项目or仓库
1. 登录 github (要注册账号) 2. 登录后点击右上侧头像,选择 Your profile . 3. 选择Repositories,可以查看已有的库,选择要删除的库进入. 4. 选择Sett ...
- HDU 5918 Sequence I
题目来源:2016 CCPC 长春站 题意:给出两个序列 a[] , b[] ,如果b1,b2....bm能够与aq,aq+p,aq+2p...aq+(m-1)p对应( q+(m-1)p<=n ...
- Linux之awk使用
基本语法 $n :当前记录的第n个字段,比如n为1表示第一个字段,n为2表示第二个字段 $0:执行过程中当前行的文本内容 \t:制表符 \n:换行符 -F'[:#/]' : 定义三个分隔符,注意有-F ...
- MongoDB入门 常用命令以及增删改查的简单操作
1,运行MongoDB服务mongod --dbpath=/usr/local/developmentTool/mongo/data/db/然后启动客户端mongo2,sudo service mon ...