Light OJ 1138 (二分查找+分解阶乘)
来源:互联网 发布:d3.js 颜色渐变 编辑:程序博客网 时间:2024/05/18 02:34
G - G 使用long long
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1138 uDebug
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 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
3
1
2
5
Sample Output
Case 1: 5
Case 2: 10
Case 3: impossible
题意:
让你求一个最小的n,使得n的阶乘含有q个0;
思路:任意一个数都可以写成质因数的幂的乘积,n=2^a*5^b*……;其中2的幂数肯定比5的多,而0的产生是有2*5;
所以5的个数决定了0的个数;有多少个0就要有多少个5;所以问题转化为求n中5的个数;
5!~1 10!~2 15!~3 20! ~4 25! ~6 30! ~7;
然后二分查找n,记得右区间要开大点;
代码:
#include<stdio.h>#include<string.h>long long judge(long long mid)//求n中含有的5的个数; {long long ans=0;while(mid){ans+=mid/5;mid/=5;}return ans;}int main(){int t,q,mm=1;scanf("%d",&t);while(t--){scanf("%d",&q);long long l=0,r=5000000000;long long sum=0;bool flag=0;while(l<=r){long long mid=(l+r)/2;if(judge(mid)==q){sum=mid;r=mid-1;//因为要找最小的n,所以满足题意之后仍然要往左找; flag=1;}else if(judge(mid)>q){r=mid-1;}else{l=mid+1;}}if(flag)printf("Case %d: %lld\n",mm++,sum);elseprintf("Case %d: impossible\n",mm++);}return 0;}
0 0
- Light OJ 1138 (二分查找+分解阶乘)
- Light OJ-----1138二分
- Light oj Trailing Zeroes (III) (二分查找)
- Light OJ 1138:Trailing Zeroes (III)【二分+求阶乘中某质因子的幂】
- LightOJ 1138 Trailing Zeroes (二分+阶乘分解+思维)
- Light OJ:1138 Trailing Zeroes (III)(二分)
- 【Light-oj】-1138 - Trailing Zeroes (III)(二分,数学)
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
- light oj 1138 - Trailing Zeroes (III)《《二分》》
- 【Light-oj】-Expanding Rods(二分&几何)
- 【light-oj】-’1307 - Counting Triangles(二分)
- light oj 1062 二分
- light oj 1236 【大数分解】
- light oj 1189 - Sum of Factorials (阶乘+贪心)
- light oj 1236 Pairs Forming LCM(整数分解)
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- HDU 3641 Treasure Hunting(阶乘素因子分解+二分)
- LIGHT OJ 1138 - Trailing Zeroes (III)【N!后0的个数&&二分(好题)】
- 初见PostgreSQL
- Recommended Doctype Declarations to use in your Web document
- 半透明Activity来实现对话框效果小技巧
- 充电送APP?火车站的这种充电桩太可怕了!
- Visualising Residuals
- Light OJ 1138 (二分查找+分解阶乘)
- Terminator 四窗口配置文件
- iOS 中几种常用的定时器
- java中+=是什么意思
- MTK Android 5.0以上版本的变化
- OpenCV中mat::copyto( )函数使用方法
- mysql 5.7.14安装成功
- JQ点击展开二级菜单
- 前端自动化工具grunt介绍