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) 二分的更多相关文章

  1. Light oj 1138 - Trailing Zeroes (III) (二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题目就是给你一个数表示N!结果后面的0的个数,然后让你求出最小的N. 我们可以知 ...

  2. LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)

    http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS     M ...

  3. 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 ...

  4. light oj 1138 - Trailing Zeroes (III)【规律&&二分】

    1138 - Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit:  ...

  5. Light oj 1138 - Trailing Zeroes (III) 【二分查找 &amp;&amp; N!中末尾连续0的个数】

    1138 - Trailing Zeroes (III) problem=1138"> problem=1138&language=english&type=pdf&q ...

  6. LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...

  7. lightoj 1138 - Trailing Zeroes (III)【二分】

    题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...

  8. LightOJ 1138 Trailing Zeroes (III) 打表

    就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...

  9. LightOj 1138 Trailing Zeroes (III)

    题目描述: 假设有一个数n,它的阶乘末尾有Q个零,现在给出Q,问n最小为多少? 解题思路: 由于数字末尾的零等于min(因子2的个数,因子5的个数),又因为2<5,那么假设有一无限大的数n,n= ...

随机推荐

  1. 编写自己的 GitHub Action,体验自动化部署

    本文将介绍如何使用 GitHub Actions 部署前端静态页面,以及如何自己创建一个 Docker 容器 Action. 简介 Actions GitHub Actions 是 GitHub 官方 ...

  2. Xcode10:library not found for -lstdc++.6.0.9 临时解决

    1.https://pan.baidu.com/s/1IkbZb6qaxgvghP1HEFQa6w?errno=0&errmsg=Auth%20Login%20Sucess&& ...

  3. 分布式唯一ID自增(雪花算法)

    public class IdWorker { // ==============================Fields===================================== ...

  4. 关于SOA架构设计的案例分析

    关于SOA架构设计的案例分析 面向服务的体系结构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来.它可以根据需求通过网络对松散耦合的粗粒度应 ...

  5. 无法解析导入javax.mail

    本文链接:https://blog.csdn.net/aaqian1/article/details/89357195下载地址: http://www.oracle.com/technetwork/j ...

  6. 微软 的 github的 weiapi dotnet的 也有了 作为菜 只有欣赏的额

    step one 猛戳 dotnet-apiweb

  7. python接口自动化测试 - configparser配置文件解析器详细使用

    configparser简介 ConfigParser模块已在Python 3中重命名为configparser 该模块定义了ConfigParser类. ConfigParser类实现一种基本的配置 ...

  8. JPA基本注解的使用

    一:JPA基本注解 使用: 使用: 使用: 查看表: 二:用table来生成主键 使用: allocationSize:每次增加多少 tablel:指定使用那张表 执行两次main方法后查看表: jp ...

  9. 分布式缓存Redis的持久化方式RDB和AOF

    一.前言 Redis支持两种方式的持久化,RDB和AOF.RDB会根据指定的规则“定时”将内存中的数据存储到硬盘上,AOF会在每次执行命令后将命令本身记录下来.两种持久化方式可以单独使用其中一种,但更 ...

  10. window平台基于influxdb + grafana + jmeter 搭建性能测试实时监控平台

    一.influxdb 安装与配置 1.1 influxdb下载并安装 官网无需翻墙,但是下载跳出的界面需要翻墙,我这里提供下载链接:https://dl.influxdata.com/influxdb ...