LightOJ-1138-Trailing Zeroes (III)-二分+求N!末尾0
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
题意:
给出一个Q,求出最小的n的阶乘的末尾0(后导)的个数为Q 。
思路:
0是由2*5组成的,也就是相当于一个偶数乘上一个5的倍数,而偶数肯定是跟在5的前面的,所以只需要求出5的倍数(包括5)的个数即可。
注意:
N的范围、求的是最小的N(需要取区间左边)
1 #include<stdio.h> 2 #include<iostream> 3 const int N=500000020; 4 using namespace std; 5 6 int findzero(int x) 7 { 8 int sum=0; 9 while(x) 10 { 11 sum=sum+x/5; 12 x=x/5; 13 } 14 return sum; 15 } 16 17 int erfen(int x) 18 { 19 int L=0,R=N,mid; 20 while(L<=R) 21 { 22 mid=L+((R-L)>>1); 23 if(findzero(mid)<x) 24 { 25 L=mid+1; 26 } 27 else 28 { 29 R=mid-1; 30 } 31 // else if(findzero(mid)==x) 32 // { 33 // return L; 34 // } 35 } 36 if(findzero(L)==x) 37 return L; 38 return 0; 39 } 40 41 42 int main() 43 { 44 int n,tt=1,t,q; 45 scanf("%d",&t); 46 while(t--) 47 { 48 scanf("%d",&q); 49 int ans=erfen(q); 50 if(ans==0) 51 printf("Case %d: impossible\n",tt++); 52 else 53 printf("Case %d: %d\n",tt++,ans); 54 55 } 56 return 0; 57 }
分类:
数论
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」