http://lightoj.com/volume_showproblem.php?problem=1138

Trailing Zeroes (III)

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

  1. 1138 - Trailing Zeroes (III) 二分

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

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

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

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

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

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

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

  5. LightOJ 1138 Trailing Zeroes (III) 打表

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

  6. LightOj 1138 Trailing Zeroes (III)

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

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

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

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

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

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

随机推荐

  1. SQL Server实时同步更新远程数据库遇到的问题

    工作中遇到这样的情况,需要在更新表TableA(位于服务器ServerA 172.16.8.100中的库DatabaseA)同时更新TableB(位于服务器ServerB 172.16.8.101中的 ...

  2. 使用TensorFlow识别照片中的物体

    1.环境ubuntu14.04.5 安装TensorFlow 官方文档:https://www.tensorflow.org/install/install_linux sudo pip instal ...

  3. logger 的使用一 小结

    方式一 依赖: <!-- log start --> <dependency> <groupId>log4j</groupId> <artifac ...

  4. 「小程序JAVA实战」小程序视频封面处理(48)

    转自:https://idig8.com/2018/09/16/xiaochengxujavashizhanxiaochengxushipinfengmianchuli47/ 截图这块,在微信小程序工 ...

  5. How To Install Spring IDE In Eclipse

    Spring IDE is a very useful graphical user interface tool adding support for Spring Framework. In th ...

  6. 小学生轻松做题App

    作业链接:https://edu.cnblogs.com/campus/fzzcxy/2016SE/homework/2180 原型模型设计工具:墨刀 原型模型链接:https://modao.cc/ ...

  7. codeforces:Prefix Sums分析和实现

    题目大意: 给出一个函数P,P接受一个数组A作为参数,并返回一个新的数组B,且B.length = A.length + 1,B[i] = SUM(A[0], ..., A[i]).有一个无穷数组序列 ...

  8. 在windows下安装python包管理器pip及使用

      从来没有在Windows下用过pip,今天试了下,原来pip也可以在Windows下安装,使用也和Linux下一样简单. 先从下面的地址下载pip源码: http://pypi.python.or ...

  9. Web页面显示日期和动态时刻脚本

    <script language="JavaScript" type="text/JavaScript">    <!--        tm ...

  10. 6-Collision-hdu5114(小球碰撞)

    Collision Time Limit: 15000/15000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Tot ...