light oj 1138
2015-09-20 11:32
225 查看
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1138
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
Submit Status Practice LightOJ 1138
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,使其阶乘的末尾有Q个0.
思路:只有2和5相乘时末尾才会出现0,在阶乘过程中,因子2的个数足够多,所以每遇到一个5,末尾就会出现一个0,因此问题转化为求1到N这N个整数中包含了多少个因子5。
#include<stdio.h> #include<string.h> #define MAX 0x3f3f3f #define LL long long LL fun(LL x) { LL ans=0; while(x) { ans+=x/5; x=x/5; } return ans; } int main() { int n,k=1; LL m; scanf("%d",&n); while(n--) { scanf("%lld",&m); LL left=0; LL right=400000010; LL mid; while(left<=right) { mid=(left+right)>>1; if(fun(mid)>=m) right=mid-1; else left=mid+1; } printf("Case %d: ",k++); if(fun(left)!=m) printf("impossible\n"); else printf("%lld\n",left); } return 0; }
相关文章推荐
- 如何在android studio上使用SVN
- A014-values资源
- sublime 添加 ConvertToUTF-8
- JAVA Lambda表达式教程
- org.springframework.beans.factory.BeanCreationException: Error creating bean SMSService
- UCGUI使用外部字库芯片显示汉字
- 09.20 结构体
- Android Action Bar 详解篇
- NSArray 初始化的三种方法和遍历的三种方法
- 安卓开发退出程序的几种方法
- MyEclipse junit测试问题initializationError
- Linux下编译或者升级git
- Android 属性动画(Property Animation 给按钮的添加添加动画效果,同样可以用在整个布局上) (下)(viewGroup创建对象竟然可以用布局的id)
- Linux函数fstat说明
- 测试用例编写要素与模板
- 多线程socket编程--聊天程序
- Error:Execution failed for task ': :dexDebug'. > com.android.ide.common.process.ProcessException: o
- 为什么要开通本博?
- 网页上禁止复制怎么办
- 结构性模式:装饰模式