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. xtrabackup备份还原mariadb数据库

    一.xtrabackup 简介 xtrabackup 是由percona公司开源免费的数据库热备软件,它能对InnoDB数据库和XtraDB存储引擎的数据库非阻塞地备份,对于myisam的备份同样需要 ...

  2. Mysql连接出现时区问题

    错误提示: The server time zone value '¥x¥_¼Ð·Ç®É¶¡' is unrecognized or represents more than one time zon ...

  3. Android studio 连接真机

    首先用数据线连接真机 1.打开开发者模式(小米手机mix2s为例 设置->我的设备->全部参数->连续点击MIUI版本——开启成功) 2.在更多设置中找到系统安全设置——允许安装未知 ...

  4. CTF实验吧——证明自己吧

    题目地址:http://www.shiyanbar.com/ctf/28 没有壳 ,vc++ 写的 拖进OD观察观察,发现代码很短哟,先来看这俩个call 怀疑他们其中有正确的flag和我们输入的东西 ...

  5. 大叔 Frameworks.Entity.Core 2 PageList

    Frameworks.Entity.Core\Commons\PageList\ 1 分页通用类 ListPageList<T> 继承  PageListBase<T>, IP ...

  6. Nginx搭建过程

    https://www.cnblogs.com/gscq073240/articles/6773000.html

  7. RSTP协议简介

    RTSP(Real-Time Stream Protocol)协议是一个基于文本的多媒体播放控制协议,属于应用层.RTSP以客户端方式工作,对流媒体提供播放.暂停.后退.前进等操作.该标准由IETF指 ...

  8. 缓存 ehcache

    只是用于自己记录,防止日后忘记,回看所用 第一步:配置ehcahe 缓存 <?xml version="1.0" encoding="UTF-8"?> ...

  9. centos7+ docker 实践部署docker及配置direct_lvm

    转载于博客园:http://www.cnblogs.com/Andrew-XinFei/p/6245330.html 前言 Docker现在在后端是那么的火热..尤其当笔者了解了docker是什么.能 ...

  10. Spring博客专栏

    1. IOC和DI的理解 2. IOC的实现原理-反射与工厂模式 3. Spring进阶之路 4. SpringBoot配置类的使用 5. @ModelAttribute详解 6. @RequestP ...