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

转一下题解:原文地址:https://blog.csdn.net/zs120197/article/details/52244482

不难发现,一个数一共包含了几个5,就会有几个零;比如,

5以及5之前的数一共包含了1个5,所以末尾共有1个零;

20以及20之前的数一共包含了4个5(5自身为1个,10包含一个,15包含一个,20包含一个),所以末尾共有4个零;

25以及25之前的数一共包含了6个5(5,10各包含一个,15包含一个,20包含一个,25包含另个(5*5等于25,所以25包含两个)),所以末尾共有6个零;

28以及28之前的数一共包含了6个5,所以末尾共有6个零;

……
这样,我们只需要求出所要求的数n一共包含了几个5,然后在从0-500000000(因为Q最大是100000000,所以要查找的范围上限最大是500000000)中查找是否有一个数它所包含的5的个数等于n就行了,如果有等于n,那么输出查找到的这个数,如果没有,则输出不可能;
注意这里要用二分查找会减少时间复杂度避免超时;

代码如下:

题中要求的是最小的N 所以注意二分的范围问题。。。

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define maxn 100009
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int LL_INF = 0x7fffffffffffffff,INF = 0x3f3f3f3f; LL init(LL x)
{
int cnt = ;
while(x)
{
cnt += x / ;
x /= ;
}
return cnt;
} LL check(LL Q)
{
LL x = , y = ;
while(x <= y)
{
LL m = x + (y-x)/;
int ans = init(m);
if(Q <= ans) y = m-;
else x = m+;
}
if(init(x) == Q) return x;
return ;
} int main()
{
int T, kase = ;
cin>> T;
while(T--)
{
LL Q;
cin>> Q;
int ix = check(Q);
if(ix)
printf("Case %d: %d\n",++kase,ix);
else
printf("Case %d: impossible\n",++kase); } return ;
}

Trailing Zeroes (III) LightOJ - 1138(二分)的更多相关文章

  1. Trailing Zeroes (III) LightOJ - 1138 二分+找规律

    Time Limit: 2 second(s) Memory Limit: 32 MB You task is to find minimal natural number N, so that N! ...

  2. Trailing Zeroes (III) LightOJ - 1138 不找规律-理智推断-二分

    其实有几个尾零代表10的几次方但是10=2*510^n=2^n*5^n2增长的远比5快,所以只用考虑N!中有几个5就行了 代码看别人的: https://blog.csdn.net/qq_422797 ...

  3. Trailing Zeroes (III)(lightoj 二分好题)

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

  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)(二分 + 思维)

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

  7. LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】

    1138 - Trailing Zeroes (III) PDF (English) problem=1138" style="color:rgb(79,107,114)" ...

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

  9. 1138 - Trailing Zeroes (III) 二分

    1138 - Trailing Zeroes (III)   You task is to find minimal natural number N, so that N! contains exa ...

随机推荐

  1. c语言学习5

    break 和 continue之间的区别: 在1000人中,募捐100000元,当达到10万元后结束   break 跳出当前循环,即  是终止循环,continue结束本次循环,不终止循环 #in ...

  2. Zookeeper-集群与单机实践

    我用的是linux,CentOS7.3,zookeeper的版本是3.4.6,工具XShell.上传zookeeper的压缩包后我们开始操作. 集群模式: 1.解压zookeeper,路径随意 tar ...

  3. Flutter - 退出App

    Flutter退出App的方法一般有两种 ①SystemNavigator.pop 推荐 onTap: () async { await pop(); }, static Future<void ...

  4. 从0到1上线一个微信小程序

    0.0 前期准备 微信小程序的出现极大地降低了个人开发者微创业的门槛,不需要后端技术,不需要服务器和域名这些乱七八糟的前置操作,只需要懂得前端技术,就能发布一款属于自己的轻量级应用,简直是前端开发者的 ...

  5. 值类型和引用类型的区别,struct和class的区别

    C#值类型和引用类型 1.简单比较 值类型的变量直接存储数据,而引用类型的变量持有的是数据的引用,数据存储在数据堆中. 值类型(value type):byte,short,int,long,floa ...

  6. Centos下堡垒机Jumpserver V3.0环境部署完整记录(1)-安装篇

    由于来源身份不明.越权操作.密码泄露.数据被窃.违规操作等因素都可能会使运营的业务系统面临严重威胁,一旦发生事故,如果不能快速定位事故原因,运维人员往往就会背黑锅.几种常见的运维人员背黑锅场景:1)由 ...

  7. Spring RPC 入门学习(2)-获取Map对象

    Spring RPC传递Map用例编写 1. 新建RPC接口类 package com.cvicse.ump.rpc.interfaceDefine; import java.util.Map; pu ...

  8. 【Beta版本发布】爬虫队长装备全面更新!

    一.Beta阶段目标回顾 1.为了解决Alpha阶段线程异常泛滥的问题,我们需要一个线程池. 2.为了爬取得到的文件正确可用,我们需要一个异常清理器. 3.为了不间断爬取,管理员不必频繁运行程序点,我 ...

  9. 实战框架ABP

    abp及实战框架概述 接触abp也快一年了,有过大半年的abp项目开发经验,目前项目中所用的abp框架版本为0.10.3,最新的abp框架已经到了1.4,并且支持了asp.net core.关于abp ...

  10. opencv学习笔记(一)

    摘要:最近要做一个和图像处理有联系的项目,从此走上了学习opencv的道路. 灰度图:2维矩阵 彩色图:3维矩阵 ps:目前大部分设备都是用无符号 8 位整数(类型为 CV_8U)表示像素亮度 Mat ...