Trailing Zeroes (III)(lightoj 二分好题)
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer Q (1 ≤ Q ≤ 108) in a line.
Output
For each case, print the case number and N. If no solution is found then print 'impossible'.
Sample Input |
Output for Sample Input |
|
3 1 2 5 |
Case 1: 5 Case 2: 10 Case 3: impossible |
题解:这道题要找末尾0的个数所需要的最小阶乘,因为想到10=2*5,然而2是很多数的质因数,数目要大于质因数5的数目,所以只考虑5的质因数的个数,便为末尾0的个数,所以
int q=0;
while(x){
q+=x/5;
x/=5;
}
这点便可以得出x!末尾0的个数;
用二分,从无穷大开始找,找到Q就是答案;
代码:
#include<stdio.h>
const int MAXN=0x3f3f3f3f;//这个要足够大才能找到10^8
int getq(int x){
int q=;
while(x){
q+=x/;
x/=;
}
return q;
}
void erfen(int n){
int l=,r=MAXN;
while(l<=r){
int mid=(l+r)>>;
if(getq(mid)>=n)r=mid-;//二分这点注意
else l=mid+;
}
if(getq(l)==n)printf("%d\n",l);
else puts("impossible");
}
int main(){
int T,Q,flot=;
scanf("%d",&T);
while(T--){
scanf("%d",&Q);
printf("Case %d: ",++flot);
erfen(Q);
}
return ;
}
Trailing Zeroes (III)(lightoj 二分好题)的更多相关文章
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- 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 ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...
- Trailing Zeroes (III) LightOJ - 1138(二分)
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- Trailing Zeroes (III) LightOJ - 1138 二分+找规律
Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! ...
- Trailing Zeroes (III) LightOJ - 1138 不找规律-理智推断-二分
其实有几个尾零代表10的几次方但是10=2*510^n=2^n*5^n2增长的远比5快,所以只用考虑N!中有几个5就行了 代码看别人的: https://blog.csdn.net/qq_422797 ...
- Trailing Zeroes (III) (二分)题解
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...
- C - Trailing Zeroes (III)(二分)
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...
随机推荐
- MySQL----基本数据类型
1.数值型: tinyint:1字节 smallint:2字节 mediumint:3字节 int:4字节 bigint:8字节 float:4字节 double:8字节 decimal(m,c):m ...
- [问题解决] LaTex Error:Unknown graphics extension:.eps
错误:编译的时候显示:“!LaTex Error:Unknown graphics extension:.eps” 发生场景:Latex写论文 解决方案: latex eps.texdvi2ps ep ...
- listview优化
http://www.2cto.com/kf/201108/99928.html 项目用到ListView,由于要用到ImageView,图片源不是在资源里面的,没法使用资源ID,因此无法直接使用Si ...
- perl 正则命名捕获
#!/usr/bin/perl -w =pod 命名捕获--给匹配上的内容加上标签 捕获到的内容都会保存在%+散列中,这个散列的key为对应的标签: 方便之处就是利于程序扩展和阅读,不用繁琐的一个一个 ...
- zk 节点故障 重连机制
<pre name="code" class="html">如果在连接时候zk服务器宕机 To create a client session th ...
- docker 保存更改的镜像:
<pre name="code" class="ruby">保存更改的镜像: docker:/root# docker commit -m &quo ...
- 量身定制顺美男女西服、衬衫、大衣、T恤等 - 北京58同城
量身定制顺美男女西服.衬衫.大衣.T恤等 - 北京58同城 量身定制顺美男女西服.衬衫.大衣.T恤等 发布时间:2014-04-11浏览2次
- 实现多文件上传在iOS开发中
该功能实现了实现多文件上传在iOS开发中,喜欢的朋友可以研究一下吧. NSURL* url = [NSURL URLWithString:@"xxx"]; ASIFormDataR ...
- C# 4.0 新特性-dynamic 【转】
前段时间看过一些关于dynamic这个C#4中的新特性,看到有些朋友认为dynamic的弊大于利,如无法使用编译器智能提示,无法在编译时做静态类型检查,性能差等等.因此在这篇文章中我将就这些问题来对d ...
- spring security执行流程图
今天看到非常多人转载了这篇文章,这里备注一下.原文来自CSDN我的博客. 近期在研究spring security的配置,研究了一个星期了,在官网看了下.仅仅配置出来了简单的登录,但不知如何从数据库读 ...