LightOJ-1138-Trailing Zeroes (III)-二分+求N!末尾0
2024-08-31 15:29:03
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(需要取区间左边)
#include<stdio.h>
#include<iostream>
const int N=;
using namespace std; int findzero(int x)
{
int sum=;
while(x)
{
sum=sum+x/;
x=x/;
}
return sum;
} int erfen(int x)
{
int L=,R=N,mid;
while(L<=R)
{
mid=L+((R-L)>>);
if(findzero(mid)<x)
{
L=mid+;
}
else
{
R=mid-;
}
// else if(findzero(mid)==x)
// {
// return L;
// }
}
if(findzero(L)==x)
return L;
return ;
} int main()
{
int n,tt=,t,q;
scanf("%d",&t);
while(t--)
{
scanf("%d",&q);
int ans=erfen(q);
if(ans==)
printf("Case %d: impossible\n",tt++);
else
printf("Case %d: %d\n",tt++,ans); }
return ;
}
最新文章
- C#开发系统服务时用的定时器组件
- HTML5火焰文字特效DEMO演示---转载
- 利用jQuery和Ajax实现检测用户名是否已经被注册
- OVS ARP Responder – Theory and Practice
- How to generate number Sequence[AX 2012]
- [改善Java代码]对字符串排序 持一种宽容的心态
- FragmentActivity和Activity的具体区别在哪里
- zoj 3351 Bloodsucker(概率 dp)
- 强制转https
- JAVA邮件收发实现(待)
- php在客户端禁用cookie时让session不失效的解决方法
- 用Python删除本地目录下某一时间点之前创建的所有文件
- codechef [snackdown2017 Onsite Final] Fusing Weapons
- Jetson TX2上的demo(原创)
- AJAX的简洁写法
- 学习ASP.NET Core Razor 编程系列十二——在页面中增加校验
- gitlab搭建
- TCP学习总结(一)
- 一些常见的Java面试题 &; 面试感悟
- Visual Studio 代码快捷键