【Light-oj】-1138 - Trailing Zeroes (III)(二分,数学)
来源:互联网 发布:java的框架 编辑:程序博客网 时间:2024/05/01 07:57
1138 - Trailing Zeroes (III)
PDF (English)StatisticsForum
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
题解:和之前做的一道题一样就是看5有多少个因为0的形成是由10或者是2*5产生的呀!
开始很纠结不明白为什么5个0是不可能的,以为25!就有5个0了,后来才想到25!会形成6个0,因为25=5*5呀!!!不要忽视啊!!!
打表会超内存的,所以这里二分,简单二分,看看就懂啦!
最后要记得再检查一下,从样例就可以发现第三组输出了25,在检查一次就ok啦!
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;#define INF 0x3f3f3f3f#define CLR(a,b) memset(a,b,sizeof(a))#define LL long longLL n;LL check(LL x){LL sum=0;while(x){sum+=x/5;x/=5;}return sum;//求 x!会有多 0 }int main(){LL u,ca=1;scanf("%d",&u);while(u--){scanf("%lld",&n);LL L=1,R=10000000000;while(R>=L){LL mid=(L+R)>>1;if(check(mid)>=n)R=mid-1;elseL=mid+1;}printf("Case %d: ",ca++);LL ant=0;R=L;while(L){ant+=L/5;L/=5;}if(ant==n)printf("%lld\n",R);elseprintf("impossible\n");}return 0;}
0 0
- 【Light-oj】-1138 - Trailing Zeroes (III)(二分,数学)
- Light oj-1138 Trailing Zeroes (III) (二分&数学)
- 【Light OJ 1138 】Trailing Zeroes (III) 【二分+数学】
- light oj 1138 - Trailing Zeroes (III)《《二分》》
- Light OJ:1138 Trailing Zeroes (III)(二分)
- Light oj Trailing Zeroes (III) (二分查找)
- 【Light oj 1138 】- Trailing Zeroes (III)(二分,思维)
- Light oj 1138 - Trailing Zeroes (III)
- Light OJ 1138 - Trailing Zeroes (III)
- Light oj 1138 - Trailing Zeroes (III)
- Light oj 1138 -Trailing Zeroes (III)
- LIGHT OJ 1138 - Trailing Zeroes (III)【N!后0的个数&&二分(好题)】
- *lightoj 1138 Trailing Zeroes (III) | 二分+数学
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
- Light OJ 1138:Trailing Zeroes (III)【二分+求阶乘中某质因子的幂】
- LightOJ 1138Trailing Zeroes (III)(二分)
- 【lightoj 1138】Trailing Zeroes (III) (二分)
- Trailing Zeroes (III) (数学)
- 异步、消息、通知、菜单 的介绍
- 【Lightoj 1282 Leading and Trailing】
- maven3 手动安装本地jar到仓库
- 安卓中的控件大发现11个总有你需要的
- java多线程
- 【Light-oj】-1138 - Trailing Zeroes (III)(二分,数学)
- 98. Validate Binary Search Tree
- GitHub常用开源框架
- c++模板的问题解析-问题2
- 边之比的平方=面积之比
- Spring 快速学习笔记
- JAVA深入研究——Method的Invoke方法。
- 分享一下书签
- Linux感悟-前言