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= ...
随机推荐
- 11.黑窗口、IDEA生成JavaDoc
JavaDoc: 它是一种技术,可以将一些注释信息生成一个帮助文档,就类似于Java的API JavaAPI帮助文档: https://www.oracle.com/cn/java/technolog ...
- Spring加载早期获取BasePackage
public class GetBasePackage { private Class<? extends Annotation> annotation; public GetBasePa ...
- CTF实验吧——证明自己吧
题目地址:http://www.shiyanbar.com/ctf/28 没有壳 ,vc++ 写的 拖进OD观察观察,发现代码很短哟,先来看这俩个call 怀疑他们其中有正确的flag和我们输入的东西 ...
- basic-pentesting-1 靶机提权
原文地址:https://www.payload.com.cn/ basic-pentesting-1 下载地址: https://www.vulnhub.com/entry/basic-pent ...
- 使用Python写的WingPro7 Pyside2 和 PyQt5插件
pyside2的 import wingapi import subprocess pyside2_uic = "pyside2-uic" pyside2_qrc = " ...
- CSS-13-块级元素和行内元素
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- python文件、文件夹的相关操作
python文件.文件夹的相关操作 #1.rename()可以完成对文件的重命名 #rename(需要修改的文件名,新的文件名) import os os.rename("readme.tx ...
- springBoot 启动没有数据库配置报错
在没有配置数据库的时候, 直接启动springBoot 项目 会有报错 Description: Failed to configure a DataSource: 'url' attribute i ...
- Spring注解开发系列Ⅸ --- 异步请求
一. Servlet中的异步请求 在Servlet 3.0之前,Servlet采用Thread-Per-Request的方式处理请求,即每一次Http请求都由某一个线程从头到尾负责处理.如果要处理一些 ...
- VS生成垃圾文件清理
@echo Off del /s /a *.txt *.exe *.suo *.ncb *.user *.dll *.pdb *.netmodule *.aps *.ilk 2>nul FOR ...