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 ...
随机推荐
- QT error LNK2019: 无法解析的外部符号
一个见到那的错误,困扰了好几天了,今天才解决,记录下. 使用QT Creator建立项目,添加一个QT设计界面widget,命名为TestWidget.有ui,头文件(.h),源码文件(.cpp).在 ...
- VsCode中vim插件剪切板等问题
剪切板共享 这个挺重要的,否则每次右键菜单复制粘贴会奔溃的. 在用户设置中添加: "vim.useSystemClipboard": true, 光标的变化 我觉得这个也重要,毕竟 ...
- html 1.0 鼠标放上去会亮 onmouseover onmouseout 以及this标签的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- gradle 刷新缓存
gradle build --refresh-dependencies -x test
- Symfony 2.0 认识Request, Response, Session, Cookie
在上一节中,我们提到了如何创建一个Bunlde 并且在默认控制器中添加一些方法.如果有参照之前的说法进行的话,读者很有可能会被提示说 返回的Response对象不能为空.好啦,我们就来研究一下,怎么从 ...
- 单独运行shell脚本与crontab运行shell脚本的区别
crontab运行脚本存在两大问题:环境变量和路径,从而导致单独运行脚本没问题,但用crontab运行就报错. 1.环境变量 描述问题:crontab: usage error: no argumen ...
- Application.streamingAssetsPath
[Application.streamingAssetsPath] This API contains the path to the StreamingAssets folder (Read Onl ...
- BashProfile
[BashProfile] ~/.bash_profile. alias ll='ls -l -G -a' alias gp='grep --colour -R'
- SpringBoot配置Shiro时@RequiresRoles不起作用
在SpringBoot中配置Shiro,结果@RequiresRoles.@RequiresPermissions等注解都无效 解决方法: 在ShiroConfiguration中注入开启支持aop. ...
- des,原理待续
网络上转载的代码,忘记出处了请原作者见谅! des类 import java.security.*; import javax.crypto.*; /** * DES加解密算法 */ public c ...