LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
来源:互联网 发布:数据工具培训心得 编辑:程序博客网 时间:2024/05/21 10:56
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
PROBLEM SETTER: JANE ALAM JAN
题意:给你一个数字,这个数字代表N!后面有几个0。给出这个数字,计算N的值。
解题思路:
任何质因数都可以写成素数相乘的形式。所以计算一个数的阶乘后面几个0,只需计算这个数包含多少5即可。(关于这点不清楚的点:点击打开链接)。
可以用二分法,找出这个点。想到用二分这道题也就没什么难度了。
AC代码;
<span style="font-size:18px;">#include <stdio.h>#include <math.h>#include <vector>#include <queue>#include <string>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long LL;LL solve(LL n){ LL num=0; while(n){ num+=n/5; n/=5; } return num;}LL er(LL n){ LL x=1; LL y=1844674407370; LL mid; LL res=-1; while(y>=x){ mid=(x+y)/2; LL ans=solve(mid); if(ans==n){ res=mid; y=mid-1; //return mid; } else if(ans>n){ y=mid-1; } else if(ans<n){ x=mid+1; } } return res;}int main(){ int t; scanf("%d",&t); int xp=1; while(t--){ LL n; scanf("%lld",&n); LL ans=er(n); if(ans==-1) printf("Case %d: impossible\n",xp++); else printf("Case %d: %d\n",xp++,ans); } return 0;}</span>
0 0
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- LightOJ-1138 Trailing Zeroes (III) (二分搜索)
- LightOJ 1138 Trailing Zeroes (二分+阶乘分解+思维)
- *lightoj 1138 Trailing Zeroes (III) | 二分+数学
- lightoj 1138 - Trailing Zeroes (III)【二分】
- Lightoj 1138 Trailing Zeroes (III) 【二分】
- LightOJ 1138 Trailing Zeroes (III)(二分)
- LightOJ 1138 - Trailing Zeroes (III)【二分】
- LightOJ 1138Trailing Zeroes (III)(二分)
- LightOJ 1138 Trailing Zeroes (III) 二分查找
- LightOJ 1138 - Trailing Zeroes (III) 二分
- 【lightoj 1138】Trailing Zeroes (III) (二分)
- lightoj-1138-Trailing Zeroes (III) -二分查找
- LightOJ 1138 - Trailing Zeroes (III)(二分&阶乘末尾0的个数)
- LightOJ 1138 Trailing Zeroes (III) (阶乘末尾0的个数)
- lightOJ 1138 Trailing Zeroes (III)
- LightOJ 1138 Trailing Zeroes (III)
- 【Windows编程】系列第十篇:文本插入符
- 类和接口的设计原则
- LeetCode 28 StrStr 解法为KMP&&Rolling hash
- win8下 node.js模块msnodesql的配置(附连接sqlserver2008 demo)
- 多线程 - 01.多线程概述
- LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】
- eclipse自动补全的设置
- 多线程 - 02.利用pthread实现多线程
- Allegro过孔打在焊盘上(via与pad重叠),如何显示DRC错误?
- Android-自定义发送/接收广播(BroadcastReceiver)
- extern 修饰全局变量深度全方位剖析
- hdu 5411 多校——矩阵快速幂模板
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
- 多线程 - 03.NSThread使用