1138 - Trailing Zeroes (III) 二分
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 |
题意:找到一个最小自然数N,满足N的阶乘后面Q个0.
分析:二分扫一下大区间, 然后由算数基本定理可知N!可以转换为素因子相乘的形式N! = 2^a+3^b + ......
10只能由2和5相乘得到,而2的个数比5多,所以只用求5的个数即可。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
long long n;
long long get_five(long long m)///函数解释链接http://blog.csdn.net/ydd97/article/details/47083319
{
long long five = 5;
long long ans = 0;
while(m >= five)
{
ans += m / five;
five *= 5;
}
return ans;
}
long long solve(long long l, long long r)
{
long long mid = (l + r) / 2;
long long ans = get_five(mid);
if( l == r)
{
if(ans == n)
return mid;
else
return -1;
}
if(ans > n)
{
solve(l, mid );
}
else if(ans < n)
{
solve(mid + 1, r);
}
else
return mid;
}
int main()
{
int T, cas;
scanf("%d", &T);
cas = 0;
while(T--)
{
cas++;
scanf("%lld", &n);
long long ans = solve(0, 10000000000000);
printf("Case %d: ", cas);
if(ans == -1)
printf("impossible\n");
else
{
while(get_five(ans-1) == n)///找最小值。
ans--;
printf("%lld\n", ans);
}
}
return 0;
}
1138 - Trailing Zeroes (III) 二分的更多相关文章
- Light oj 1138 - Trailing Zeroes (III) (二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题目就是给你一个数表示N!结果后面的0的个数,然后让你求出最小的N. 我们可以知 ...
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...
- light oj 1138 - Trailing Zeroes (III)【规律&&二分】
1138 - Trailing Zeroes (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Light oj 1138 - Trailing Zeroes (III) 【二分查找 && N!中末尾连续0的个数】
1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...
- LightOj 1138 Trailing Zeroes (III)
题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...
随机推荐
- 完美解决win10系统无法安装.NET Framework问题
今天在安装willow插件的时候系统提示需要安装.NET Framework3.5的问题,当点击系统自动解决的时候,Windows系统又会提示错误,其实这也见怪不怪了,如果能自动解决的话也不会出现这种 ...
- bzoj 2683 CDQ分治
题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...
- numpy 数组的拼接
一.数组的拼接 1.水平拼接 a.格式 np.hstack((数组1, 数组2)) # 注意: 值是元祖 # 0轴长要相同 b.例子 import numpy as np arr1 = np.aran ...
- 从0开发3D引擎:目录
介绍 大家好,本系列带你踏上Web 3D编程之旅- 本系列是实战类型,从0开始带领读者写出"良好架构.良好扩展性.优秀的性能.最小功能集合(MVP)" 的3D引擎. 本系列的素材来 ...
- Dynamics 365 CRM 在 Connected Field Service 中部署 IoT Central (一)- 配置 IoT Central和IoT alert
今天这个系列给大家带来怎样在connected field service中部署IoT Central 并且做连接. 首先, 这里提供微软官方的tutorial的链接https://docs.micr ...
- python读取文件使用相对路径的方法
场景描述: python传统的读取文件的方法,通过读取文件所在目录来读取文件,这样出现的问题是,如果文件变更了存储路径,那么就会读取失败导致报错 如下方脚本 def stepb(a):#写入txt f ...
- servlet 深入了解
servlet 作用 在Java web b/s架构中,servlet扮演了重要的角色,作为一个中转处理的容器,他连接了客户端和服务器端的信息交互和处理.简单来说,客户端发送请求,传递到servle ...
- 论文翻译:Mastering the Game of Go without Human Knowledge (第一部分)
长久以来,人工智能的一个目标是在那些具有挑战性的领域实现超过人类表现的算法.最近,AlphaGo成为了在围棋上第一个打败了世界冠军的程序.在AlphaGo中,使用深度神经网络来进行树搜索,评估位置,和 ...
- sass css样式:@for循环、样式变量与#{} 变量插值
/* sass 可以用写JS的思想来写CSS代码 * #{} 用来插值,大括号中填写需要插入的变量 * @for 变量 from ...
- kvm命令
查询:virsh -c qemu:///system list 查看当前的虚拟系统 brctl show 列出当前所有的网桥接口virsh list 列出运行的虚拟机virs ...