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

    snprintf(),函数原型为int snprintf(char *str, size_t size, const char *format, ...).   将可变参数 “…” 按照format的 ...

  2. 【codeforces 235B】Let's Play Osu!

    [题目链接]:http://codeforces.com/problemset/problem/235/B [题意] 让你玩一个游戏,游戏结果由一个长度为n的01字符组成; 这个结果的分数与连续的1的 ...

  3. python多线程编程代码

    参考了这篇文章,写的挺好的. http://blog.csdn.net/abcjennifer/article/details/49474897 import time import threadin ...

  4. npm --save 选项作用

    npm --save 选项作用 学习了:https://segmentfault.com/q/1010000000403629 -save和save-dev可以省掉你手动修改package.json文 ...

  5. Android在程序中浏览网页

    本文是自己学习所做笔记,欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 有时须要在程序中浏览一些网页.当然了能够通过调用系统的浏览器来打开浏览.可是大多 ...

  6. [IOS]mac远程window全屏显示

    在mac自带着一个远程window的软件.这让我们远程起来很方便. 其步骤和window远程也很相似. 输入ip地址: 输入username以及password: 然后点击确定就可以. 只是.这时就出 ...

  7. 闲来无事爬了下通讯录 试手 jsdom

    curl http://xxx.com/address/addresslist\?search\=%40 --cookie oa_cookie=123 -s| node parss .js js 代码 ...

  8. ThinkPHP5.0框架开发--第9章 TP5.0视图和模板

    ThinkPHP5.0框架开发--第9章 TP5.0视图和模板 第9章 TP5.0视图和模板 ===================================================== ...

  9. nyoj--514--1的个数(贪心)

     1的个数 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 给你两个数a和b,你的任务是计算出1在a和b之间出现的次数,比如说,如果a=1024,b=1032,那么a ...

  10. 从零开始编写一个vue插件

    title: 从零开始编写一个vue插件 toc: true date: 2018-12-17 10:54:29 categories: Web tags: vue mathjax 写毕设的时候需要一 ...