LightOJ 1138(求末尾零个数)
来源:互联网 发布:java 项目中使用log4j 编辑:程序博客网 时间:2024/05/22 03:23
Description
You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you knowN! = 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
3
1
2
5
Sample Output
Case 1: 5
Case 2: 10
Case 3: impossible
不难发现,一个数一共包含了几个5,就会有几个零;比如,
5以及5之前的数一共包含了1个5,所以末尾共有1个零;
20以及20之前的数一共包含了4个5(5自身为1个,10包含一个,15包含一个,20包含一个),所以末尾共有4个零;
25以及25之前的数一共包含了6个5(5,10包含一个,15包含一个,20包含一个,25包含另个(5*5等于25,所以25包含两个)),所以末尾共有6个零;
28以及28之前的数一共包含了6个5,所以末尾共有6个零;
……这样,我们只需要求出所要求的数n一共包含了几个5,然后在从0-500000000(因为Q最大是100000000,所以要查找的范围上限最大是500000000)中查找是否有一个数它所包含的5的个数等于n就行了,如果有等于n,那么输出查找到的这个数,如果没有,则输出不可能;
注意这里要用二分查找会减少时间复杂度避免超时;
#include<iostream>#include<cstdlib>#include<cstdio>#include<algorithm>#include<cstring>#include<map>#define inf 99999999#define mod 9973using namespace std;int tongji(int x)//统计当前数中5的个数{ int ans=0; while(x) { ans+=x/5; x=x/5; } return ans;}int serch(int n){ long long lower,upper,mid; lower=1,upper=500000000; while(lower<=upper)//二分查找 { mid=(lower+upper)/2; if(tongji(mid)<n) lower=mid+1; else upper=mid-1; } if(tongji(lower)==n)//能找到,则返回查找到的那个值; return lower; else return 0;}int main(){ int t; int m; int k=0; scanf("%d",&t); while(t--) { scanf("%d",&m); if(serch(m)==0) printf("Case %d: impossible\n",++k); else printf("Case %d: %d\n",++k,serch(m)); } return 0;}
- LightOJ 1138(求末尾零个数)
- LightOJ 1138(求末尾零个数)
- LightOJ 1138(求末尾零个数) 二分+数学
- 快速求N!末尾零的个数
- 求100!中的末尾的零的个数
- C++练习之求N!末尾零的个数
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- LightOJ 1138 Trailing Zeroes (III) (阶乘末尾0的个数)
- 100!末尾零的个数
- 阶乘末尾零的个数
- LightOJ 1138 - Trailing Zeroes (III) (求末尾0为x的最小N---二分)
- 求n的阶乘结果末尾含零的个数--记一次华为技术交流面试
- 给定数X,求X!的末尾连续零的个数
- UVA10061求不同进制的n!的数字位数和末尾零的个数
- 知识点(求n!的末尾有几个零)
- ACM-大数阶乘末尾零的个数
- ACM-大数阶乘末尾零的个数
- n的阶乘末尾零的个数
- 【机器学习】bias and variance
- MyEclipse 新建->后面的菜单选项设置
- linux下为java设置正确的时区
- 编写最小生成树代码遇到的小问题
- 分类讨论
- LightOJ 1138(求末尾零个数)
- SDUTACM 数据结构实验之排序七:选课名单
- myeclipse集成maven
- leetcode 168 Excel Sheet Column Title C++
- Spring MVC 前后台传值情况汇总
- 杭电-1498 50 years, 50 colors(二分匹配)
- 状态不好
- CmFree
- 暑期dp46道(42)--HDOJ 1789 Doing Homework again 贪心