1138 - Trailing Zeroes (III)
Time Limit: 2 second(s) Memory Limit: 32 MB

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


PROBLEM SETTER: JANE ALAM JAN

题意:给你一个数字,这个数字代表N!后面有几个0。给出这个数字,计算N的值。

解题思路:

不论什么质因数都能够写成素数相乘的形式。所以计算一个数的阶乘后面几个0。仅仅需计算这个数包括多少5就可以。(关于这点不清楚的点:点击打开链接)。

能够用二分法,找出这个点。

想到用二分这道题也就没什么难度了。

AC代码;

<span style="font-size:18px;">#include <stdio.h>
#include <math.h>
#include <vector>
#include <queue>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> using namespace std; typedef long long LL; LL solve(LL n)
{
LL num=0;
while(n){
num+=n/5;
n/=5;
}
return num;
} LL er(LL n)
{
LL x=1;
LL y=1844674407370;
LL mid;
LL res=-1;
while(y>=x){
mid=(x+y)/2;
LL ans=solve(mid);
if(ans==n){
res=mid;
y=mid-1;
//return mid;
}
else if(ans>n){
y=mid-1;
}
else if(ans<n){
x=mid+1;
}
}
return res;
}
int main()
{ int t;
scanf("%d",&t);
int xp=1;
while(t--){
LL n;
scanf("%lld",&n);
LL ans=er(n);
if(ans==-1) printf("Case %d: impossible\n",xp++);
else printf("Case %d: %d\n",xp++,ans);
}
return 0;
}
</span>

LightOJ Trailing Zeroes (III) 1138【二分搜索+阶乘分解】的更多相关文章

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

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

  2. Trailing Zeroes (III) -;lightoj 1138

    Trailing Zeroes (III)   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...

  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) 【二分查找好题】【 给出N!末尾有连续的Q个0,让你求最小的N】

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

  5. 1138 - Trailing Zeroes (III) 二分

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

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

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

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

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

  8. Trailing Zeroes (III) 假设n!后面有x个0.现在要求的是,给定x,要求最小的n; 判断一个n!后面有多少个0,通过n/5+n/25+n/125+...

    /** 题目:Trailing Zeroes (III) 链接:https://vjudge.net/contest/154246#problem/N 题意:假设n!后面有x个0.现在要求的是,给定x ...

  9. 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

随机推荐

  1. 110_leetcode_Best Time to Buy and sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  2. Hibernate的延迟检索和立即检索

    一.立即检索 所谓立即检索就是立即装载和初始化检索方法指定的对象,即使Session关闭了,依然可以正常访问.立即检索策略的启用是通过在映射配置文件中将lazy实行值设置为false实现的. 通俗讲就 ...

  3. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  4. hdu Escape

    Escape 题目: 非常裸的多重匹配. 可是点数较多,所以要用到状态压缩. . .. .. 第一次写. 好厉害的赶脚. #include <iostream> #include < ...

  5. ES API 备忘

    本文所列的所有API在ElasticSearch文档是有详尽的说明,但它的结构组织的不太好. 这篇文章把ElasticSearch API用表格的形式供大家参考. https://www.iteblo ...

  6. ORA-1157错误解决

    一.错误描述 ORA-1157, "cannot identify/lock data file %s - see DBWR trace file" 引起的原因: 因为数据文件已经 ...

  7. Ubuntu新建用户并加入SUDO组

    Ubuntu新建用户并加入SUDO组 新建用户: adduser xxxx 加入用户组: usermod -aG sudo username

  8. Java序列化注意事项

    当父类继承Serializeble接口时,所有子类可以被序列化 子类实现了Serializeble接口,父类没有,父类中的属性不能序列化(不报错,数据会丢失),但是在子类中属性仍能正确序列化 如果序列 ...

  9. Linux下安装ipython与jupyter

    IPython从Python发展而来,更倾向于科学计算.互联网数据分析更喜欢用. 首先切换root用户: su - root pip3自动安装ipython [root@hear ~]# ipytho ...

  10. CDN(Content Distribution Network)概念

    CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节 ...