LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
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使N!中0的个数等于q
0是有5乘4、2、8等等构成的,N中只要有因子5,那么N!中一定能构成0,所以我们只需要找N中因子5的个数,然后用二分来快速查找N
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm> using namespace std;
typedef long long ll; ll solve(ll n)
{
ll ans = ;
while(n)
{
ans += n / ;
n /= ;
}
return ans;
}//求n中因子5的个数 int main()
{
int t, p = ;
ll q;
scanf("%d", &t);
while(t--)
{
p++;
scanf("%lld", &q);
ll low = , high = ;
while(low <= high)
{
ll mid = (low + high) / ;
ll m = solve(mid);
if(m < q)
low = mid + ;
else
high = mid - ;
}//二分
if(solve(low) == q)
printf("Case %d: %lld\n", p, low);
else
printf("Case %d: impossible\n", p);
}
return ;
}
LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)的更多相关文章
- 1138 - Trailing Zeroes (III) 二分
1138 - Trailing Zeroes (III) You task is to find minimal natural number N, so that N! contains exa ...
- 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,由于 ...
- Light oj 1138 - Trailing Zeroes (III) (二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题目就是给你一个数表示N!结果后面的0的个数,然后让你求出最小的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= ...
- 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 ...
随机推荐
- 自己动手实现RPC服务调用框架
转自:http://www.cnblogs.com/rjzheng/p/8971629.html#3977269 担心后面忘了,先转了,后面借鉴实现一下RPC -------------------- ...
- python爬虫----基本操作
一.爬虫基本操作 有些网站和其他网站是有关系(链接),全球的网站就相当于一个蜘蛛网,我们放一只蜘蛛在上面爬,一定能够把网爬个遍.那么如果我们要爬取互联网上内容我们就相当于放一只蜘蛛在上面. 爬虫分为 ...
- spring data jpa 的各种查询总结
参考哦:https://blog.csdn.net/weixin_36667844/article/details/79945156
- 配置vmWare10(桥接模式),使得虚拟机成为网络中的一台独立的主机
受到启发的原文:https://www.cnblogs.com/liongis/p/3265458.html 第一步:配置虚拟网络 第二步:配置桥接(VMnet0) 第三步:给虚拟机配置桥接模式 第四 ...
- 参数中传Null值
参数中传Null值虽然不是一种优雅的方式,但有时候可以省时间.不过不推荐.
- Java调用SQL Server的存储过程详解
转载自Microsoft的官方文档 http://msdn2.microsoft.com/zh-cn/library/ms378995.aspx收录于 www.enjoyjava.net/f25 本文 ...
- 南京大学发布无序列限制的DNA编辑新工具(转自生物通)
编辑推荐: 内切酶经过改造可以成为强大的DNA编辑工具,比如ZFN.TALEN.风头正劲的CRISPR–Cas系统和充满争议的NgAgo技术.不过这些技术都是通过序列识别来实现靶向切割的,会受到序列偏 ...
- 01d-1: 算法分析
- MFC The Screen Flickers When The Image Zoomed
问题描述 当初写MFC也是不情愿的. 既然写了,遇到一些问题. 解决也废了一切功夫.所以简单的记录一下. 这个问题,也就是使用MFC显示图像的时候, 放缩图像的过程中, 图像会一闪一闪的. 这个问题的 ...
- Golang 字符编码
需要添加的库 go get code.google.com/p/go.text/encoding go get code.google.com/p/go.text/transform 两个转码函数 i ...